从sqlite数据库android编写数据

时间:2014-06-21 13:05:20

标签: android sqlite

我想在我的Android应用程序中读取sqlite数据库中的数据,但应用程序强行关闭。

public void getData(String[] results)
    {


        String[] columns = new String[]{ KEY_SNO,KEY_ITEM,KEY_PRICE,KEY_TIME };
        Cursor c = canteendb.query(DATABASE_CTABLE,columns,null,null,null,null,null);
        int iSno = c.getColumnIndex(KEY_SNO);
        int iItem = c.getColumnIndex(KEY_ITEM);
        int iPrice = c.getColumnIndex(KEY_PRICE);
        int iTime = c.getColumnIndex(KEY_TIME);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        {
        results[0]=c.getString(iSno);
            results[1]=c.getString(iItem);
                results[2]=c.getString(iPrice);
                results[3]=c.getString(iTime); 
        }

    }

这是logcat:

06-21 18:55:17.028: E/AndroidRuntime(9160): FATAL EXCEPTION: main
06-21 18:55:17.028: E/AndroidRuntime(9160): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expenditurelogs/com.example.expenditurelogs.CanteenLog}: java.lang.NullPointerException
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread.access$700(ActivityThread.java:150)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.os.Looper.loop(Looper.java:137)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread.main(ActivityThread.java:5283)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at java.lang.reflect.Method.invoke(Method.java:511)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at dalvik.system.NativeStart.main(Native Method)
06-21 18:55:17.028: E/AndroidRuntime(9160): Caused by: java.lang.NullPointerException
06-21 18:55:17.028: E/AndroidRuntime(9160):     at com.example.expenditurelogs.CTable.getData(CTable.java:78)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at com.example.expenditurelogs.CanteenLog.onCreate(CanteenLog.java:22)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.Activity.performCreate(Activity.java:5283)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-21 18:55:17.028: E/AndroidRuntime(9160):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
06-21 18:55:17.028: E/AndroidRuntime(9160):     ... 11 more

1 个答案:

答案 0 :(得分:0)

我认为您需要先打开数据库,然后使用光标c;

db.open();

Cursor c = canteendb.query(DATABASE_CTABLE,columns,null,null,null,null,null);