[英]Android SQLLite Table Column Missing Error
This is my code to create: 这是我要创建的代码:
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);
}
and my code for inserting: 和我的插入代码:
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
}
where CurrentGameData is my constructor. 其中CurrentGameData是我的构造函数。
Then when trying to insert the table like that: 然后在尝试插入表时:
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addCurrentGameData(new CurrentGameTable(1,2,3,4));
In my phone I get this error: 在我的手机中我收到此错误:
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(?, ?, ?, ?);
In my emulator I get this error: 在我的模拟器中,我收到此错误:
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)
Other table created with the same syntax works properly but this, second one, does not. 使用相同语法创建的其他表可正常工作,但第二个表没有。
Go to Settings -> Manage Applications -> Your Application -> Clear all application data. 转到设置 - >管理应用程序 - >您的应用程序 - >清除所有应用程序数据。
Maybe it is using an old table stored in the phone before you added this column in your table. 在表中添加此列之前,可能正在使用存储在手机中的旧表。
for example : 例如 :
C_SCORE_1+"INT,"
create one column named C_score_1INT without type, 创建一个名为C_score_1INT的列,不带类型,
your code shoud be (whith space added): 你的代码应该是(添加空格):
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)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.