[英]syntax error in “SELECT DISTINCT _id near ”SELECT"
If someone knows a better way to get a rowId from text in the row, please let me know.如果有人知道从行中的文本中获取 rowId 的更好方法,请告诉我。
I've been running around in circles with this and I know it's probably something simple, but I can't figure it out.我一直在绕圈子,我知道这可能很简单,但我无法弄清楚。 Hoping someone can tell me what I'm doing wrong.
希望有人能告诉我我做错了什么。 I'm getting an error running this SQLite code:
运行此 SQLite 代码时出现错误:
String where = "SELECT rowid, * FROM masterRecord WHERE masNameCol=" + name;
Cursor c = db.query(true, masterName, ALL_KEYS_MASTER, where, null, null, null, null, null);
The error points to the second line.错误指向第二行。
"name" is a string variable (in this case it's "Mary"). “name”是一个字符串变量(在本例中是“Mary”)。 The exact error I'm getting is:
我得到的确切错误是:
SQLiteLog: (1) near "SELECT": syntax error in "SELECT DISTINCT _id, masNameCol, masTotalTimeCol FROM masterRecord WHERE SELECT rowid, * FROM masterRecord WHERE masNameCol=Mary" SQLiteLog:(1)“SELECT”附近:“SELECT DISTINCT _id,masNameCol,masTotalTimeCol FROM masterRecord WHERE SELECT rowid,* FROM masterRecord WHERE masNameCol=Mary”中的语法错误
I've tried every syntax change I could find and think of, and it never changes the error.我已经尝试了我能找到和想到的所有语法更改,但它永远不会改变错误。 I'm just trying to get the rowId of the row so I can change a value in another column.
我只是想获取该行的 rowId,以便可以更改另一列中的值。
Use rawQuery()
, not query()
.使用
rawQuery()
,而不是query()
。
You are trying to specify the entire SQL statement, which is what rawQuery()
is for.您正在尝试指定整个 SQL 语句,这就是
rawQuery()
的用途。 query()
assembles the SQL statement from pieces, and your one piece ( where
) is not just the WHERE
clause. query()
从片段中组装 SQL 语句,并且您的片段( where
)不仅仅是WHERE
子句。
Use placeholders for queries:使用占位符进行查询:
where = "masNameCol = ?";
whereArgs = new String[] { name };
columns = new String[] { "rowId" , /* all other column names you are interested in */ };
Cursor c = db.query("mytable", columns, where, whereArgs, null, null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.