" AUTOINCREMENT"附近的SQLite语法错误

时间:2017-07-15 14:03:53

标签: android sqlite android-sqlite

错误:

07-15 19:21:59.831 5770-5770/com.example.android.resumemaker E/SQLiteLog: (1) near "AUTOINCREMENT": syntax error
07-15 19:21:59.833 5770-5770/com.example.android.resumemaker D/AndroidRuntime: Shutting down VM
07-15 19:21:59.834 5770-5770/com.example.android.resumemaker E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.android.resumemaker, PID: 5770
   android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE users(id INTEGER AUTOINCREMENT, name TEXT, address TEXT, phone INTEGER, email TEXT PRIMARY KEY, password TEXT, career_objectives TEXT, technical_skills TEXT, work_experience TEXT, hobbies TEXT)
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1675)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
       at com.example.android.resumemaker.UserDbHelper.onCreate(UserDbHelper.java:74)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
       at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
       at com.example.android.resumemaker.MainActivity$1.onClick(MainActivity.java:31)
       at android.view.View.performClick(View.java:5612)
       at android.view.View$PerformClick.run(View.java:22285)
       at android.os.Handler.handleCallback(Handler.java:751)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6123)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

这是我的代码:

private static final String DB_NAME="mywbut";
private static final int DB_VERSION=3;
private static final String TABLE_NAME_1="users";
private static final String COL1_1="id";
private static final String COL1_2="name";
private static final String COL1_3="address";
private static final String COL1_4="phone";
private static final String COL1_5="email";
private static final String COL1_6="password";
private static final String COL1_7="career_objectives";
private static final String COL1_8="technical_skills";
private static final String COL1_9="work_experience";
private static final String COL1_10="hobbies";
private static final String CREATE_TABLE_1=" CREATE TABLE " + TABLE_NAME_1 + "(" +
        COL1_1+ " INTEGER AUTOINCREMENT, " + COL1_2 + " TEXT, " +
        COL1_3 + " TEXT, " + COL1_4 + " INTEGER, " + COL1_5 + " TEXT PRIMARY KEY, " +
        COL1_6 + " TEXT, " + COL1_7 + " TEXT, " + COL1_8 + " TEXT, " +
        COL1_9 + " TEXT, " + COL1_10 + " TEXT)";

2 个答案:

答案 0 :(得分:0)

使用此声明

private static final String CREATE_TABLE_1=" CREATE TABLE " + TABLE_NAME_1 + "(" +
        COL1_1+ " INTEGER PRIMARY KEY, " + COL1_2 + " TEXT, " +
        COL1_3 + " TEXT, " + COL1_4 + " INTEGER, " + COL1_5 + " TEXT PRIMARY KEY, " +
        COL1_6 + " TEXT, " + COL1_7 + " TEXT, " + COL1_8 + " TEXT, " +
        COL1_9 + " TEXT, " + COL1_10 + " TEXT)";

使用INTEGER PRIMARY KEY代替AUTOINCREMENT

答案 1 :(得分:0)

自动增量仅适用于整数列PK:

https://sqlite.org/autoinc.html