我正在尝试将字典链接到我的应用程序。 由于某种原因,我没有得到查询结果,因为游标是空的。 这是我的字典助手类:
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;
}
这是我在主类中实现它的方式:
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());
}
}
游标获得的错误:请求索引 0,大小为 0