【转】Android 中数据库查询方法 query() 中的 select
2010-09-08 01:37
507 查看
Android 中涉及数据库查询的地方一般都会有一个 query() 方法,而这些 query 中有大都(全部?)会有一个参数 selectionArgs,比如下面这个 android.database.sqlite.SQLiteDatabase.query():
view plaincopy to clipboardprint?
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
selection 参数很好理解,就是 SQL 语句中 WHERE 后面的部分,即过滤条件, 比如可以为 id=3 AND name='Kevin Yuan' 表示只返回满足 id 为 3 且 name 为 "Kevin Yuan" 的记录。
再实际项目中像上面那样简单的“静态”的 selection 并不多见,更多的情况下要在运行时动态生成这个字符串,比如
view plaincopy to clipboardprint?
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name='" + name + "'", // selection
//...... 更多参数省略
);
}
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name='" + name + "'", // selection
//...... 更多参数省略
);
}
在这种情况下就要考虑一个字符转义的问题,比如如果在上面代码中传进来的 name 参数的内容里面有单引号('),就会引发一个 "SQLiteException syntax error .... "。
手工处理转义的话,也不麻烦,就是 String.replace() 调用而已。但是 Android SDK 为我们准备了 selectionArgs 来专门处理这种问题:
view plaincopy to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
也就是说我们在 selection 中需要嵌入字符串的地方用 ? 代替,然后在 selectionArgs 中依次提供各个用于替换的值就可以了。在 query() 执行时会对 selectionArgs 中的字符串正确转义并替换到对应的 ? 处以构成完整的 selection 字符串。 有点像 String.format()。
不过需要注意的是 ? 并不是“万金油”,只能用在原本应该是字符串出现的地方。比如下面的用法是错误的:
view plaincopy to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"? = " + id + " AND name=?", // selection XXXX 错误!? 不能用来替换字段名
new String[]{"id", name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/uoyevoli/archive/2009/12/09/4970860.aspx
MSN空间完美搬家到新浪博客!
view plaincopy to clipboardprint?
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
selection 参数很好理解,就是 SQL 语句中 WHERE 后面的部分,即过滤条件, 比如可以为 id=3 AND name='Kevin Yuan' 表示只返回满足 id 为 3 且 name 为 "Kevin Yuan" 的记录。
再实际项目中像上面那样简单的“静态”的 selection 并不多见,更多的情况下要在运行时动态生成这个字符串,比如
view plaincopy to clipboardprint?
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name='" + name + "'", // selection
//...... 更多参数省略
);
}
public doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name='" + name + "'", // selection
//...... 更多参数省略
);
}
在这种情况下就要考虑一个字符转义的问题,比如如果在上面代码中传进来的 name 参数的内容里面有单引号('),就会引发一个 "SQLiteException syntax error .... "。
手工处理转义的话,也不麻烦,就是 String.replace() 调用而已。但是 Android SDK 为我们准备了 selectionArgs 来专门处理这种问题:
view plaincopy to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"id=" + id + " AND name=?", // selection
new String[] {name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
也就是说我们在 selection 中需要嵌入字符串的地方用 ? 代替,然后在 selectionArgs 中依次提供各个用于替换的值就可以了。在 query() 执行时会对 selectionArgs 中的字符串正确转义并替换到对应的 ? 处以构成完整的 selection 字符串。 有点像 String.format()。
不过需要注意的是 ? 并不是“万金油”,只能用在原本应该是字符串出现的地方。比如下面的用法是错误的:
view plaincopy to clipboardprint?
public void doQuery(long id, final String name) {
mDb.query("some_table", // table name
null, // columns
"? = " + id + " AND name=?", // selection XXXX 错误!? 不能用来替换字段名
new String[]{"id", name}, //selectionArgs
//...... 更多参数省略
);
// ...... 更多代码
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/uoyevoli/archive/2009/12/09/4970860.aspx
MSN空间完美搬家到新浪博客!
相关文章推荐
- Android 中数据库查询方法 query() 中的 select
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- Android 中数据库查询方法query()中的selectionArgs参数解析
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- Android 中数据库查询方法query()中的selectionArgs的用法
- Android 中数据库查询方法query()中的selectionArgs的用法
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- Android 中数据库查询方法 query() 中的参数
- Android 数据存储——数据查询query方法参数解析
- android中数据库查询优化方法
- YII2-数据库yii\db\Query 查询方法
- 自己的笔记___摘自别人(Android数据库中查找一条数据 query方法详解)
- Android 数据库查询query函数参数解析
- MySQL大数据量分页查询方法及其优化 ---方法1: 直接使用数据库提供的SQL语句 ---语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N ---适
- 你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?
- android中adb shell常用命令及查询数据库的方法
- php mysqli query 查询数据库后读取内容的方法
- 在oracle 数据库查询的select 查询字段中关联其他表的方法