Android SQLite表列缺少错误

时间:2012-08-19 14:05:18

标签: android sqlite

这是我要创建的代码:

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)

使用相同语法创建的其他表可以正常工作,但是第二个表没有。

2 个答案:

答案 0 :(得分:1)

转到设置 - >管理应用程序 - >您的申请 - >清除所有应用程序数据。

在您在表格中添加此列之前,可能正在使用存储在手机中的旧表格。

答案 1 :(得分:1)

例如:

C_SCORE_1+"INT,"

创建一个名为C_score_1INT的列,不带类型

你的代码应该是(添加空格):

     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)";