列存在但未显示

时间:2014-01-08 11:10:27

标签: android sqlite

我创建了以下数据库..

public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("create table ITEMS(ROW_ID integer primary key autoincrement,ITEM_DESCRIPTION text,ITEM_AMOUNT integer," +
            "time DATETIME DEFAULT CURRENT_DATE);");
    db.execSQL("create table payments(PAY_ID integer primary key autoincrement,PAY_DESC text,PAY_AMT integer,date DATETIME DEFAULT CURRENT_DATE);");
}

我正在使用以下功能来检索付款表条目

public Cursor getPayments(){
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor c=db.rawQuery("select * from " + KEY_PTABLE, null);
    for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
        c.getColumnIndexOrThrow("pay_id");
    }
    return c;
}

并且logcat正在显示 -

AndroidRuntime(30868): FATAL EXCEPTION: main
**AndroidRuntime(30868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expensesdatabase/com.example.expensesdatabase.PayView}: java.lang.IllegalArgumentException: column 'pay_id' does not exist**
AndroidRuntime(30868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
AndroidRuntime(30868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
AndroidRuntime(30868): at android.app.ActivityThread.access$600(ActivityThread.java:123)
AndroidRuntime(30868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
AndroidRuntime(30868): at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime(30868): at android.os.Looper.loop(Looper.java:137)
AndroidRuntime(30868): at android.app.ActivityThread.main(ActivityThread.java:4424)
AndroidRuntime(30868): at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime(30868): at java.lang.reflect.Method.invoke(Method.java:511)
AndroidRuntime(30868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
AndroidRuntime(30868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
AndroidRuntime(30868): at dalvik.system.NativeStart.main(Native Method)
**AndroidRuntime(30868): Caused by: java.lang.IllegalArgumentException: column 'pay_id' does not exist**
AndroidRuntime(30868): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
AndroidRuntime(30868): at com.example.expensesdatabase.MySQLiteHelper.getPayments(MySQLiteHelper.java:152)

4 个答案:

答案 0 :(得分:1)

PAY_IDpay_id

不同

您必须使用正确的外壳。

答案 1 :(得分:0)

像这样在大写中写PAY_ID

c.getColumnIndexOrThrow("PAY_ID");

同时将您的查询更改为

Cursor c=db.rawQuery("select * from payments", null);

答案 2 :(得分:0)

您的错误表明该列不存在。

试试这个:

public Cursor getPayments(){
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor c=db.query("payments", null, null, null, null, null, null);
    while(c.moveToNext())
    {
        // You can log the recieved data
        // Log.i("PAY_ID",c.getString(c.getString(c.getColumnIndex("PAY_ID"))));
    }
    return c;
}

答案 3 :(得分:0)

如果您最近修改了表架构,请删除旧的数据库文件,以便再次调用onCreate()