syntax error near "," while compiling select - android

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.

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);

Sqlite syntax error near "?"

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

Distinct CONTACT_ID from ContactsContract.Data

I need to make a query to ContactsContract.Data table and values in CONTACT_ID column would be different (distinct).
Code:
final Uri uri = ContactsContract.Data.CONTENT_URI;
final String[] projection = new String[] {//
ContactsContract.Data.CONTACT_ID, //
ContactsContract.Data._ID, //
ContactsContract.Data.DISPLAY_NAME,//
ContactsContract.Data.LOOKUP_KEY //
};
final StringBuilder selectionBuilder = new StringBuilder();
selectionBuilder.append(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID);
selectionBuilder.append("= ? AND ");
selectionBuilder.append(ContactsContract.Data.MIMETYPE);
selectionBuilder.append("= ? ");
final String selection = selectionBuilder.toString();
final String[] selectionArgs = new String[] {//
String.valueOf(groupId), //
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE //
};
return context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
First of all, I've tried to add "DISTINCT " to ContactsContract.Data.CONTACT_ID in projection. But there was an exception: java.lang.IllegalArgumentException: Invalid column DISTINCT contact_id
Then, I write this way:
"'DISTINCT "+ContactsContract.Data.CONTACT_ID+"'".
java.lang.IllegalArgumentException: Invalid column 'DISTINCT contact_id'
Then, I add to selectionBuilder:
selectionBuilder.append(" GROUP BY ").append(ContactsContract.Data.CONTACT_ID);
Once again, an exception: android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? GROUP BY contact_id) ORDER BY display_name ASC
At last, I've append "group by" statement right after sortOrder, but:
android.database.sqlite.SQLiteException: near "GROUP": syntax error: , while compiling: SELECT contact_id, _id, display_name, lookup FROM view_data_restricted data WHERE (1) AND (data1= ? AND mimetype= ? ) ORDER BY display_name ASC GROUP BY contact_id
Is it ever possible to make query with distinct?
Maybe, I should append something to URI?
If you are targeting devices below ICS, you can use the GROUP_BY clause by adding a ) before the group by and a ( after:
selectionBuilder.append(") GROUP BY (")
As of ICS and above, the query interpretor is smarter and closes any unclosed parenthesis to prevent injection.
However, I don't see why you need distinct contact_ids here. A contact should probably have only one Data to make the association with one group, so you probably receive a different contact on each line.
Also, there may be something to do with http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#CONTENT_GROUP_URI it is not documented, but given its position, it may well be a direct access to Contacts belonging to a Group. You would use that Uri :
Uri uri = ContentUri.withAppendedId(ContactsContract.Contacts.CONTENT_GROUP_URI, groupId);
And then query it like the Contacts.CONTENT_URI

Error on cursor query on sqlite database

I am trying to get column info from a cursor, and getting a frustrating error. I want to select all columns containing $SEARCH. Here is the code:
Bundle b = getIntent().getExtras();
SEARCH = b.getString("searchtext");
Cursor c = mDBHelper.getReadableDatabase().query("table", null, "name="+ SEARCH, null, null, null, null);
For some reason, the cursor is throwing a runtime exception. here is the error:
12-30 03:55:00.357: E/AndroidRuntime(1302): Caused by: android.database.sqlite.SQLiteException: near "CAP": syntax error: , while compiling: SELECT * FROM kroger WHERE name=john
Not sure why this is happening, there's probably a very simple error in my code, but I'm not sure what it is. THanks for your help!
"name="+SEARCH will become name=CAP, which isn't a valid SQL expression. The naïve solution is to wrap the term in single-quotes:
"name='" + SEARCH + "'"
But this is subject to SQL injection attacks. Use the argument-passing facilities to pass the search term in an injection-free manner:
query("table", null, "name=?", new String[] {SEARCH}, null, null, null);

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.

Categories

Resources