0

我在创建数据库时总是遇到这个错误:

java.lang.IllegalArgumentException:版本必须 >= 版本 0 在 android.database.sqlite.SQLiteOpenHelper

任何想法如何解决这个问题。我的代码如下

public class DatabaseHandler extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME   = "Exm"; 
    private static final String KEY_ID          = "id";
    private static final String KEY_USERID      = "userId";
    private static final String KEY_USERNAME    = "userName";
    private static final String KEY_LOGINSTATUS = "userLoginStatus";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USER + "("
                                        + KEY_ID +   " INTEGER PRIMARY KEY ,"
                                        + KEY_USERID + " VARCHAR (32) ,"
                                        + KEY_USERNAME + " VARCHAR (50),"
                                        + KEY_LOGINSTATUS + " INTEGER" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        // Create tables again
        onCreate(db);
    }
}
4

2 回答 2

1

Make sure you have declared TABLE_USER. I cannot see it in your code above.

于 2013-03-18T07:02:50.177 回答
0

您不应该在 onUpgrade() 中调用 onCreate(db)。

如果您想重用代码,请将那段代码放在其他地方,然后从 onCreate 和 onUpgrade 调用该方法。

字符串 CREATE_CONTACTS_TABLE = "创建表 " + TABLE_USER + "("
                                    + KEY_ID + " 整数主键 "
                                    + KEY_USERID + " VARCHAR (32) "
                                    + KEY_USERNAME + " VARCHAR (50),"
                                    + KEY_LOGINSTATUS + "整数" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);

将这段代码保存在另一个方法中,并从 onCreate 和 onUpgrade 方法中调用该方法。

于 2013-03-18T07:41:52.960 回答