android studio SQLite创建表查询语法错误

时间:2015-10-21 22:37:24

标签: android sqlite

引起:android.database.sqlite.SQLiteException:接近“CREATE_TABLE”:语法错误(代码1):,同时编译:CREATE_TABLE用户(用户名TEXT,密码TEXT);

公共类MyDB扩展了SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "users.db";
private static final String TABLE_USERS = "users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";


public MyDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

// called when database is first created
@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE_TABLE " + TABLE_USERS + "(" +
         //   COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL DEFAULT 1" +
            COLUMN_USERNAME + " TEXT, " +
            COLUMN_PASSWORD + " TEXT " +
            ");";

    db.execSQL(query);
}

2 个答案:

答案 0 :(得分:2)

SQL CREATE TABLE语句中没有下划线。将"CREATE_TABLE "更改为"CREATE TABLE "

另见the relevant documentation

答案 1 :(得分:0)

另外,快速提示。您不必告诉SQL COLUMN_ID是INTEGER PRIMARY KEY AUTOINCREMENT。 SQL中的INTEGER PRIMARY KEY将始终采用行ID的值。唯一一次必须添加INCREMENT时,如果您在删除数据时不想要重复使用的值(因为,就像我之前说的那样,它需要行的ID)。