-1
我正在使用一個SQLite數據庫,我得到了這個錯誤:
引起:android.database.sqlite.SQLiteException:沒有這樣的列:coin(code 1):,while compiling:SELECT _id,名稱,年齡,硬幣,日誌FROM person_tb1 WHERE name =?SQLite異常:沒有這樣的列
我不知道它爲什麼給我看這個!
我已經嘗試過它的工作正常!
這裏是我的代碼:
public ArrayList<Person> getPersons(){
persons.clear();
SQLiteDatabase dba = this.getReadableDatabase();
Cursor cursor = dba.query(Constants.TABLE_NAME,
new String[]{Constants.KEY_ID, Constants.NAME, Constants.AGE , Constants.COIN , Constants.LOG},null,null,null,null,null);
if(cursor.moveToFirst()){
do {
Person p = new Person();
p.setName(cursor.getString(cursor.getColumnIndex(Constants.NAME)));
p.setAge(cursor.getInt(cursor.getColumnIndex(Constants.AGE)));
p.setCoin(cursor.getInt(cursor.getColumnIndex(Constants.COIN)));
p.setLog(cursor.getInt(cursor.getColumnIndex(Constants.LOG)));
p.setPersonId(cursor.getInt(cursor.getColumnIndex(Constants.KEY_ID)));
persons.add(p);
}while (cursor.moveToNext());
cursor.close();
dba.close();
}
return persons;
}
,這裏的選擇方法:
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + Constants.TABLE_NAME + "(" +
Constants.KEY_ID + " INTEGER PRIMARY KEY, " + Constants.NAME + " TEXT, "+
Constants.AGE + " INT, " + Constants.COIN + " INT, " + Constants.LOG + " INT);";
db.execSQL(CREATE_TABLE);
}
//編輯:(OnUpgrade)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + Constants.TABLE_NAME);
onCreate(db);
}
或只是+1的數據庫版本,並確保應用任何遷移需要在onUpgrade(通常只是一個滴/創建) – njzk2
是的,另一種方法:) –
我在我的代碼中有onUpgrade ^^!看我更新了我的帖子 –