首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从现有的SQLite数据库中查询数据

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

Stack Overflow用户
提问于 2014-04-13 04:55:40
回答 2查看 1.4K关注 0票数 0

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

这里我的数据库结构是使用SQLite数据库浏览器创建的,在教程链接中有说明;

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

代码语言:javascript
复制
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;
}

我试着用activity类中的这些行在textview上显示first market的信息:

代码语言:javascript
复制
     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());

然而,我得到了logcat错误:

04-12 23:40:24.477: E/SQLiteLog(30698):(1)无此表: markets

我检查了数据/数据文件,没有我的db文件,我逐行遵循教程,但我不知道为什么我不能正确创建表或sqlite数据库?我尝试过.db,.sqlite3扩展..如果我错了,那么我的db文件扩展名应该是什么?请帮帮我..

(注:我的手机上有cyanogenmod 11.0 )

EN

回答 2

Stack Overflow用户

发布于 2014-04-13 05:23:08

我不确定是什么问题,但这段代码:

代码语言:javascript
复制
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.moveToFirst(),您已经指向了表中的第一行,但是您执行了额外的cursor.moveToNext()操作,它跳过第一行,转到第二行。

票数 0
EN

Stack Overflow用户

发布于 2014-11-10 21:20:50

你没有定义一个表。我假设它不在教程中。这就是这一行,它应该在DATABASE_NAME和DATABASE_VERSION之后,在用于标识列的行之前:

代码语言:javascript
复制
public static final String TABLE_NAME = "markets";

如果你用其他代码修复了它,请让我知道!我一直在研究somet

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23036018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档