如何在 Android 应用程序中执行 SQLite 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1243199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform an SQLite query within an Android application?
提问by Dennie
I am trying to use this query upon my Android database, but it does not return any data. Am I missing something?
我正在尝试在我的 Android 数据库上使用此查询,但它不返回任何数据。我错过了什么吗?
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String select = "Select _id, title, title_raw from search Where(title_raw like " + "'%Smith%'" +
")";
Cursor cursor = db.query(TABLE_NAME, FROM,
select, null, null, null, null);
startManagingCursor(cursor);
return cursor;
回答by Prashast
This will return you the required cursor
这将返回您所需的游标
Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"},
"title_raw like " + "'%Smith%'", null, null, null, null);
回答by xiojason
Alternatively, db.rawQuery(sql, selectionArgs) exists.
或者, db.rawQuery(sql, selectionArgs) 存在。
Cursor c = db.rawQuery(select, null);
回答by Gautam Mandsorwale
This will also work if the pattern you want to match is a variable.
如果您要匹配的模式是变量,这也将起作用。
dbh = new DbHelper(this);
SQLiteDatabase db = dbh.getWritableDatabase();
Cursor c = db.query("TableName", new String[]{"ColumnName"}
, "ColumnName LIKE ?" ,new String[]{_data+"%"}, null, null, null);
while(c.moveToNext())
{
// your calculation goes here
}
回答by Suragch
I came here for a reminder of how to set up the query but the existing examples were hard to follow. Here is an example with more explanation.
我来这里是为了提醒如何设置查询,但现有示例很难遵循。这是一个带有更多解释的示例。
SQLiteDatabase db = helper.getReadableDatabase();
String table = "table2";
String[] columns = {"column1", "column3"};
String selection = "column3 =?";
String[] selectionArgs = {"apple"};
String groupBy = null;
String having = null;
String orderBy = "column3 DESC";
String limit = "10";
Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
Parameters
参数
table
: the name of the table you want to querycolumns
: the column names that you want returned. Don't return data that you don't need.selection
: the row data that you want returned from the columns (This is the WHERE clause.)selectionArgs
: This is substituted for the?
in theselection
String above.groupBy
andhaving
: This groups duplicate data in a column with data having certain conditions. Any unneeded parameters can be set to null.orderBy
: sort the datalimit
: limit the number of results to return
table
: 要查询的表名columns
: 要返回的列名。不要返回不需要的数据。selection
:您希望从列返回的行数据(这是 WHERE 子句。)selectionArgs
:这是代替?
在selection
上面的字符串。groupBy
和having
:这将具有特定条件的数据分组到列中的重复数据。任何不需要的参数都可以设置为空。orderBy
: 对数据进行排序limit
: 限制返回的结果数
回答by styling
Try this, this works for my code name is a String:
试试这个,这适用于我的代号是一个字符串:
cursor = rdb.query(true, TABLE_PROFILE, new String[] { ID,
REMOTEID, FIRSTNAME, LASTNAME, EMAIL, GENDER, AGE, DOB,
ROLEID, NATIONALID, URL, IMAGEURL },
LASTNAME + " like ?", new String[]{ name+"%" }, null, null, null, null);