AUTOINCREMENT
只能用作列類型名稱的一部分,必須緊跟INTEGER PRIMARY KEY
。
你將不得不線沿線的一個錯誤: -
08-18 14:23:18.865 12008-12008/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: mjt.so45722118, PID: 12008
java.lang.RuntimeException: Unable to start activity ComponentInfo{mjt.so45722118/mjt.so45722118.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE user_account(id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, full_name TEXT NOT NULL, address TEXT NOT NULL, contact TEXT NOT NULL, password TEXT NOT NULL)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE user_account(id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, full_name TEXT NOT NULL, address TEXT NOT NULL, contact TEXT NOT NULL, password TEXT NOT NULL)
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:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at mjt.so45722118.DatabaseHelper.onCreate(DatabaseHelper.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at mjt.so45722118.DatabaseHelper.<init>(DatabaseHelper.java:24)
at mjt.so45722118.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
因此,你需要改變: -
+COL_1+" INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, "
到
+COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT, "
你甚至可以不代碼AUTOINCREMENT
(可能不是,按照,AUTOINCR EMENT關鍵字會增加額外的CPU,內存,磁盤空間和磁盤I/O開銷,如果不是嚴格需要,應該避免。通常不需要。SQLite Autoincrement)。
INTEGER PRIMARY KEY
暗示NOT NULL
因此不需要。
所以我建議剛編碼: -
+COL_1+" INTEGER PRIMARY KEY, "
您可以在下面的幾行添加到MainActivity
,後database = new DatabaseHelper(this);
: -
Cursor csr = database.getWritableDatabase().rawQuery("PRAGMA TABLE_INFO(" + DatabaseHelper.TABLE_NAME + ")",null)
String rowinfo;
while (csr.moveToNext()) {
rowinfo = "Row position=" + Integer.toString(csr.getPosition());
for (int i=0;i < csr.getColumnCount();i++) {
rowinfo = rowinfo + " Column=" + csr.getColumnName(i) +
" Value=" + csr.getString(i);
}
Log.d("TABLEINFO",rowinfo);
}
csr.close();
這則顯示日誌在以下告訴你數據庫和表存在: -
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=0 Column=cid Value=0 Column=name Value=id Column=type Value=INTEGER Column=notnull Value=0 Column=dflt_value Value=null Column=pk Value=1
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=1 Column=cid Value=1 Column=name Value=full_name Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=2 Column=cid Value=2 Column=name Value=address Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=3 Column=cid Value=3 Column=name Value=contact Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=4 Column=cid Value=4 Column=name Value=password Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
您究竟在哪裏查找數據庫?請注意,數據庫所在的應用程序私有文件夾實際上是私人的,除非設備是根源的(例如模擬器),否則您無法在其中看到它。 – laalto