这里我想在我的数据库中插入一些内容。我还用this link 作为参考,但是徒劳无功。
但这是我的错误记录。
05-04 16:53:20.272: E/AndroidRuntime(18378): FATAL EXCEPTION: main
05-04 16:53:20.272: E/AndroidRuntime(18378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crews.databaseproto.activity/com.crews.databaseproto.activity.DatabaseProtoActivity}: java.lang.IllegalStateException: database not open
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.os.Looper.loop(Looper.java:130)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-04 16:53:20.272: E/AndroidRuntime(18378): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 16:53:20.272: E/AndroidRuntime(18378): at java.lang.reflect.Method.invoke(Method.java:507)
05-04 16:53:20.272: E/AndroidRuntime(18378): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-04 16:53:20.272: E/AndroidRuntime(18378): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-04 16:53:20.272: E/AndroidRuntime(18378): at dalvik.system.NativeStart.main(Native Method)
05-04 16:53:20.272: E/AndroidRuntime(18378): Caused by: java.lang.IllegalStateException: database not open
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:555)
05-04 16:53:20.272: E/AndroidRuntime(18378): at com.crews.databaseproto.activity.DataBaseHelper.insertData(DataBaseHelper.java:153)
05-04 16:53:20.272: E/AndroidRuntime(18378): at com.crews.databaseproto.activity.DatabaseProtoActivity.onCreate(DatabaseProtoActivity.java:37)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-04 16:53:20.272: E/AndroidRuntime(18378): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-04 16:53:20.272: E/AndroidRuntime(18378): ... 11 more
提前致谢。 对不起,链接错误
答案 0 :(得分:1)
按如下所示打开DataBase
SqliteDatabase db=myDbHelper.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(COLUMN_NAME,10);
cv.put(COLUMN_NAME,"Kaushik");
cv.put(COLUMN_NAME,"dhor sala");
cv.put(COLUMN_NAME,22.332322332);
cv.put(COLUMN_NAME,88.2323232);
float row id=db.insert(TABLE_NAME,null,cv);
答案 1 :(得分:1)
public synchronized SQLiteDatabase getWritableDatabase()
无需覆盖此功能。只需使用内置版。
此外,
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
更改您打开要写入的数据库的模式。
答案 2 :(得分:0)
插入了数据,但因为DeptId是主键,所以在表Dept中插入重复数据会出错。
05-05 00:01:09.783: E/Database(1011): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)