无法将数据插入SQLite-android

时间:2017-04-19 15:02:25

标签: android database sqlite android-sqlite

我想用SQLite数据库管理器程序将数据添加到我创建的SQLite数据库中,我得到“只读数据库”错误

当我评论插入行时,程序运行正常,并在取消注释时崩溃。

注意:我已经请求了读/写存储权限,并且已被授予。

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`

2 个答案:

答案 0 :(得分:0)

  1. 如果以前创建的表没有列,请确认设备数据库是您在创建语句中所需的方式。您可以通过卸载应用程序并重新部署它来完成此操作。
  2. 然后使用ContentValues类和database.insert进行实际插入。这里解释了很多:https://developer.android.com/training/basics/data-storage/databases.html,但这里简短而简单:
  3. 删除此行:

    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);
    

答案 1 :(得分:0)

SQL使用&#34;双引号&#34;引用表/列名称和单引号&#39;引用字符串。

`Backticks`是误导性的MySQL兼容尝试;永远不要使用它们。