博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLIteDatabase.query method
阅读量:4207 次
发布时间:2019-05-26

本文共 3205 字,大约阅读时间需要 10 分钟。

7
2

I am using the query method of SQLitDatabase. I am little bit confused about the parameters using in "query" method.

Cursor cursor = sqLiteDatabase.query(tableName, tableColumns, whereClause, whereArgs, groupBy, having, orderBy);

tableColumns - columns parameter is constructed as follows.

String[] columns = new String[]{KEY_ID, KEY_CONTENT};

If we need to get all the fields, how should the column parameter to be constructed. Do we need to include all the Field Names in String array?

whereClause - Confused about the construction of this parameter.

whereArgs - Confused about the construction of this parameter.

Can anybody help me

thanks in advance.

|
 
Try a simple approach like  –   
 
I know that method. But I am trying to learn how to implement query method instead of rawQuery. –   

4 Answers

15
accepted

tableColumns

  • null for all columns as in SELECT * FROM ...
  • new String[] { "column1", "column2", ... } for specific columns as in SELECT column1, column2 FROM ... - you can also put complex expressions here:
    new String[] { "(SELECT max(column1) FROM table1) AS max" } would give you a column named max holding the max value of column1

whereClause

  • the part you put after WHERE without that keyword, e.g. "column1 > 5"
  • should include ? for things that are dynamic, e.g. "column1=?" -> see whereArgs

whereArgs

  • specify the content that fills each ? in whereClause in the order they appear

the others

  • just like whereClause the statement after the keyword or null if you don't use it.

Example

String[] tableColumns = new String[] {
"column1", "(SELECT max(column1) FROM table2) AS max"};String whereClause = "column1 = ? OR column1 = ?";String[] whereArgs = new String[] {
"value1", "value2"};String orderBy = "column1";Cursor c = sqLiteDatabase.query("table1", tableColumns, whereClause, whereArgs, null, null, orderBy);// since we have a named column we can doint idx = c.getColumnIndex("max");

is equivalent to the following raw query

String queryString =    "SELECT column1, (SELECT max(column1) FROM table1) AS max FROM table1 " +    "WHERE column1 = ? OR column1 = ? ORDER BY column1";sqLiteDatabase.rawQuery(queryString, whereArgs);

By using the Where/Bind -Args version you get automatically escaped values and you don't have to worry if input-data contains '.

Unsafe: String whereClause = "column1='" + value + "'";

Safe: String whereClause = "column1=?";

because if value contains a ' your statement either breaks and you get exceptions or does unintended things, for example value = "XYZ'; DROP TABLE table1;--" might even drop your table since the statement would become two statements and a comment:

SELECT * FROM table1 where column1='XYZ'; DROP TABLE table1;--'

using the args version XYZ'; DROP TABLE table1;-- would be escaped to 'XYZ''; DROP TABLE table1;--' and would only be treated as a value. Even if the ' is not intended to do bad things it is still quite common that people have it in their names or use it in texts, filenames, passwords etc. So always use the args version. (It is okay to build int and other primitives directly into whereClausethough)

|

转载地址:http://yhlli.baihongyu.com/

你可能感兴趣的文章
sqlserver语句总结
查看>>
informatica
查看>>
sqlserver 文章
查看>>
test
查看>>
SQLSERVER排查CPU占用高的情况
查看>>
Informatica中实现count(distinct)
查看>>
Informatica Powercenter调优
查看>>
增量聚集
查看>>
INCREMENTAL AGGREGATION IN INFORMATICA
查看>>
INFORMATICA1
查看>>
INFORMATICA2
查看>>
GENERATE DATE / TIME DIMENSION IN INFORMATICA
查看>>
日期维表数据生成方法(Oracle方式)
查看>>
2013年中国城市及省份GDP排名
查看>>
2013年欧洲穆斯林人口达5638万(转载)
查看>>
中国将在2014年成为十万亿级的超级大国
查看>>
月球可能曾是火星卫星
查看>>
华为u8818如何刷机
查看>>
少不读水浒——揭秘水浒传
查看>>
华为——让华盛顿感到恐慌的中国公司(转载)
查看>>