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

然后我将这个方法添加到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;
}我试着用activity类中的这些行在textview上显示first market的信息:
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 )
发布于 2014-04-13 05:23:08
我不确定是什么问题,但这段代码:
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()操作,它跳过第一行,转到第二行。
发布于 2014-11-10 21:20:50
你没有定义一个表。我假设它不在教程中。这就是这一行,它应该在DATABASE_NAME和DATABASE_VERSION之后,在用于标识列的行之前:
public static final String TABLE_NAME = "markets";如果你用其他代码修复了它,请让我知道!我一直在研究somet
https://stackoverflow.com/questions/23036018
复制相似问题