dimanche 1 février 2015

SQLite Database, column doesn't exist

I'm trying to create this database and then retrieve the values from it, but I just can't figure out the syntax. I've been searching google all weekend and I'm going crazy. Please help me understand why this isn't working. Here is the SQLiteHelper:



public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "DBName.db";
public static final String RESULTS_TABLE_NAME = "results";
public static final String RESULTS_COLUMN_ID = "id";
private static final String KEY_WEATHER = "weather";
private static final String KEY_WEEKEND = "weekend";
private static final String KEY_HOBBIES = "hobbies";
private static final String KEY_MUSIC = "music";
private static final String KEY_FAMILY = "family";

private static final String[] columnArray = {KEY_WEATHER, KEY_WEEKEND, KEY_HOBBIES, KEY_MUSIC, KEY_FAMILY};

private HashMap hp;

public DBHelper(Context context){
super(context, DATABASE_NAME, null, 1);

}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("create db", "created database");
db.execSQL("CREATE TABLE results "+
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"weather0 INTEGER, weather1 INTEGER, weather2 INTEGER, weather3 INTEGER, weather4 INTEGER, " +
"weekend0 INTEGER, weekend1 INTEGER, weekend2 INTEGER, weekend3 INTEGER, weekend4 INTEGER, " +
"hobbies0 INTEGER, hobbies1 INTEGER, hobbies2 INTEGER, hobbies3 INTEGER, hobbies4 INTEGER, " +
"music0 INTEGER, music1 INTEGER, music2 INTEGER, music3 INTEGER, music4 INTEGER, " +
"family0 INTEGER, family1 INTEGER, family2 INTEGER, family3 INTEGER, family4 INTEGER );");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLES IF EXISTS results");
this.onCreate(db);
}

public boolean insertResult (String[] topics){

//String topicUnitId = column+unitId;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
/*for(int topicLoop = 0; topicLoop < topics.length; topicLoop++){
for(int unitLoop = 0; unitLoop < 5; unitLoop++){
contentValues.put(topics[topicLoop].toLowerCase(), -1);
Log.d("CREATING TABLE", topics[topicLoop]+unitLoop);
}
}*/
values.put("weather0", -1);
values.put("weather1", -1);
values.put("weather2", -1);
values.put("weather3", -1);
values.put("weather4", -1);
db.insert(RESULTS_TABLE_NAME, null, values);
db.close();
return true;
}

public ArrayList getAllResults(int i){

String arrayId = columnArray[i];
ArrayList arrayList = new ArrayList();
SQLiteDatabase db = this.getReadableDatabase();
for(int n = 0; n <= 4; n++) {
Cursor res = db.rawQuery("SELECT * FROM results WHERE " + arrayId + n +" IS NOT NULL;", null);
res.moveToFirst();
while (res.isAfterLast() == false) {
arrayList.add(res.getString(res.getColumnIndex(arrayId+n)));
res.moveToNext();
}
res.close();
}

db.close();
return arrayList;
}
}


I get this error:



android.database.sqlite.SQLiteException: no such column: weather0 (code 1): , while compiling: SELECT * FROM results WHERE weather0 IS NOT NULL;


When I try to run "getAllResults()" from another activity in my app.


Thanks for any help.


Aucun commentaire:

Enregistrer un commentaire