The difference between sqlite query and rawQuery

query

public Cursor query(boolean distinct, String table, String[] columns,
            String selection, String[] selectionArgs, String groupBy,
            String having, String orderBy, String limit, CancellationSignal cancellationSignal) 
             //10 parameters

//distinct: set to true, each row must have unique data. vice versa.
//Table: table name of the operation
//Columns: the returned column name. If it is set to null, all columns will be returned
//selection: which line is returned, which is equivalent to the WHERE keyword in the SQL statement. Passing null returns all rows of the given table.
//selectionArgs: used to replace the? , and the order corresponds to "in selection"? The order. Format is limited to String format.
//groupBy: used to set the grouping method of returned rows, equivalent to the GROUP BY keyword in SQL statements. Passing null means that the returned rows will not be grouped.
//HAVING: decide which line is put into the filter in Cursor. If row grouping is used, it is equivalent to the HAVING keyword in the SQL statement.
//          Passing null causes all lines to be included, provided the groupBy property is also set to null.
//orderBy: row arrangement, equivalent to the "ORDER BY" keyword in SQL statement
//LIMIT: set the number of rows returned by the query statement, equivalent to the "LIMIT" keyword in the SQL statement. Note that the format is String
//cancellationSignal: signal to cancel program operation

public Cursor query(boolean distinct, String table, String[] columns,
            String selection, String[] selectionArgs, String groupBy,
            String having, String orderBy, String limit) //9 parameters

public Cursor query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy, String limit)   //8 parameters  

public Cursor query(String table, String[] columns, String selection,
            String[] selectionArgs, String groupBy, String having,
            String orderBy)//7 parameters, common

Example:

Cursor cursor = db.query("user", new String[]{"name"}, "id=?",  
                    new String[]{"1"}, null, null, null);

rawQuery

Query directly using SQL statement: SQL statement, condition parameter

public Cursor rawQuery(String sql, String[] selectionArgs,
            CancellationSignal cancellationSignal)

public Cursor rawQuery(String sql, String[] selectionArgs)//Commonly used

//The question mark (placeholder) in the first parameter String will be replaced one by one by the following String [] array

//Writing one:
String sql = "select * from user where name=?"; 
Cursor cursor = db.rawQuery(sql, new String[]{"Xu Qiang"});  

//Writing two:
String sql = "select * from user where name=Xu Qiang"; 
Cursor cursor = db.rawQuery(sql, null);  

Same point:

The last call is rawQueryWithFactory(CursorFactory,String,String[],String,CancellationSignal).

Difference:

query() helps you spell SQL statements, while rawQuery() helps you spell SQL statements.

There is an advantage of query compared with rawQuery. The former is that when you write SQL statements in rawQuery,
It's possible that he will make mistakes when he misspells or misses a word. The latter is relatively less likely to make mistakes.

Keywords: SQL less

Added by ChrisDarl on Thu, 02 Apr 2020 09:37:46 +0300