2017-04-19 1 views
-1

Ich mag Daten in meine erstellten SQLite-Datenbank hinzuzufügen, mit SQLite-Datenbank-Manager-Programmen bekomme ich die „Nur-Lese-Datenbank“ Fehlerkippen Einfügen von Daten in SQLite-android

Wenn ich Insertionslinie das Programm funktioniert und stürzt ab, wenn unkommentiert Kommentar .

Hinweis: Ich habe eine Lese-/Schreib-Speicherberechtigung angefordert und sie wurde erteilt.

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 Fehler

 ` --------- 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

Verwenden Sie bitte den SQLiteOpenHelper, der hier erklärt wird: https://developer.android.com/training/basics/data-storage/databases.html (oder Realm) – petey

+0

Auch diese Warnungen Zeilen stammen nicht aus Ihrer App. Können Sie es in die tatsächlichen Fehlerprotokolle vom Absturz ändern? – petey

+0

@petey danke für die antwort, ich habe das tatsächliche fehlerprotokoll hinzugefügt. – Ahmad

Antwort

0
  1. sicher, dass die Gerätedatenbank ist so, wie Sie es wie in Ihrem erstellen Aussage im Fall, dass Sie Ihren Tisch vorher ohne eine Spalte erstellt wurde. Sie können dies tun, indem Sie die App deinstallieren und erneut bereitstellen.
  2. Dann verwenden Sie ContentValues Klasse und die database.insert, um die tatsächliche einfügen. Ein guter Teil davon wird hier erklärt: https://developer.android.com/training/basics/data-storage/databases.html, aber hier ist die kurze und einfach:

Entfernen Sie diese Zeile:

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

und ersetzen Sie es mit:

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

SQL Anwendungen "doppelte Anführungszeichen", um Tabellen-/Spaltennamen zu zitieren, und "einfache Anführungszeichen", um Zeichenfolgen zu zitieren.

`Backticks` sind ein fehlgeleiteter Versuch, MySQL-kompatibel zu sein; benutze sie nie, niemals.