Sqlite syntax error near "?" - android

Simple enough; my code looks like this:
int idhash = getIdHash();
String[] args = {"runways", ""+idhash};
cursor = sqlite.rawQuery("SELECT * FROM ? WHERE idhash = ?", args);
And I see this line in logcat:
E/SQLiteLog( 9570): (1) near "?": syntax error
Where did I go wrong?

You can use ? only for binding literals. You cannot use it for binding identifiers such as table names. Identifiers must be in the SQL itself.

Maybe you can try
Cursor cursor = sqlite.rawQuery("SELECT * FROM runways WHERE idhash = ?", new String[]{idhash.toString()});
and it

Related

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.
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:
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"). 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"
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.
Use rawQuery(), not query().
You are trying to specify the entire SQL statement, which is what rawQuery() is for. query() assembles the SQL statement from pieces, and your one piece (where) is not just the WHERE clause.
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);

syntax error near "," while compiling select

Anyone con find the error in these query?
String[] column = {"titolo","regista","cast","descrizione"};
String selection = "_id_film = ? ";
String[] selection_args = {Integer.toString(selectedfilm+1)};
Cursor cursor = db.query("T_FILM", column, selection,selection_args, null, null, null);
logcat says:
android.database.sqlite.SQLiteException: syntax error near"," (code 1) :, while compling SELECT titolo,regista,cast,descrizione FROM T_FILM WHERE _id_film=?
"Cast" is a reserved operation in SQLLite. So it's trying to interpret the column name "cast" as an operation. Your easiest answer is to change the column name to something else.

How can pass two parameters to rawQuery in android? [duplicate]

PREF_NAME, PREF_STATUS - text
It gives me this error:
07-02 14:08:07.457: E/AndroidRuntime(17295): android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: SELECT ? FROM ? WHERE ? = ?
The code I'm using is:
Cursor c = db.rawQuery("SELECT ? FROM ? WHERE ? = ?", new String[]{ PREF_STATUS,
PREF_TABLE, PREF_NAME, assigned_pref_name });
According with the documentation, you only can put "?" on where clause:
public Cursor rawQuery (String sql, String[] selectionArgs)
Parameters
sql the SQL query. The SQL string must not be ; terminated
selectionArgs You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.
After the SELECT keyword you have a ? instead of something like *.
I guess you wanted to pass assigned_pref_name as a string and not variable name so :
Cursor c = db.rawQuery(
"SELECT ? FROM ? WHERE ? = ?",
new String[]{
PREF_STATUS,
PREF_TABLE,
PREF_NAME,
"assigned_pref_name" //as a string
});

Getting a SQLiteException and I'm not sure what's causing it

My Android app is crashing with this error:
E/AndroidRuntime( 315): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3470a0
...
E/AndroidRuntime( 315): at lee.medical.icu.dataentry.db.PatientInfoDbHelper.getTests(PatientInfoDbHelper.java:163)
It's stumbling on this bit of code:
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {C_LOCATION, C_TESTS};
String selection = "? = '?' and ? = '?'";
String[] selectionArgs = {C_LAST, lastName, C_FIRST, firstName};
Cursor cursor = db.query(TABLE, columns, selection, selectionArgs,
null, null, null); // line 163
What could this error mean?
(A couple of notes: C_LOCATION, C_TESTS, C_LAST, and C_FIRST are columns in my database. lastName and firstName are method arguments that are self-explanatory.)
Edit: I had to play around with antlersoft's solution a bit to make it work. In case anyone else having the same problem stumbles upon this question, the solution in my case was to change the selection to:
String selection = C_LAST + " = ? and " + C_FIRST + " = ?"
Don't put your ? in single-quotes for string arguments; in single quotes the ? is taken as a literal string and not as an argument placeholder-- SQLite will know the type of the argument passed as ? and so you don't have to quote it.
The way you have written it there are four arguments and only two argument placeholders in your query and so the exception.

Where clause in SQLite not working in android :(

I'm getting an annoying error when trying to query some data in SQLite.
Here is my code:
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +" = "+compareToThis, null, null, null, null);
I'm just returning the cursor as a string.
The error is saying:
no such column: compareToThis: while compiling.....the statement
My question is: why is SQLite setting the compareToThis attribute as a column when it's just a value?
How can I fix this?
Thanks in advance.
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +" = ?", new String[]{compareToThis}, null, null, null);
The selection must include placeholder for parameter, and the next argument should be the array of parameters.
The solution by Vladimir works, however if you are like me and wonder why your approach did not work initially when it should have, here is why:
It is because it expects an integer unless you used (single or double) quotation marks to indicate that it is a string.
For example, in MySql this would return no results:
SELECT * FROM clients WHERE firstName = Bob; -- This will not work.
However when you surround it with quotations, it will return a result because it identifies Bob as a String literal.
Select * FROM clients WHERE firstName = 'Bob'; -- Single quotes work.
Select * FROM clients WHERE firstName = "Bob"; -- Double quotes as well.
Therefore for it to work, you would have to surround your compareToString with single quotes, as Muhhammad mentioned within the comments.
Cursor cursor= db.query(TABLE_IMAGES, new String[]{"_id"}, "name" +'" = "+compareToThis+"'", null, null, null, null);

Categories

Resources