问题描述
我使用GUI来创建一个在它1650记录的数据库。
I've used the GUI to create a DB which has 1650 records in it.
我试图查询这个数据库,但它总是返回什么。我试着写一个简单的 getrowcount()
方法,看看是否我得到任何东西,但它总是返回零。我必须在这里失去了一些东西很明显,如果有人可以帮助指出,这是怎么回事。
I'm trying to query this DB but it's always returning nothing. I've tried writing a simple getrowcount()
method to see if I'm getting anything at all, but it always returns zero. I must be missing something obvious here, if someone can help point out what's going on.
在我的主 app.java
:
db = new DbHandler(this);
String sIcao1 = "ROW COUNT = " + String.valueOf(db.getRowCount());
在我的 dbhandler.java
:
package com.jammo.mywidget4;
<snip - standard includes>
public class DbHandler extends SQLiteOpenHelper {
private static SQLiteDatabase db;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "airports";
private static final String TABLE_AIRPORTS = "airports";
public DbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db = this.getWritableDatabase();
}
int getRowCount() {
int nCount = -1;
//SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM airports", null);
nCount = cur.getCount();
if (cur != null) {
//cur.moveToFirst();
//nCount = cur.getInt(0);
//if (cur.getInt (0) == 0) {
//}
}
return nCount;
}
}
在GUI(SQLite的DB浏览器)我做了一个简单
In the GUI (SQLite DB Browser) I'm doing a simple
select * from airports
......,我找回完整的行数。当我调试Java,光标返回任何内容。
... and I'm getting back the full number of rows. When I debug the Java, cursor returns nothing.
此外,由GUI创建的数据块位于的myapp /资产/ airports.db
。
Also, the DB created by the GUI is located in myapp/assets/airports.db
.
任何想法?
推荐答案
我认为你需要包括 .db的
在DATABASE_NAME。
I think you need to include the .db
in the DATABASE_NAME.
尝试改变这一点:
private static final String DATABASE_NAME = "airports";
这样:
private static final String DATABASE_NAME = "airports.db";
编辑:
其实我觉得即使有这样的变化,它不会为你工作。 SQLiteOpenHelper期待您的数据库文件是 /data/data/your.package/databases /
里面的所以我认为你需要,如果你将其从资产复制到那里希望它与未修改SQLiteOpenHelper工作。
Actually I think even with this change it is not going to work for you. SQLiteOpenHelper is expecting your db file to be inside of /data/data/your.package/databases/
So I think you'll need to copy it from assets to there if you want it to work with an unmodified SQLiteOpenHelper.
请参阅这里了解如何过来复制:Android:访问资产文件夹.sqlite扩展SQLite数据库文件
See here to learn how to copy it over: Android: Accessing assets folder sqlite database file with .sqlite extension
这篇关于Android的sqlite的行数始终为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!