我在将值插入数据库时遇到问题。当我尝试插入时,我收到了数据不匹配错误。我是Android / SQLite / ContentValues的新手。我一直在关注您可以找到Here的教程。
我正在尝试使用insert方法中内置的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();
}
这是我的日志输出。第一行显示了Agency对象及其属性。
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)
任何建议?
答案 0 :(得分:2)
您无法将字符串插入integer primary key
列。