Java Sqlite rawQuery返回空游标android:请求索引0,大小为0
我正在尝试将字典链接到我的应用程序。 由于某些原因,我没有得到查询结果,因为光标是空的。 这是我的字典助手类:Java Sqlite rawQuery返回空游标android:请求索引0,大小为0,java,android,sqlite,database-cursor,Java,Android,Sqlite,Database Cursor,我正在尝试将字典链接到我的应用程序。 由于某些原因,我没有得到查询结果,因为光标是空的。 这是我的字典助手类: public dbHandler(Context context) { super(context, DB_NAME, null, DB_VERSION); if (android.os.Build.VERSION.SDK_INT >= 17) DB_PATH = context.getApplicationInfo().dataDir + &
public dbHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
if (android.os.Build.VERSION.SDK_INT >= 17)
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
else
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
copyDataBase();
this.getReadableDatabase();
}
public List<DictionaryModel> search(String keyword,SQLiteDatabase myDatabase) {
List<DictionaryModel> contacts = null;
openDataBase();
//myDatabase = SQLiteDatabase.openOrCreateDatabase(dbPath+dbName+".db",null,null);
Cursor cursor = null;
try {
//SQLiteDatabase sqLiteDatabase = getReadableDatabase();
//Log.d("DICTIONARY_CHECK",dbPath+dbName+".db");
String query = "select * from entries where word like ?";
cursor = mDataBase.rawQuery(query,new String[] { "%" + keyword + "%" });
cursor.moveToFirst();
contacts = new ArrayList<>();
if(cursor.getCount()>0)
while (cursor.isAfterLast()){
DictionaryModel contact = new DictionaryModel();
contact.setWord(cursor.getString(0));
contact.setWordType(cursor.getString(1));
contact.setDefinition(cursor.getString(3));
contacts.add(contact);
cursor.moveToNext();
}
} catch (Exception e) {
Log.d("DICTIONARY_CHECK_GET",e.getLocalizedMessage()+" ");
} finally {
closeDatabase(myDatabase);
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return contacts;
}
公共数据库处理程序(上下文){
super(上下文,数据库名称,空,数据库版本);
如果(android.os.Build.VERSION.SDK_INT>=17)
DB_PATH=context.getApplicationInfo().dataDir+“/databases/”;
其他的
DB_PATH=“/data/data/”+context.getPackageName()+”/databases/”;
this.mContext=上下文;
copyDataBase();
这是.getReadableDatabase();
}
公共列表搜索(字符串关键字,SQLiteDatabase myDatabase){
列出联系人=空;
openDataBase();
//myDatabase=SQLiteDatabase.openOrCreateDatabase(dbPath+dbName+“.db”,null,null);
游标=空;
试一试{
//SQLiteDatabase SQLiteDatabase=getReadableDatabase();
//Log.d(“DICTIONARY_CHECK”,dbPath+dbName+”.db);
String query=“从word like所在的条目中选择*”;
cursor=mDataBase.rawQuery(查询,新字符串[]{“%”+关键字+“%”});
cursor.moveToFirst();
联系人=新的ArrayList();
if(cursor.getCount()>0)
while(cursor.isAfterLast()){
DictionaryModel contact=新建DictionaryModel();
contact.setWord(cursor.getString(0));
setWordType(cursor.getString(1));
contact.setDefinition(cursor.getString(3));
联系人。添加(联系人);
cursor.moveToNext();
}
}捕获(例外e){
d(“DICTIONARY\u CHECK\u GET”,e.getLocalizedMessage()+”);
}最后{
封闭数据库(myDatabase);
if(cursor!=null&!cursor.isClosed()){
cursor.close();
}
}
返回联系人;
}
这是我在我的主类中实现它的方式:
mDBHelper = new dbHandler(this);
try {
mDBHelper.updateDataBase();
} catch (IOException mIOException) {
throw new Error("UnableToUpdateDatabase");
}
try {
mDb = mDBHelper.getWritableDatabase();
} catch (SQLException mSQLException) {
throw mSQLException;
}
List<DictionaryModel> list = mDBHelper.search("absent",mDb);
for(DictionaryModel model : list){
if(model!=null){
Log.d("DICTIONARY_CHECK", model.getWord()+" "+model.getDefinition());
}
}
mDBHelper=newdbhandler(此);
试一试{
mDBHelper.updateDataBase();
}捕获(IOException-mIOException){
抛出新错误(“无法更新数据库”);
}
试一试{
mDb=mDBHelper.getWritableDatabase();
}catch(SQLException-msqleexception){
抛出mSQLException;
}
List List=mDBHelper.search(“缺席”,mDb);
用于(字典模型:列表){
如果(型号!=null){
Log.d(“字典检查”,model.getWord()+“”+model.getDefinition());
}
}
游标获取错误:请求索引0,大小为0将
while(cursor.isAfterLast())
更改为while(!cursor.isAfterLast())
。我已经尝试过了,忘记添加了!这一次,但问题是由于if(cursor.getCount()>0)
ifcursor.getCount()>0
返回false
这意味着表中没有任何包含变量关键字值的字。从设备中提取数据库并检查表。我检查了设备中的数据库。数据库的大小为12kb,而原始数据库的大小为15mb。可能是什么问题?