I'm doing a simple query that counts the records ... but I get syntax error here: COUNT (field1). Have you any idea why? thanks
String sql = "SELECT _id, field1, field2, field3 COUNT(field1)
FROM Appoggio GROUP BY field1 ORDER BY field1 ASC";
You are missing a comma after field3
Try using:
String sql = "SELECT _id, field1, field2, field3, COUNT(field1)
FROM Appoggio GROUP BY field1 ORDER BY field1 ASC";
Related
Hello I have a query like this:
String sql ="SELECT _id, field10, field11, SUM(field2) FROM Table WHERE field2>0 AND data LIKE '"+anno+"-%'";
If in the database, it is stored a string like this (pubblicita') I get syntax error like this:
Caused by: android.database.sqlite.SQLiteException: near "2014": syntax error (code 1): , while compiling:
SELECT _id, field10, field11, SUM(field2) FROM Table WHERE field2>0 AND field10 LIKE 'PUBBLICITA'' AND data LIKE '2014-%'
The problem lies in the apex, how can I solve this case?
UPDATE
Unfortunately, all the answers are wrong. The error is generated if the string corresponding with the apex. See the image
In SQL string literals, quotes must be escaped by doubling them.
However, you can avoid doing this manually by passing the string as a parameter:
cursor = db.rawQuery("SELECT ... data LIKE ? || '-%'",
new String[] { anno });
you need to escape quote character before using string in sql.
public static String escapeSQLQuotes(final String in) {
return in.replaceAll("'", "''");
}
and use:
String sql ="SELECT _id, field10, field11, SUM(field2) FROM Table WHERE field2>0 AND data LIKE '"+escapeSQLQuotes(anno)+"-%'";
Try that:
String sql ="SELECT _id, field10, field11, SUM(field2) FROM Table WHERE field2>0 AND data LIKE '"+anno+"%'";
"-%'" replace "%'"
Replace "-%" with "%"
String sql ="SELECT _id, field10, field11, SUM(field2) FROM Table WHERE field2>0 AND data* LIKE '"+anno+"%'";
what do you think my problem is.
I am querying a sqlite database.
This code doesn't give any result.
String sql = " select _id from MYTABLE where _id = ? ";
Cursor cur = mDb.rawQuery(sql, new String[] {"5653"}) ;
but if I am executing the query without parameters like this:
String sql = " select _id from MYTABLE where _id = 5653 ";
Cursor cur = mDb.rawQuery(sql, null) ;
One row is returned as expected.
Many thanks.
That happens because the values are bound as strings, and that column is (i am guessing) an int. so the where clause will end up being
where _id = "5653"
From rawQuery javadoc for 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.
Try this:
String sql = " select _id from MYTABLE where _id = '5653' ";
I am using rawquery() for perform inner join, but this is not giving me any error and does not working, if i use this query directly into sqlite browser than this is working but in application this query does not work, following is my code, sorry for bad English communication
public void deleteFifo() {
final String MY_QUERY1 = "Delete from Items where res_id in (select _id from Favourite where _id not in (select _id from Favourite order by date desc limit 50))";
final String MY_QUERY2 = "Delete from Favourite where _id not in (select _id from Favourite order by date desc limit 50)";
db.rawQuery(MY_QUERY1, null);
db.rawQuery(MY_QUERY2, null);
}
Try:
db.delete(TableName, whereCondition, null);
i.e. in your case
db.delete("Items", "res_id in (select _id from Favourite where
_id not in (select _id from Favourite order by date desc limit 50))", null);
and
db.delete("Favourite ","_id not in (select _id from Favourite
order by date desc limit 50)");
Hope it helps !!
I am new in android application development.
I want to do UNION of two query in a same cursor.
public Cursor getAll() {
return (getReadableDatabase().rawQuery(
"SELECT _id,x,y from XYZ where _id=" + id+" UNION SELECT _id,z from lmn where _id=" + id, null));
}
Use UNION in sqlite query to connect more than one query and execute that query as rawQuery in android
I am banging my head over this for several hours, I tested many ways and I know where the proble is, but I just can't figure out how to fix it, I need an external point of view here.
That's my query code:
public Cursor getAllExpensesUser(int user){
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ user +")";
return mDb.rawQuery(sql, null);
}
With this query it returns nothing.
When using the following query it returns my desired rows... obviously im using the id number directly.
`String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = 2)";`
I have tried also with arguments:
public Cursor getAllExpensesUser(int user){
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = ?)";
String[] arguments = {Integer.toString(user)};
return mDb.rawQuery(sql, arguments);
So bacically the problem is when I am using a variable.
Can anyone see where I am messing it up?
Thank you
It should be WHERE id_user = '" + user + "')";
SQL requires single quotes around data on the right of the equals.
try to replace
String sql = "SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ user +")";
with
String sql = new String("SELECT * FROM expense WHERE _id IN (SELECT id_expense FROM forwho WHERE id_user = "+ String.valueOf(user) +")");
EDIT: use String.valueOf instead of Integer.toString