这是我要创建的代码:
private static final String TABLE_CURRENT_GAME = "current";
private static final String C_KEY_ID = "C_id";
private static final String C_SCORE_1 = "C_score_1";
private static final String C_SCORE_2 = "C_score_2";
private static final String C_K_1 = "C_k_1";
private static final String C_K_2 = "C_k_2";
public void onCreate(SQLiteDatabase db) {
String CREATE_CURRENT_GAME_TABLE = "CREATE TABLE " + TABLE_CURRENT_GAME + "("
+ C_KEY_ID + " INTEGER PRIMARY KEY," + C_SCORE_1+"INT,"+C_SCORE_2+"INT,"+C_K_1+"INT,"+C_K_2+"INT)";
db.execSQL(CREATE_CURRENT_GAME_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CURRENT_GAME);
// Create tables again
onCreate(db);
}
和我的插入代码:
void addCurrentGameData(CurrentGameTable data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(C_SCORE_1, data.getScore1()); // score player1
values.put(C_SCORE_2, data.getScore2());//score player2
values.put(C_K_1, data.getKseres1());//kseres player1
values.put(C_K_2, data.getKseres2());//kseres player2
// Inserting Row
db.insert(TABLE_CURRENT_GAME, null, values);
db.close(); // Closing database connection
}
CurrentGameData 是我的构造函数。
然后当尝试像这样插入表格时:
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addCurrentGameData(new CurrentGameTable(1,2,3,4));
在我的手机中,我收到此错误:
08-19 16:36:23.876: E/Database(3996): Error inserting current
08-19 16:36:23.876: E/Database(3996): android.database.sqlite.SQLiteException: table current has no column named C_score_2: , while compiling: INSERT INTO current(C_score_2, C_k_1, C_score_1, C_k_2) VALUES(?, ?, ?, ?);
在我的模拟器中,我收到此错误:
08-19 13:48:19.036: E/AndroidRuntime(519): FATAL EXCEPTION: main
08-19 13:48:19.036: E/AndroidRuntime(519): android.database.sqlite.SQLiteException: no such table: current: , while compiling: SELECT * FROM current
08-19 13:48:19.036: E/AndroidRuntime(519): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
使用相同语法创建的其他表可以正常工作,但第二个表不能正常工作。