如何在Android应用程序中执行SQLite查询?

时间:2021-09-12 20:40:44

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;

5 个解决方案

#1


108  

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);

#2


57  

Alternatively, db.rawQuery(sql, selectionArgs) exists.

或者,db.rawQuery(sql,selectionArgs)存在。

Cursor c = db.rawQuery(select, null);

#3


26  

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
        }

#4


9  

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 query
  • table:要查询的表的名称
  • columns: the column names that you want returned. Don't return data that you don't need.
  • columns:要返回的列名称。不要返回您不需要的数据。
  • selection: the row data that you want returned from the columns (This is the WHERE clause.)
  • selection:要从列返回的行数据(这是WHERE子句。)
  • selectionArgs: This is substituted for the ? in the selection String above.
  • selectionArgs:这取代了?在上面的选择字符串中。
  • groupBy and having: This groups duplicate data in a column with data having certain conditions. Any unneeded parameters can be set to null.
  • groupBy and having:这会将具有特定条件的数据的列中的重复数据分组。任何不需要的参数都可以设置为null。
  • orderBy: sort the data
  • orderBy:对数据进行排序
  • limit: limit the number of results to return
  • limit:限制返回的结果数

#5


1  

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);

#1


108  

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);

#2


57  

Alternatively, db.rawQuery(sql, selectionArgs) exists.

或者,db.rawQuery(sql,selectionArgs)存在。

Cursor c = db.rawQuery(select, null);

#3


26  

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
        }

#4


9  

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 query
  • table:要查询的表的名称
  • columns: the column names that you want returned. Don't return data that you don't need.
  • columns:要返回的列名称。不要返回您不需要的数据。
  • selection: the row data that you want returned from the columns (This is the WHERE clause.)
  • selection:要从列返回的行数据(这是WHERE子句。)
  • selectionArgs: This is substituted for the ? in the selection String above.
  • selectionArgs:这取代了?在上面的选择字符串中。
  • groupBy and having: This groups duplicate data in a column with data having certain conditions. Any unneeded parameters can be set to null.
  • groupBy and having:这会将具有特定条件的数据的列中的重复数据分组。任何不需要的参数都可以设置为null。
  • orderBy: sort the data
  • orderBy:对数据进行排序
  • limit: limit the number of results to return
  • limit:限制返回的结果数

#5


1  

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);