当我尝试onCreate时CREATE_USER_TABLE出现错误

时间:2019-03-10 02:25:22

标签: android database sqlite

//这是我的DatabaseHelper.java

CREATE_USER_TABLE = "CREATE TABLE" + TABLE_USER + "(" + COLUMN_USER_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + "TEXT," + COLUMN_USER_EMAIL + "TEXT," + COLUMN_USER_PASSWORD + "TEXT," + ")";

private String DROP_USER_TABLE = "DROP TABLE IF EXISTS" + TABLE_USER;

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_USER_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_USER_TABLE);
    onCreate(db);
}

public void addUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_USER_NAME, user.getName());
    values.put(COLUMN_USER_EMAIL, user.getEmail());
    values.put(COLUMN_USER_PASSWORD, user.getPassword());

    db.insert(TABLE_USER, null, values);
    db.close();
}

public boolean checkUser(String email, String password) {
    String[] columns = {
            COLUMN_USER_ID
    };
    SQLiteDatabase db = this.getWritableDatabase();

    String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + "=?";
    String[] selectionArgs = {email,password};

    Cursor cursor = db.query(TABLE_USER,
            columns,
            selection,
            selectionArgs,
            null,
            null,
            null);
    int cursorCount = cursor.getCount();
    cursor.close();
    db.close();
    if (cursorCount > 0){
        return  true;
    }
    return false;
}`

1 个答案:

答案 0 :(得分:0)

您没有发布错误是什么,但是,查看您的代码,我可以看到一个主要问题。

需要记住的一点是,SQLite依赖其语句中的适当空格。所以,你的声明

CREATE_USER_TABLE = "CREATE TABLE" + TABLE_USER + "(" + 
COLUMN_USER_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + "TEXT," + COLUMN_USER_EMAIL + "TEXT," + COLUMN_USER_PASSWORD + "TEXT," + ")";

将翻译为类似

CREATE user_table useridINTEGER PRIMARY KEY AUTOINCREMENT,user_nameTEXTuser_emailTEXT,user_passwordTEXT,)

看到问题了吗?空间不在需要的地方。您将需要产生这样的声明:

CREATE user_table, userid INTEGER PRIMARY KEY  AUTOINCREMENT,user_name TEXT, user_email TEXT, user_password TEXT

以正确的方式插入空格。

进行logcat打印以查看正在生成的语句,以便进行调整。