Android SQLite 查询的结果不一致
Inconsistent results on Android SQLite query
我在 Android 上的 SQL 查询结果中遇到了一些意外的不一致。
给定以下架构(非常简单):
public static final String TABLE_TOTD = "totd";
public static final String COLUMN_ID = "id";
public static final String COLUMN_TG = "tg";
public static final String COLUMN_EX = "ex";
private static final String DATABASE_NAME = "TOTD.db";
private static final String DATABASE_CREATE = "CREATE TABLE "
+ TABLE_TOTD + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_TG + " TEXT NOT NULL, "
+ COLUMN_EX + " TEXT"
+ ");";
数据库正在初始化并填充了一些示例文本:
SQLiteDatabase db = mdbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_TG, "This is a text (1)");
values.put(MySQLiteHelper.COLUMN_EX, "Example goes here");
db.insert(MySQLiteHelper.TABLE_TOTD, null, values);
[...]
我在使用 SQLiteDatabase.query() 查询时检索到不一致的结果
下面的代码通过 return 将 Cursor 与 table.
的第一行进行匹配来完成预期的工作
String[] projection = null;
String selection = "id = 1";
String[] selectionArgs = null;
Cursor cursor = db.query(
MySQLiteHelper.TABLE_TOTD,
projection,
selection,
selectionArgs,
null, null, null);
通过像这样更改 selection
和 selectionArgs
它的工作原理完全相同:
String selection = "id = ?";
String[] selectionArgs = {"1"};
但是现在,与预期不同的是,通过执行以下更改,returned 光标现在将为空:
String selection = "? = ?";
String[] selectionArgs = {"id", "1"};
实际上,尝试将 "1"
以外的任何内容推入参数将导致查询 return 一个空游标。
我试过以下方法:
String selection = "? = 1";
String[] selectionArgs = {"id"};
String selection = "id ? 1";
String[] selectionArgs = {"="};
String selection = "?";
String[] selectionArgs = {"id = 1"}; //This is where I couldn't take it anymore...
现在,我对关于此的任何解释持开放态度。我的代码有效,而且我知道我可以将所有参数放在 selection
字符串中,因为它们不包含任何禁止使用的字符。
我只想知道,为什么它不起作用,为什么开发者文档中没有提到这个明确的限制?
选择参数是 sqlite 查询的 WHERE
子句。当您使用 ? 执行查询时它将在 selectionArgs 字符串数组中查找此参数。但是,您只能将值指定为 ?而不是关键。几个有效的例子:
String selection = "id = ?";
String[] selectionArgs = new String[]{"1"};
类似于
String selection = "id = 1";
String[] selectionArgs = null;
然而你做不到
String selection = "? = ?";
您还可以指定多个 ?在选择中。
String selection = "id = ? AND carId = ?";
String[] selectionArgs = new String[]{"1", "2"};
来自文档:
You may include ?s in selection, which will be replaced by the values
from selectionArgs, in order that they appear in the selection. The
values will be bound as Strings.
我在 Android 上的 SQL 查询结果中遇到了一些意外的不一致。
给定以下架构(非常简单):
public static final String TABLE_TOTD = "totd";
public static final String COLUMN_ID = "id";
public static final String COLUMN_TG = "tg";
public static final String COLUMN_EX = "ex";
private static final String DATABASE_NAME = "TOTD.db";
private static final String DATABASE_CREATE = "CREATE TABLE "
+ TABLE_TOTD + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_TG + " TEXT NOT NULL, "
+ COLUMN_EX + " TEXT"
+ ");";
数据库正在初始化并填充了一些示例文本:
SQLiteDatabase db = mdbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_TG, "This is a text (1)");
values.put(MySQLiteHelper.COLUMN_EX, "Example goes here");
db.insert(MySQLiteHelper.TABLE_TOTD, null, values);
[...]
我在使用 SQLiteDatabase.query() 查询时检索到不一致的结果 下面的代码通过 return 将 Cursor 与 table.
的第一行进行匹配来完成预期的工作 String[] projection = null;
String selection = "id = 1";
String[] selectionArgs = null;
Cursor cursor = db.query(
MySQLiteHelper.TABLE_TOTD,
projection,
selection,
selectionArgs,
null, null, null);
通过像这样更改 selection
和 selectionArgs
它的工作原理完全相同:
String selection = "id = ?";
String[] selectionArgs = {"1"};
但是现在,与预期不同的是,通过执行以下更改,returned 光标现在将为空:
String selection = "? = ?";
String[] selectionArgs = {"id", "1"};
实际上,尝试将 "1"
以外的任何内容推入参数将导致查询 return 一个空游标。
我试过以下方法:
String selection = "? = 1";
String[] selectionArgs = {"id"};
String selection = "id ? 1";
String[] selectionArgs = {"="};
String selection = "?";
String[] selectionArgs = {"id = 1"}; //This is where I couldn't take it anymore...
现在,我对关于此的任何解释持开放态度。我的代码有效,而且我知道我可以将所有参数放在 selection
字符串中,因为它们不包含任何禁止使用的字符。
我只想知道,为什么它不起作用,为什么开发者文档中没有提到这个明确的限制?
选择参数是 sqlite 查询的 WHERE
子句。当您使用 ? 执行查询时它将在 selectionArgs 字符串数组中查找此参数。但是,您只能将值指定为 ?而不是关键。几个有效的例子:
String selection = "id = ?";
String[] selectionArgs = new String[]{"1"};
类似于
String selection = "id = 1";
String[] selectionArgs = null;
然而你做不到
String selection = "? = ?";
您还可以指定多个 ?在选择中。
String selection = "id = ? AND carId = ?";
String[] selectionArgs = new String[]{"1", "2"};
来自文档:
You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.