2017-04-19 93 views
-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` 
+0

請使用這裏解釋的SQLiteOpenHelper:https://developer.android.com/training/basics/data-storage/databases.html (或領域) – petey

+0

此外,這些警告線不是從您的應用程序。你可以把它改變成崩潰的實際錯誤日誌嗎? – petey

+0

@petey謝謝你的回答,我添加了實際的錯誤日誌。 – Ahmad

回答

0
  1. 確認設備數據庫就是希望它在你的表沒有列先前創建的情況下,您創建語句的方式。您可以通過卸載應用程序並重新部署應用程序來完成此操作。
  2. 然後用ContentValues類和database.insert來做實際的插入。這方面的一個很好的協議在這裏解釋:https://developer.android.com/training/basics/data-storage/databases.html,但這裏是短而簡單:

刪除此行:

database.execSQL("INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)"); 

,取而代之的是:

ContentValues values = new ContentValues(); 
values.put("name", "John"); 
values.put("family", "Doe"); 
long insertedRowId = database.insert("tbltest", null, values); 
0

SQL使用引用表/列名的「雙引號」和引用字符串的「單引號」。

「反引號」被誤導爲與MySQL兼容;不要使用它們。