简体   繁体   English

从现有的SQLite数据库查询数据

[英]Query Data from Existing SQLite Database

I followed this tutorial: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ Created db with SQLite Database Browser, put db file in "assets" folder etc.. 我遵循了本教程: http : //www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/使用SQLite数据库浏览器创建了db,将db文件放在“ assets”文件夹等中。 。

Then i add this method to end of DataBaseHelper class; 然后,我将此方法添加到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;
}

and try to show markets on listview with these lines in main activity class: 并尝试在主活动类中使用以下几行在列表视图上显示市场:

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

my layout xml file: 我的布局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>

However, i got error, "unfotunately application has stopped".. I'm new in android, anybody can help me ? 但是,我收到错误消息,“不幸的是,应用程序已停止”。.我是android新手,有人可以帮助我吗? Thanks. 谢谢。

Here logcat: 这里的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
dbh = new DataBaseHelper(null);

null is not a valid Context reference that SQLiteOpenHelper requires. null不是SQLiteOpenHelper要求的有效Context引用。 Replace with this to use the activity as a context. 替换this以将活动用作上下文。 This is the reason for the NPE in getDatabaseLocked() . 这就是在getDatabaseLocked()使用NPE的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM