我一直有问题让我的SQLite数据库工作,我认为它可能不是我的代码。我从here下载了一个示例SQLite应用程序,它在我的模拟器或手机上无效。以下是您要查看的LogCat错误。
如果您对可能发生的事情有任何想法,请告诉我。
03-04 00:48:26.472: E/Database(6653): Error inserting DeptID=3 DeptName=HR
03-04 00:48:26.472: E/Database(6653): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
03-04 00:48:26.472: E/Database(6653): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
03-04 00:48:26.472: E/Database(6653): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
03-04 00:48:26.472: E/Database(6653): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
03-04 00:48:26.472: E/Database(6653): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.DatabaseHelper.InsertDepts(DatabaseHelper.java:144)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.DatabaseHelper.onCreate(DatabaseHelper.java:65)
03-04 00:48:26.472: E/Database(6653): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.DatabaseHelper.getEmployeeCount(DatabaseHelper.java:105)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.AddEmployee.onStart(AddEmployee.java:40)
03-04 00:48:26.472: E/Database(6653): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
03-04 00:48:26.472: E/Database(6653): at android.app.Activity.performStart(Activity.java:3791)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
03-04 00:48:26.472: E/Database(6653): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-04 00:48:26.472: E/Database(6653): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-04 00:48:26.472: E/Database(6653): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:657)
03-04 00:48:26.472: E/Database(6653): at android.widget.TabHost.setCurrentTab(TabHost.java:329)
03-04 00:48:26.472: E/Database(6653): at android.widget.TabHost.addTab(TabHost.java:219)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.DatabaseDemo.SetupTabs(DatabaseDemo.java:85)
03-04 00:48:26.472: E/Database(6653): at mina.android.DatabaseDemo.DatabaseDemo.onCreate(DatabaseDemo.java:32)
03-04 00:48:26.472: E/Database(6653): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-04 00:48:26.472: E/Database(6653): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 00:48:26.472: E/Database(6653): at android.os.Looper.loop(Looper.java:130)
03-04 00:48:26.472: E/Database(6653): at android.app.ActivityThread.main(ActivityThread.java:3688)
03-04 00:48:26.472: E/Database(6653): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 00:48:26.472: E/Database(6653): at java.lang.reflect.Method.invoke(Method.java:507)
03-04 00:48:26.472: E/Database(6653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-04 00:48:26.472: E/Database(6653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-04 00:48:26.472: E/Database(6653): at dalvik.system.NativeStart.main(Native Method)
03-04 00:48:26.925: W/PhoneWindow(6653): Previously focused view reported id 2131034112 during save, but can't be found during restore.
答案 0 :(得分:0)
此示例中存在错误。看DatabaseHelper:144
。你会发现这行代码两次:
db.insert(deptTable, colDeptID, cv);
第一次调用成功,第二次尝试插入具有相同ID的行,因此它违反了主键约束。删除它。
我不明白为什么有人发布未经测试的代码示例......