[英]Search SQLite database in Android
I'm having some trouble searching a SQLite database in my Android app. 我在Android应用中搜索SQLite数据库时遇到了一些麻烦。 I use the Search interface to pass the search variable (searchActivity.query) to this:
我使用Search接口将搜索变量(searchActivity.query)传递给:
//searchActivity.query obtained from:
//String query = searchActivity.getStringExtra(SearchManager.QUERY);
String searchString = "'" + searchActivity.query + "%'";
//sqlitedb is called via: private SQLiteDatabase sqlitedb;
Cursor cursor = sqlitedb.query(DATABASE_TABLE, new String[]
{"id", "FirstName","LastName"},
"FirstName LIKE " + searchString, null, null, null, null);
int index_CONTENT = cursor.getColumnIndex(FirstName);
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
result = result + cursor.getString(index_CONTENT) + "\n";
}
The problem is that using the "LIKE" statement outputs everything in the database - defeating the purpose of using the Search interface. 问题是使用“LIKE”语句输出数据库中的所有内容 - 无法使用搜索界面。 I'm not too sure where I'm going wrong.
我不太确定我哪里出错了。
I tried replacing LIKE with "=" except that displayed nothing, even when I was 100% sure the record existed. 我尝试用“=”替换LIKE,除非没有显示任何内容,即使我100%确定记录存在。
Any help would be greatly appreciated! 任何帮助将不胜感激!
Thanks in advance, 提前致谢,
Joe 乔
UPDATE : Sorry about the misleading title. 更新 :对这个误导性的标题感到抱歉。 I wasn't passing the search query properly.
我没有正确传递搜索查询。
Cursor c = sqLiteDatabase.query(SQLiteHelper.MYDATABASE_TABLE, null, null, null, null, null, SQLiteHelper.KEY_CONTENT1 +" ASC");
String sql = " SELECT * FROM Table WHERE xColumn LIKE '%"+textView1.getText().toString()+"%'";
cursor = db.rawQuery(sql, null);
you can use as above 你可以像上面一样使用
Try this, 尝试这个,
Cursor cursor = sqlitedb.query(DATABASE_TABLE, new String[]
{"id", "FirstName","LastName"},
"FirstName LIKE '"+searchActivity.query+"%'", null, null, null, null);
and let me know what happen.. 让我知道发生了什么..
You should bind the parameter. 你应该绑定参数。
query = "UPPER(" + COLUMNNAME + ") like ?";
String searchString = searchActivity.query + "%";
and then, 接着,
sqlitedb.query(DATABASE_TABLE, new String[]
{"id", "FirstName","LastName"},
query , new String[] {searchString},....);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.