我正在尝试将 sqlite 数据库中的短信导入短信收件箱。db 位于 SD 卡上。我收到错误请求错误。Sql 语句结构正确。我不确定是什么错误。请帮我找出错误在哪里。
MainActivity 片段:
private void importSms() {
int id = 10;
while(id < 10860) {
System.out.println("The id is " + id);
String address = null;
String body = null;
dbHelper.returnAddress(id);
dbHelper.returnBody(id);
ContentValues values = new ContentValues();
values.put("address", address);
values.put("body", body);
getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
Toast.makeText(getApplicationContext(),"Importing sms number" + id,Toast.LENGTH_SHORT).show();
id = id ++;
}
Toast.makeText(getApplicationContext(),"Finished import",Toast.LENGTH_LONG).show();
}
DataBaseHelper 片段:(如果需要,我可以发布整个课程,它不是那么大)
public String returnAddress(int id) {
System.out.println("returnAddress has been called");
openDataBase();
SQLiteDatabase db = getReadableDatabase();
String addressQuery = "SELECT address FROM sms WHERE _id = " + id;
Cursor ca = db.rawQuery(addressQuery, null);
ca.moveToFirst();
String address = ca.getString(1);
return address;
}
public String returnBody(int id) {
System.out.println("returnBody has been called");
openDataBase();
SQLiteDatabase db = getReadableDatabase();
String bodyQuery = "SELECT body FROM sms WHERE _id = " + id;
Cursor cb = db.rawQuery(bodyQuery, null);
cb.moveToFirst();
String body = cb.getString(2);
return body;
}
错误信息:
09-18 19:09:33.466: I/System.out(2729): The id is 10
09-18 19:09:33.466: I/System.out(2729): returnAddress has been called
09-18 19:09:33.526: E/CursorWindow(2729): Bad request for field slot 0,1. numRows = 1, numColumns = 1
09-18 19:09:33.526: D/AndroidRuntime(2729): Shutting down VM
09-18 19:09:33.526: W/dalvikvm(2729): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-18 19:09:33.526: E/AndroidRuntime(2729): FATAL EXCEPTION: main
09-18 19:09:33.526: E/AndroidRuntime(2729): java.lang.IllegalStateException: get field slot from row 0 col 1 failed
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.database.CursorWindow.getString_native(Native Method)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.database.CursorWindow.getString(CursorWindow.java:329)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.mobile.sms.testing.DataBaseHelper.returnAddress(DataBaseHelper.java:64)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.mobile.sms.testing.MainActivity.importSms(MainActivity.java:53)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.mobile.sms.testing.MainActivity.access$0(MainActivity.java:47)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.mobile.sms.testing.MainActivity$1.onClick(MainActivity.java:41)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.view.View.performClick(View.java:2485)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.view.View$PerformClick.run(View.java:9080)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.os.Handler.handleCallback(Handler.java:587)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.os.Looper.loop(Looper.java:130)
09-18 19:09:33.526: E/AndroidRuntime(2729): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-18 19:09:33.526: E/AndroidRuntime(2729): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 19:09:33.526: E/AndroidRuntime(2729): at java.lang.reflect.Method.invoke(Method.java:507)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-18 19:09:33.526: E/AndroidRuntime(2729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-18 19:09:33.526: E/AndroidRuntime(2729): at dalvik.system.NativeStart.main(Native Method)
09-18 19:09:35.676: I/Process(2729): Sending signal. PID: 2729 SIG: 9