Android中的游标错误告诉我列不存在

前端之家收集整理的这篇文章主要介绍了Android中的游标错误告诉我列不存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序需要返回包含一堆引号的游标,具体取决于所选的作者.现在,我正在编写应用程序的硬编码,因此它只返回一个作者的引号.

错误发生在我的游标适配器参数中,设置“到”列,它告诉我此列“引用”不存在.

我已经尝试将to参数更改为KEY_QUOTE,甚至尝试更改实际数据库列的名称,但现在仍然很高兴.

我究竟做错了什么?

以下是此操作中使用的代码块.

>创建数据库表,引号
>填充此表
>查询报价
>打开数据库连接,创建游标(我收到实际错误,使用“from”参数)

1.

private static final String CREATE_QUOTE_TABLE = 

            "create table " + QUOTES_TABLE +
            " (_id integer primary key autoincrement," + 
            "auth_name text not null," +
            "myQuote text not null," +
            "category text not null);";

2.

public long populateQuotes(){

    ContentValues initialValues = new ContentValues();

    long[] rowIds = new long[authorName.length];

    // Add wilson quotes category: Anthropology
    for(int i = 0; i < 3; i++){
        initialValues.put(KEY_AUTHNAME,authorName[3]);
        initialValues.put(KEY_QUOTE,quoteRAW[i]);
        initialValues.put(KEY_CATEGORY,category[0]);
        rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE,null,initialValues);
    }
    // Add wilson quotes category: RAW
    for(int i = 3; i < 5; i++){
        initialValues.put(KEY_AUTHNAME,category[1]);
        rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE,initialValues);

    }
    // Add wilson quotes category: Philosophy
    for(int i = 5; i < 11; i++){
        initialValues.put(KEY_AUTHNAME,category[2]);
        rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE,initialValues);

    }
    // Add wilson quotes category: General Semantics
    for(int i = 11; i < 12; i++){
        initialValues.put(KEY_AUTHNAME,category[3]);
        rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE,initialValues);

    }
    // Add wilson quotes category: Humor
    for(int i = 11; i < 12; i++){
        initialValues.put(KEY_AUTHNAME,category[4]);
        rowIds[i] = qmDB.insertOrThrow(QUOTES_TABLE,initialValues);

    }

    return rowIds[0];
}

3.

public Cursor getQuotes(){

    /*
    return qmDB.query(QUOTES_TABLE,new String[]{
            KEY_QUOTE
            },KEY_AUTHNAME + "=" + "'Robert Anton Wilson'",null);*/



    //String who = authorName[position];
    return qmDB.rawQuery("SELECT _id as _id from Quotes WHERE auth_name = ?",new String[]{"'Robert Anton Wilson'"});

}

4.

// Create DB connection and open.
    dbm = new MyDBManager(this);
    dbm.open();

    // Call for the quotes to be queried,store results in cursor.
    myCursor = dbm.getQuotes();
    myCursor.moveToFirst();

    // Column quote does not exist?
    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.quoterow,myCursor,new String[]{"myQuote"},new int[]{R.id.quote});
    this.setListAdapter(mAdapter);

    myCursor.close();

为了进一步说明,我已经包含了错误日志:

05-01 18:38:13.876: ERROR/AndroidRuntime(1455): FATAL EXCEPTION: main
05-01 18:38:13.876: ERROR/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.QuoteMachine/com.QuoteMachine.Quote}: java.lang.IllegalArgumentException: column 'myQuote' does not exist
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.os.Looper.loop(Looper.java:144)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread.main(ActivityThread.java:4937)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at java.lang.reflect.Method.invoke(Method.java:521)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at dalvik.system.NativeStart.main(Native Method)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455): Caused by: java.lang.IllegalArgumentException: column 'myQuote' does not exist
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:312)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:87)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at com.QuoteMachine.Quote.onCreate(Quote.java:42)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
05-01 18:38:13.876: ERROR/AndroidRuntime(1455):     ... 11 more

解决方法

在CREATE_QUOTE_TABLE中,您使用“quote text not null”,并在您显示的logcat中:引起:java.lang.IllegalArgumentException:列’myQuote’不存在. KEY_QUOTE的价值是什么?它应该与表中的列名匹配.

测试此查询

return qmDB.rawQuery("SELECT _id,myQuote,auth_name,category FROM Quotes WHERE auth_name=?",new String[]{"'Robert Anton Wilson'"});

我相信你的问题不是查询试图检索不存在的列.我认为问题是你创建的光标没有你的适配器所需的所有信息(即,它是没有列的光标,而不是表格).

原文链接:https://www.f2er.com/android/308662.html

猜你在找的Android相关文章