-1
我想數據使用SQLite數據庫管理程序添加到我的創建SQLite數據庫我得到的「只讀數據庫」錯誤着將數據插入SQLite的,Android的
當我評論插入線的程序正常工作和崩潰註釋掉時。
注意:我已經請求讀/寫存儲權限並且已被授予。
public static Context context;
public static SQLiteDatabase database;
public static String SDK = Environment.getExternalStorageDirectory().toString();
public static String DB = SDK + "/database/";
context = getApplicationContext();
File file = new File(DB);
file.mkdirs();
database = SQLiteDatabase.openOrCreateDatabase(DB + "/db.sqlite",null);
database = SQLiteDatabase.openDatabase(DB + "/db.sqlite",null,SQLiteDatabase.OPEN_READWRITE);
database.execSQL("CREATE TABLE IF NOT EXISTS 'tbltest' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , 'name' VARCHAR, 'family' VARCHAR)");
database.execSQL("INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)");
database.close();
logcat的錯誤
` --------- beginning of crash
04-19 20:54:50.832 13353-13353/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: andishmand.database, PID: 13353
java.lang.RuntimeException: Unable to start activity ComponentInfo{andishmand.database/andishmand.database.MainActivity}: android.database.sqlite.SQLiteException: no such column: John (code 1): , while compiling: INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: no such column: John (code 1): , while compiling: INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)
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:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at andishmand.database.MainActivity.executecommand(MainActivity.java:128)
at andishmand.database.MainActivity.createdb(MainActivity.java:115)
at andishmand.database.MainActivity.requestperms(MainActivity.java:100)
at andishmand.database.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
04-19 20:54:50.853 1610-3265/? W/ActivityManager: Force finishing activity andishmand.database/.MainActivity`
請使用這裏解釋的SQLiteOpenHelper:https://developer.android.com/training/basics/data-storage/databases.html (或領域) – petey
此外,這些警告線不是從您的應用程序。你可以把它改變成崩潰的實際錯誤日誌嗎? – petey
@petey謝謝你的回答,我添加了實際的錯誤日誌。 – Ahmad