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
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 –
Veuillez écrire votre nom de colonne sans() 'parenthèses'. –
Vous voulez dire ça? "INSERT INTO Station nom_station VALUES ('" + stations [i] [0] + "');" ' –