android - SQLite Android rawQuery 仅返回准确值
问题描述
我有一个用于一些食品和饮料的 SQLite 数据库,以及它们用于多语言应用程序的卡路里值。我想在 android 应用程序中显示它们,但除非我写出确切的值,否则我的查询不会返回值。
搜索牛奶时的数据库示例部分:
public class SearchFragment extends Fragment {
Button searchbutton;
EditText editText;
View v;
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_search, container, false);
addListenerOnButton(v);
return v;
}
private void addListenerOnButton(View v) {
searchbutton = v.findViewById(R.id.searchfragment_button);
editText = v.findViewById(R.id.searchfragment_edittext);
searchbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String keystring = editText.getText().toString();
new FoodQuery().execute(keystring);
}
});
}
private class FoodQuery extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String [] keys) {
String name_col;
DietAppDatabaseHelper daDBHelper;
SQLiteDatabase daDB;
daDBHelper = new DietAppDatabaseHelper(getContext());
try {
daDB = daDBHelper.getReadableDatabase();
if (Locale.getDefault().getLanguage() == "tr")
name_col = "name_tr";
else
name_col = "name_eng";
String query = "SELECT * FROM food_data WHERE "
+ name_col + " = ?" ;
cursor = daDB.rawQuery(query, new String[]{"%" + keys[0] + "%"});
int i = cursor.getCount();
daDB.close();
} catch (SQLiteException e){
Log.d("SearchFragment", "doInBackground: " + e);
}
return null;
}
}
}
当我调试“int i = cursor.getCount();”时 行,上面的代码不会向光标返回任何值。因此我总是出错。但不知何故,当我将 rawQuery 的 selectionArgs 部分从 "%" + keys[0] + "%" 更改为只有 keys[0] 并输入任何行的直接值(例如:Milk (Whole))区分大小写时,我确实得到了1 导致光标。这是数据库助手类
public class DietAppDatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "food_data.db";
private static String DB_PATH = "";
private static final int DB_VERSION = 1;
private SQLiteDatabase mDataBase;
private final Context mContext;
private boolean mNeedUpdate = false;
public DietAppDatabaseHelper(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/";
Log.d("Dietpath", DB_PATH);
this.mContext = context;
copyDataBase();
this.getReadableDatabase();
}
public void updateDataBase() throws IOException {
if (mNeedUpdate) {
File dbFile = new File(DB_PATH + DB_NAME);
if (dbFile.exists())
dbFile.delete();
copyDataBase();
mNeedUpdate = false;
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() {
if (!checkDataBase()) {
this.getReadableDatabase();
this.close();
try {
copyDBFile();
} catch (IOException mIOException) {
throw new Error("ErrorCopyingDataBase");
}
}
}
private void copyDBFile() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
//InputStream mInput = mContext.getResources().openRawResource(R.raw.info);
OutputStream mOutput = new FileOutputStream(DB_PATH + DB_NAME);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0)
mOutput.write(mBuffer, 0, mLength);
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException {
mDataBase = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
mNeedUpdate = true;
}
}
解决方案
=
运算符进行精确匹配。如果要使用通配符%
,请使用LIKE
运算符,如
String query = "SELECT * FROM food_data WHERE "
+ name_col + " LIKE ?" ;
推荐阅读
- ios - 如何修复 Swift 中的 textView.resignFirstResponder() 崩溃
- google-cloud-datastore - 如何在 Windows 上配置 GAE 数据存储模拟器?
- android - ByteArray 属性太大,无法在打开另一个 Activity 时在意图的 putExtra("obj", obj) 内发送
- excel - 包含时钟的工作簿会自动重新打开
- arduino - 什么是“调用'println(decode_results *)'没有匹配的函数”?
- django - Django 的 DB API 无法返回所有结果
- c++ - 如何存储http响应并发送到浏览器?
- sql - 当我搜索任何关键词时,我的数据重复了
- sql - ORDER 中无法识别的别名
- java - RichPresence 不再适用于 JDA