2017-10-11 2 views
1

J'ai essayé d'ajouter quelques éléments d'un tableau de chaînes dans une base de données et je n'arrive pas à obtenir l'insertion de travailler sur ce que j'ai vu here Ive essayé diverses variantes de la requête d'insertion.Les données ne sont pas insérées dans la base de données Sqlite à l'aide de la requête d'insertion

Voici mon code:

SQLiteDatabase db1 = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null); 
    try{ 
     String query = "CREATE TABLE IF NOT EXISTS Station (" 
       + "Station_name VARCHAR);"; 
     db1.execSQL(query); 
     Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show(); 
     for(i=0;i<10;i++) 
     { 
      Toast.makeText(MainActivity.this, stations[i][0],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i][0]+"');"; 
      db1.execSQL(query); 

     } 
    }catch (Exception e){ 
     Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show(); 
    } 
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null); 

    String[] station_array = new String[data_fetch.getCount()]; 
    data_fetch.moveToFirst(); 
    i = 0; 
    while (data_fetch.moveToNext()) { 
     String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name")); 
     station_array[i] = name; 
     i++; 
     Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show(); 
    } 
    data_fetch.close(); 
} 

Quand je Griller les données récupérées Le pain grillé dit retrieved datanull J'ai même essayé d'insérer une chaîne et non une variable mais je reçois encore datanull pains.

Toute aide serait appréciée.

PS C'est le tableau chaîne im tente d'insérer:

stations[0][0]= "New York"; 
    stations[1][0]= "Boston"; 
    stations[2][0]= "Las Vegas"; 
    stations[3][0]= "Miami"; 
    stations[4][0]= "Chicago"; 
    stations[5][0]= "New England"; 
    stations[6][0]= "Detroit"; 
    stations[7][0]= "Michigan"; 
    stations[8][0]= "Austin"; 
    stations[9][0]= "New Orealns"; 

Répondre

1

votre nom de colonne code manquant après le nom de la table dans la requête d'insertion:

insert correct Question:

INSERT INTO Station Station_name VALUES ('"+stations[i][0]+"'); 

Exemple:

INSERT INTO table_name_here (column1, column2, column3) VALUES ("Learn PHP", "John Poul", NOW()); 
+0

I Tried insertion avec 'query = "INSERT INTO VALUES Station (STATION_NAME) (' "+ stations [i] [0] +"');"; db1.execSQL (query); 'Et je reçois toujours datanull comme sortie –

+0

Veuillez écrire votre nom de colonne sans() 'parenthèses'. –

+0

Vous voulez dire ça? "INSERT INTO Station nom_station VALUES ('" + stations [i] [0] + "');" ' –

0

Je crois que la question découle de l'utilisation de tableau à deux dimensions: -

stations[0][0]= "New York"; 
stations[1][0]= "Boston"; 
stations[2][0]= "Las Vegas"; 
stations[3][0]= "Miami"; 
stations[4][0]= "Chicago"; 
stations[5][0]= "New England"; 
stations[6][0]= "Detroit"; 
stations[7][0]= "Michigan"; 
stations[8][0]= "Austin"; 
stations[9][0]= "New Orealns"; 

utilisent plutôt soit: -

String[] stations = new String[10]; 
    stations[0]= "New York"; 
    stations[1]= "Boston"; 
    stations[2]= "Las Vegas"; 
    stations[3]= "Miami"; 
    stations[4]= "Chicago"; 
    stations[5]= "New England"; 
    stations[6]= "Detroit"; 
    stations[7]= "Michigan"; 
    stations[8]= "Austin"; 
    stations[9]= "New Orealns"; 

ou (probablement celui-ci comme il est plus flexible)

String[] stations = new String[] { 
      "New York", 
      "Boston", 
      "Las Vegas", 
      "Miami", "Chicago", 
      "New England", 
      "Detroit", 
      "Michigan", 
      "Austin", 
      "New Orleans" 
    }; 

le long avec: -

for(i=0;i< stations.length();i++) 
     { 
      Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i]+"');"; 
      db1.execSQL(query); 

     } 

Sinon la requête pourrait inclure le nom de colonne (s) et être: -

query = "INSERT INTO Station Station_name VALUES ('"+stations[i]+"');"; 

Une version entièrement fonctionnelle de votre code pourrait être: -

String[] stations = new String[] { 
      "New York", 
      "Boston", 
      "Las Vegas", 
      "Miami", "Chicago", 
      "New England", 
      "Detroit", 
      "Michigan", 
      "Austin", 
      "New Orleans" 
    }; 

    int i=0; 
    SQLiteDatabase db1 = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null); 
    try{ 
     String query = "CREATE TABLE IF NOT EXISTS Station (" 
       + "Station_name VARCHAR);"; 
     db1.execSQL(query); 
     Log.d("STATION_TBLCRT","Table Created"); 
     //Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show(); 
     for(i=0;i<stations.length;i++) 
     { 
      Log.d("STATION_INS","Inserting station " + stations[i]); 
      //Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i]+"');"; 
      db1.execSQL(query); 

     } 
    }catch (Exception e){ 
     Log.d("STATION_INSERR","Error inserting station " + stations[i]); 
     //Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show(); 
    } 
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null); 

    String[] station_array = new String[data_fetch.getCount()]; 
    //data_fetch.moveToFirst(); 
    i = 0; 
    while (data_fetch.moveToNext()) { 
     String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name")); 
     station_array[i] = name; 
     Log.d("STATION_GET","Retrieved station " + station_array[i]); 
     i++; 
     //Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show(); 
    } 
    data_fetch.close(); 
} 

Remarques!

  • Toasts ont été remplacées par Connexion
  • data_fetch.moveToFirst a été commenté comme cela aboutirait à sauter la première ligne.
  • Si ce qui précède est exécuté plus d'une fois, des ensembles de stations supplémentaires seront ajoutés.
  • le i ++ dans le data_fetch a été déplacé à après avoir signalé la station (les données seraient sinon toujours nulles). L'utilisation de Toasts en raison de la durée de Toast peut être trompeuse, d'où l'utilisation de Log pour afficher tous les résultats.

Exemple de sortie: -

10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_TBLCRT: Table Created 
10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New York 
10-14 08:16:37.451 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Boston 
10-14 08:16:37.454 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Las Vegas 
10-14 08:16:37.457 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Miami 
10-14 08:16:37.461 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Chicago 
10-14 08:16:37.465 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New England 
10-14 08:16:37.468 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Detroit 
10-14 08:16:37.471 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Michigan 
10-14 08:16:37.473 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Austin 
10-14 08:16:37.478 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New Orleans 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New York 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Boston 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Las Vegas 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Miami 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Chicago 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New England 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Detroit 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Michigan 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Austin 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New Orleans