0
我無法將值插入到我的數據庫中。當我嘗試插入時收到數據不匹配錯誤。我對Android/SQLite/ContentValues相當陌生。我一直在關注一個教程,你可以找到Here。Android SQLite - 使用ContentValue插入問題
我試圖插入使用插入方法內置的SQLite的值。此方法的文檔是here。當我嘗試使用這種方法時,我最終遇到了一個奇怪的錯誤:
E/SQLiteLog(15162): (20) statement aborts at 6: [INSERT INTO agency(id,timezone,phone,fare_url,lang,url,name) VALUES (?,?,?,?,?,?,?)] datatype mismatch
我知道'?'可以用於選擇,但林不知道這些問號是從哪裏來的。
在我的MainActivity,我用這條線來試圖插入
db.addAgency(new Agency("905-527-4441", null,"http://www.hamilton.ca/hsr",
"HSR","Hamilton Street Railway","America/Toronto","en"));
上面一行調用下面的方法。
public void addAgency(Agency agency){
Log.e("addAgency", agency.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_AGENCY_PHONE, agency.phone);
values.put(KEY_AGENCY_FARE_URL, agency.fare_url);
values.put(KEY_AGENCY_URL, agency.url);
values.put(KEY_AGENCY_ID, agency.id);
values.put(KEY_AGENCY_NAME, agency.name);
values.put(KEY_AGENCY_TIMEZONE, agency.timezone);
values.put(KEY_AGENCY_LANG, agency.lang);
// 3. insert
db.insert(TABLE_AGENCY, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
這是我的日誌輸出。第一行顯示代理對象及其屬性。
12-23 16:13:44.601: E/addAgency(15162): Agency [phone=905-527-4441, fare_url=null, url=http://www.hamilton.ca/hsr, id=HSR, name=Hamilton Street Railway, timezone= America/Toronto, lang=en]
12-23 16:13:44.611: E/SQLiteLog(15162): (20) statement aborts at 6: [INSERT INTO agency(id,timezone,phone,fare_url,lang,url,name) VALUES (?,?,?,?,?,?,?)] datatype mismatch
12-23 16:13:44.611: E/SQLiteDatabase(15162): Error inserting id=HSR timezone=America/Toronto phone=905-527-4441 fare_url=null lang=en url=http://www.hamilton.ca/hsr name=Hamilton Street Railway
12-23 16:13:44.611: E/SQLiteDatabase(15162): android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:972)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1603)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1473)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.example.myshuttle.MySQLiteHelper.addAgency(MySQLiteHelper.java:80)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.example.myshuttle.MainActivity.onCreate(MainActivity.java:39)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.Activity.performCreate(Activity.java:5451)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.access$900(ActivityThread.java:169)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.os.Handler.dispatchMessage(Handler.java:102)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.os.Looper.loop(Looper.java:136)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at android.app.ActivityThread.main(ActivityThread.java:5479)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-23 16:13:44.611: E/SQLiteDatabase(15162): at dalvik.system.NativeStart.main(Native Method)
12-23 16:13:44.981: E/Map Zoom(15162): 15.0
12-23 16:13:44.991: E/AndroidRuntime(15162): FATAL EXCEPTION: main
12-23 16:13:44.991: E/AndroidRuntime(15162): Process: com.example.myshuttle, PID: 15162
12-23 16:13:44.991: E/AndroidRuntime(15162): java.lang.NullPointerException
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.example.myshuttle.MainActivity$1.onCameraChange(MainActivity.java:135)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.GoogleMap$7.onCameraChange(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.internal.e$a.onTransact(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Binder.transact(Binder.java:361)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.android.gms.maps.internal.af.a(SourceFile:93)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.maps.api.android.lib6.gmm6.c.c.a(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.google.maps.api.android.lib6.gmm6.c.d.run(Unknown Source)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Handler.handleCallback(Handler.java:733)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Handler.dispatchMessage(Handler.java:95)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.os.Looper.loop(Looper.java:136)
12-23 16:13:44.991: E/AndroidRuntime(15162): at android.app.ActivityThread.main(ActivityThread.java:5479)
12-23 16:13:44.991: E/AndroidRuntime(15162): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 16:13:44.991: E/AndroidRuntime(15162): at java.lang.reflect.Method.invoke(Method.java:515)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-23 16:13:44.991: E/AndroidRuntime(15162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-23 16:13:44.991: E/AndroidRuntime(15162): at dalvik.system.NativeStart.main(Native Method)
有什麼建議嗎?