我正在尝试将SELECT查询保留在Android的光标中
我使用此查询:
Cursor cursor = myDb.rawQuery("SELECT * FROM tbl_main_words WHERE word_id BETWEEN 1 AND 500 ORDER BY word_eng",null)
和cursor.getCount()返回0!
与(<)查询相同!
而不应该是:(
因为我的myDb表中有15000行,word_id从1到15000
注意:问题不是来自数据库,因为我可以从某些查询中返回真实值,例如:
SELECT * FROM tbl_main_words WHERE word_id > 0 ORDER BY word_eng
重要提示:在某些SQLite软件(例如SQLite Expert)中,这些查询的执行没有问题!
答案 0 :(得分:0)
我不认为您的问题与查询有关,使用:-
可以正常工作public class SO51108624DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "worddb";
public static final int DBVERSION = 1;
public static final String TB_MAIN_WORDS = "tbl_main_words";
public static final String COL_WORDID = "word_id";
public static final String COL_WORDENG = "word_eng";
SQLiteDatabase mDB;
public SO51108624DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " + TB_MAIN_WORDS + "(" +
COL_WORDID + " INTEGER PRIMARY KEY," +
COL_WORDENG + " TEXT" +
")";
db.execSQL(crtsql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long addRow(String word) {
ContentValues cv = new ContentValues();
cv.put(COL_WORDENG,word);
return mDB.insert(TB_MAIN_WORDS,null,cv);
}
public void populateMainWordsTable() {
mDB.delete(TB_MAIN_WORDS,null,null);
for (int i=0; i < 1000; i++) {
addRow("test");
}
}
public int getWordCountBetweenIDs() {
String sql = "SELECT * FROM tbl_main_words WHERE word_id BETWEEN 1 AND 500 ORDER BY word_eng";
Cursor csr = mDB.rawQuery(sql,null);
int rv = csr.getCount();
csr.close();
return rv;
}
}
以及:-
SO51108624DBHelper mDBHlpr = new SO51108624DBHelper(this);
mDBHlpr.populateMainWordsTable();
int rowcount = mDBHlpr.getWordCountBetweenIDs();
Log.d("ROWCOUNT", "Number of rows returned was " + String.valueOf(rowcount));
产生:-
06-29 22:29:39.193 1452-1452/soanswers.soanswers D/ROWCOUNT: Number of rows returned was 500
因此,当您运行此表时,表本身很可能为空。假设您没有遗漏任何错误,例如找不到列/表。
您可以尝试使用:-
Log.d ("ROWCOUNT","Number of rows in tbl_main_words is " + String.valueOf(DatabaseUtils.queryNumEntries(myDb,"tbl_main_words")));
Cursor cursor = myDb.rawQuery("SELECT * FROM tbl_main_words WHERE word_id BETWEEN 1 AND 500 ORDER BY word_eng",null)
,然后检查日志,其内容应类似于:-
06-29 22:36:54.232 1529-1529 / soanswers.soanswers D / ROWCOUNT: tbl_main_words中的行是1000