0

我正在尝试将 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
4

2 回答 2

1

看起来您正试图从不存在的列中获取字符串。

我认为这可能是您需要的:

String addressQuery = "SELECT _id, address FROM sms WHERE _id = ?";
Cursor ca = db.rawQuery(addressQuery, id);
ca.moveToFirst();
int addressColumn = ca.getColumnIndexOrThrow ("address");
String address = ca.getString(addressColumn);

此代码参数化您的 id 并让您知道您获得了正确的列。

虽然,IllegalStateException让我想知道数据库是否可读

于 2012-09-18T23:47:30.590 回答
1

查看您的查询,您只返回一列,并且游标中的列从 0 开始编号。因此 usingString address = ca.getString(1);本质上是要求它从(不存在的)第二列中提取数据......与您的相同体法。

更改这些代码,我相信您的问题将得到解决。

String address = ca.getString(0);
String body = cb.getString(0);

更好的是,按照 pjco 的建议进行操作并使用该getColumnIndexOrThrow方法,因为如果您在游标中插入或删除其他元素(因此可能会更改所需数据的列 ID),则不必在整个过程中更改列 ID你的应用程序的其余部分:

String address = ca.getString(ca.getColumnIndexOrThrow("address"));
String body = ca.getString(ca.getColumnIndexOrThrow("body"));
于 2012-09-19T07:03:15.627 回答