2014-01-08 42 views
0

我創建了以下數據庫..柱存在,但沒有顯示

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的是showing--

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) 
+0

什麼是'KEY_PTABLE'? – laalto

+0

表名..即付款 – droidX

+0

這些字段是否定義爲常量? –

回答

0

如果您最近修改表結構,刪除舊的數據庫文件,以便onCreate()被再次調用。

1

PAY_IDpay_id不一樣

您必須使用正確的外殼。

+0

我也試過更換箱子。但錯誤仍然相同 – droidX

+0

相同的'列'pay_id'不存在'錯誤? – MillaresRoo

0

在這樣的首都寫PAY_ID

c.getColumnIndexOrThrow("PAY_ID"); 

而且改變你的查詢作爲

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

同時改變你的查詢爲'Cursor c = db.rawQuery(「select * from payments」,null);' – GrIsHu

+0

爲什麼不把它寫回答而不是評論? – Kitkat

+0

我在等待我的答案准備好更新。我的互聯網運行緩慢。 @Kitkat – GrIsHu

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; 
}