Android error query SQLite - android

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+"%'";

Related

I have a problem with checking my email feild

I'm new to android and I created a database and I have an id as an integer primary key autoincrement
but I check on the email attribute when I register a new acc on the application, but I have a problem with the sql query that I can't put my hand on.
So I got this error today on this method that calls the name from the database using the email, and I hope you can tell me what's wrong!
E/MessageQueue-JNI: android.database.sqlite.SQLiteException: near ".": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT user_name FROM USER_TABLE WHERE user_email = androidx.appcompat.widget.AppCompatEditText{3c496d9 VFED..CL. ........ 0,0-656,127 #7f0a0068 app:id/email}
Here's the method:
public String getName(String mail) {
String nameReturn="Name";
SQLiteDatabase db=getWritableDatabase();
Cursor cursor=db.rawQuery("SELECT user_name FROM USER_TABLE WHERE user_email = "+mail, null);
if(cursor.moveToFirst()) nameReturn = cursor.getString(0);
return nameReturn.toString();
}
Looks like you are passing the EditText view to the query. Just pass the text itself
editTextView.getText()
Because your condition is string. So you must set into single quote
Cursor cursor=db.rawQuery("SELECT user_name FROM USER_TABLE WHERE user_email = '"+mail +"'", null);
Or you can use this type:
String[] args = {mail};
String sql = "SELECT user_name FROM USER_TABLE WHERE user_email =?";
Cursor cursor= db.rawQuery(sql, args);

How to properly format queries while inserting Commas to Separate Strings in SQLITE

I need to insert coordinates in SQLLIte Table in Android
My Query is as below .
Its gives a Syntax error due to cordinates which contain string by comma separated information .
INSERT INTO "tableNonOilFarmer" ("farmerId","FirstName","MiddleName","LastName","FatherName","DOB","CategoryId","MobileNo","LandlineNo","StateId","DistrictId","TalukaMandalId","ClusterId","VillageId","Pincode","RsNumber","Area","LocationId","BorewellAvailable","BorewellDepth","SoilType","Flood","CurrentCropId","CurrentCropStatusId","CurrentCropAge","CurrentCropRating","Awareness","Interested","ContactDate","NextCrop","OverallRating","CreatedOn","Latitude","Longitude","Coordinates","Converted","Status","StatusBy","StatusDate","Comments","UserId","Sync") VALUES (3,test,test,test,test,2017-12-27,6,64,58,1,01,02,01,04,96559,1,1,1,false,353,kdf,1,1,1,5555,3,true,true,2017-12-27,,5,2017-12-27,16.868542,81.306554,81.30682,16.8682,0 81.306309,16.86842,0 81.306607,16.869098,0 81.307039,16.868813,0 81.30682,16.8682,0,false,0,null,1,null,167,S)
You can Use PreparedStatement for inserting the Data with commas
SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteStatement stmt = db.compileStatement("INSERT INTO tableNonOilFarmer (FirstName,Adress) VALUES (?,?)");
stmt.bindString(1, "First");
stmt.bindString(2, "No 1, 1st street, MyCity");
stmt.execute();

How to copy data from one table to another?

I have an application with some searching, where I have a number of search terms.
I would like to do something like.
for( each_search_term ) {
INSERT INTO tmpTable SELECT dataId FROM SearchTable WHERE _id = ?;
}
I wasn't sure if the supported way is like the code below, or if there is some other better supported method.
void doInsert( SqliteDatabase db, long dataId ) {
db.execSQL( "INSERT INTO tmpTable SELECT dataId FROM SearchTable WHERE _id = ?;", new String [] { String.valueOf( dataId ) } );
}
The developer documents seem to imply that execSQL is not for INSERT operations.
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
You can try something like:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1;........................
Use this with
db.exeSQL(command);

How insert from table A to Table B in sqlite?

i want insert from table A to table B
My code:
String sql = "Insert Into B (emsunitcode,gpsacquisition_datetime,insert_datetime) "
+ "Select emsunitcode,gpsacquisition_datetime,update_datetime From A";
database.rawQuery(sql, null);
But it not work.
How insert from table A to Table B in sqlite?
Use execSQL() and not rawQuery().
rawQuery() just compiles the SQL but doesn't run it. execSQL() both compiles and runs.

Android strange syntax error query sqlite

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

Categories

Resources