简体   繁体   English

“SELECT DISTINCT _id near “SELECT”中的语法错误

[英]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.

相关问题 SELECT附近的语法错误 - SYNTAX error near SELECT “SELECT”附近:语法错误(代码 1) - Near "SELECT": syntax error (code 1) 编译选择时,“,”附近的语法错误 - syntax error near “,” while compiling select android.database.sqlite.SQLiteException:靠近“ *”:语法错误:,正在编译:SELECT调用。_id - android.database.sqlite.SQLiteException: near “*”: syntax error: , while compiling: SELECT calls._id 无法解决SQLiteException:“ SELECT”附近:语法错误(代码1) - Can't resolve SQLiteException: near “SELECT”: syntax error (code 1) 出现错误:由以下原因引起:android.database.sqlite.SQLiteException:靠近“ TABLENAME”语法错误(代码1),编译SELECT查询 - getting error : Caused by: android.database.sqlite.SQLiteException: near “TABLENAME” syntax error (code 1) compiling a SELECT query 如何从android sqlite中的值的静态列表中选择,值附近的语法错误( - How to select from static list of values in android sqlite, syntax error near values( android.database.sqlite.SQLiteException:near“SELECT”:语法错误(代码1):,同时编译:SELECT * FROM Table_Name - android.database.sqlite.SQLiteException: near “SELECT ”: syntax error (code 1): , while compiling: SELECT * FROM Table_Name SQLite语法错误在“?”附近 - Sqlite syntax error near “?” “ @gmail”语法错误附近 - near “@gmail” Syntax error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM