简体   繁体   English

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

[英]Query Data from Existing SQLite Database Android

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”文件夹等中。 。

Here my db structure created using SQLite Database Browser with instructions in tutorial link; 在这里,我的数据库结构是使用SQLite数据库浏览器创建的,并具有教程链接中的说明; 数据库结构

Then i added this method to end of DataBaseHelper class; 然后,我将此方法添加到DataBaseHelper类的末尾;

public ArrayList<market> getMarkets() {

    String table = "markets";
    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()) {
        cursor.moveToNext();
        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 i try to show first market's informations on textview with these lines in activity class: 我尝试在活动类中使用以下几行在textview上显示第一市场的信息:

     myDbHelper = new DataBaseHelper(this);
     markets = new ArrayList<market>();

     myDbHelper.createDataBase();
     myDbHelper.openDataBase();
     markets = myDbHelper.getMarkets();

     id.setText(markets.get(0).getMarket_id());
     name.setText(markets.get(0).getMarket_name());
     telno.setText(markets.get(0).getMarket_telno());
     loc.setText(markets.get(0).getMarket_location());
     hours.setText(markets.get(0).getMarket_hours());

However i got logcat error: 但是我得到logcat错误:

04-12 23:40:24.477: E/SQLiteLog(30698): (1) no such table: markets 04-12 23:40:24.477:E / SQLiteLog(30698):(1)没有这样的表格:市场

i checked data/data file and there is not my db file i followed tutorial line by line but i dont know why i can not create table or sqlite database properly ? 我检查了数据/数据文件,但没有我的数据库文件,我逐行按照教程进行操作,但我不知道为什么我无法正确创建表或sqlite数据库? i tried .db, .sqlite3 extensions.. if i'm wrong then what should be my db file extension ? 我试过.db,.sqlite3扩展名..如果我错了,那我的db文件扩展名应该是什么? please help.. 请帮忙..

(note: i have cyanogenmod 11.0 on my phone) (注意:我的手机上有cyanogenmod 11.0)

I am not sure what is the problem but this code: 我不确定是什么问题,但是这段代码:

if(cursor.moveToFirst()) {
    cursor.moveToNext();
    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());
}

skips the first row of the query. 跳过查询的第一行。 With cursor.moveToFirst() you are already pointing to the first row in the table, but you do additional cursor.moveToNext() which skips the first row, and goes to the second. 使用cursor.moveToFirst()您已经指向表中的第一行,但是您执行了另外的cursor.moveToNext() ,它跳过了第一行,而转到了第二行。

You didn't define a table. 您没有定义表格。 I'm assuming as it wasn't in the tutorial. 我假设它不是本教程中的内容。 This is the line and it should be after the DATABASE_NAME and DATABASE_VERSION, and before the lines to identify the columns: 这是一行,它应该在DATABASE_NAME和DATABASE_VERSION之后,并在用于标识列的行之前:

public static final String TABLE_NAME = "markets";

If you fixed it with other code, please let me know! 如果您用其他代码修复它,请告诉我! I've been working on somet 我一直在做某事

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

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