从现有SQLite数据库查询数据

时间:2014-04-09 10:32:35

标签: java android database sqlite

我遵循了这个教程: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 使用SQLite数据库浏览器创建数据库,将db文件放在“assets”文件夹等中。

然后我将此方法添加到DataBaseHelper类的末尾;

  public ArrayList<market> getMarkets(String table) {

    ArrayList<market> markets = new ArrayList<market>();
    market mrkt = new market();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + table, null);

    if(cursor.moveToFirst()) {
        do {
            mrkt.market_id = cursor.getInt(cursor.getColumnIndex("marketid"));
            mrkt.market_name = cursor.getString(cursor.getColumnIndex("name"));
            mrkt.market_telno = cursor.getInt(cursor.getColumnIndex("telno"));
            mrkt.market_location = cursor.getString(cursor.getColumnIndex("location"));
            mrkt.market_hours = cursor.getString(cursor.getColumnIndex("hours"));
            markets.add(mrkt);
        }while(cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return markets;
}

并尝试使用主要活动类中的这些行在listview上显示市场:

 dbh = new DataBaseHelper(getApplicationContext());
 markets = new ArrayList<market>();
 marketListView = (ListView) findViewById(R.id.listViewMarkets);

 markets = dbh.getMarkets("markets");



 // Create The Adapter with passing ArrayList as 3rd parameter
 ArrayAdapter<market> arrayAdapter =      
 new ArrayAdapter<market>(this,android.R.layout.simple_list_item_1, markets);

 // Set The Adapter
 marketListView.setAdapter(arrayAdapter);

我的布局xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listViewMarkets"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:dividerHeight="0.1dp"
    android:divider="#0000CC"
    >
</ListView>

</LinearLayout>

然而,我得到了错误,“不幸的是,应用已经停止了”.. 我是android的新手,有人可以帮帮我吗? 感谢。

这里是logcat:

04-09 16:32:00.125: W/dalvikvm(17103): threadid=1: thread exiting with uncaught exception (group=0x41685c80)
04-09 16:32:00.130: E/AndroidRuntime(17103): FATAL EXCEPTION: main
04-09 16:32:00.130: E/AndroidRuntime(17103): Process: com.example.deneme, PID: 17103
04-09 16:32:00.130: E/AndroidRuntime(17103): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deneme/com.example.deneme.MainActivity}: android.database.sqlite.SQLiteException: no such table: markets (code 1): , while compiling: SELECT * FROM markets
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread.access$800(ActivityThread.java:145)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.os.Looper.loop(Looper.java:136)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread.main(ActivityThread.java:5081)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at java.lang.reflect.Method.invoke(Method.java:515)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at dalvik.system.NativeStart.main(Native Method)
04-09 16:32:00.130: E/AndroidRuntime(17103): Caused by: android.database.sqlite.SQLiteException: no such table: markets (code 1): , while compiling: SELECT * FROM markets
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at com.example.deneme.DataBaseHelper.getMarkets(DataBaseHelper.java:165)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at com.example.deneme.MainActivity.onCreate(MainActivity.java:25)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.Activity.performCreate(Activity.java:5231)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-09 16:32:00.130: E/AndroidRuntime(17103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
04-09 16:32:00.130: E/AndroidRuntime(17103):    ... 11 more

1 个答案:

答案 0 :(得分:0)

dbh = new DataBaseHelper(null);

null不是Context要求的有效SQLiteOpenHelper引用。替换为this以将活动用作上下文。这就是getDatabaseLocked()中NPE的原因。