updating sqlite database using a String not working - android

This works but i need to be able to change the value of the product_name
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='Hammer'";
myDatabase.execSQL(query);
This does not work and I can't see why.
String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='%"+test+ "%'";
myDatabase.execSQL(query);

Try this code
i think it will work
ContentValues data = new ContentValues();
data.put("Column Name", column_value);
i.e,
data.put("_quantity", 3);
myDatabase.update(TABLE_NAME ,data,"product_name = ?",new String[]{test});

To match a pattern you should use the LIKE statement:
String test = "Hammer";
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name LIKE '%"+test+ "%'";
myDatabase.execSQL(query);

You need to remove % symbols from your query. This symbols are used to match the strings values if you want to match anything like what he wants %mer% would match hammer
Try as below:
String query = "UPDATE UserTable SET _quantity=_quantity - 1 WHERE product_name ='"+test+ "'";

Related

Android SQLITE : Get Id value based on column value

I am building my first android app where I am trying to sync mysql data to sqlite in android. I have two tables in mysql and both gets synced properly into android sqlite. The first table is as follows:
id ProjectName
1 RA_Tesco
2 RA_Coors
3 RA_JWNT
The second table is as follows:
id pid Outlet Add1
1 1 Tesco XYZ
2 1 Tesco ABC
3 2 Coors NBC
The PID in second table references to id of first table. How can I subset the second table based on PID value derived from id of first table. I know it is pretty straight forward in php or mysql or even in Python or R. However, fetching the id based on string and referencing the same in the second table seems quite tricky in Android. My codes so far:
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
clickedId = getIntent().getExtras().get("clickedId").toString();
When I toast clickedId, I get the correct string, for example, RA_Tesco.
cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME1+" where pid = 1"+"", null);
The above code also renders the correct set of records from the sqlite table. I am struggling with integrating them both. I tried the following:
String pid;
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
clickedId = getIntent().getExtras().get("clickedId").toString();
pid = sqLiteDatabase.rawQuery( "select id from "+sqLiteHelper.TABLE_NAME+" where projectName = "+clickedId+"", null );
I am getting incompatible types error.
This is what worked for me:
clickedId = getIntent().getExtras().get("clickedId").toString();
cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME1+" where pid = (select id from "+SQLiteHelper.TABLE_NAME+ " where ProjectName = '"+clickedId+"'"+")", null);
I just followed the same MySQL principle of nesting queries. The above code roughly reads as follows:
select * from table2 where pid = (select id from table1 where projectname="xyz");
1) Try put your query to single quote
2) rawQuery returns Cursor, not String
So,
Cursor pidCursor = sqLiteDatabase.rawQuery( "select id from "+sqLiteHelper.TABLE_NAME+" where projectName = '"+clickedId+"'", null );
If you want to get the corresponding rows from the 2nd table when you pass as an argumnent the value of a ProjectName (I guess this is clickedId although its name is id?), create a statement like this:
String sql =
"select t2.* from " + SQLiteHelper.TABLE_NAME1 +
" t2 inner join " + SQLiteHelper.TABLE_NAME +
" t1 on t1.id = t2.pid where t1.ProjectName = ?";
This joins the 2 tables and returns all the columns of the 2nd table.
The execute rawQuery() by passing clickedId as a parameter, which is the proper way to avoid sql injection:
Cursor cursor = sqLiteDatabase.rawQuery(sql, new String[] {clickedId});

Check if column string in database is a substring of a query in sqlite

the current Sqlite syntax can check if a query is a substring of a column string.
and I can do a hack to add a % behind the ? described in https://stackoverflow.com/a/5752671/908821
final String whereClause = DbHelper.COLUMN_URL + " LIKE ? " ;
is there a syntax where i can do the opposite? I want to check if the column string is a substring of a query.
The following code does not seem to work.
final String whereClause = "? LIKE " + DbHelper.COLUMN_URL ;
The following are the rest of my codes:
String[] whereArg = {url};
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_LAST_URL, lastUrl);
database.updateWithOnConflict(DbHelper.TABLE_BOOKMARK,
values,
whereClause,
whereArg,
SQLiteDatabase.CONFLICT_IGNORE);
I am trying to update "LastUrl" column if the base url is a subString of a query.
some examples that i like to catch
Base Url (in database) Query
-----------------------------------------------------------------
http://example.com/path http://example.com/path/path2
http://example.com/path?id=0 http://example.com/path?id=0&x=xx
The LIKE operator checks whether the value on the left matches the pattern on the right. So just put the value on the left, and the pattern on the right.
If the pattern needs a % that is not in the database, you have to append it:
SELECT ... WHERE ? LIKE LastUrl || '%' ...

How to write query if I have to get output using two values from the same columns

I want to get values from table Noon for the columns category=LD for age=1.5 and 4. I have written the following query but there is something wrong.What should i add between age = '1.5' '4'(AND,+ or something else) or some whole new query??
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age = '1.5' '4' " ;
Your query should be like:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age BETWEEN '1.5' AND '4'";
Your query should be like this:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND age in
('1.5','4' )"
;
Or try this:
String query = "SELECT * FROM Noon WHERE category = 'LD' AND (age=
'1.5' OR age='4' )" ;
Both should work.
please try and accept the answer if successful.

error occurs when updating a table in sqlite

String updateQuery = "UPDATE Bookdetails SET lastchapter = " + test + " WHERE bookpath=" +sentFilename;
db.execSQL(updateQuery);
Error:
03-04 13:36:23.997: I/System.out(9722): android.database.sqlite.SQLiteException:
near "/": syntax error: , while compiling: UPDATE Bookdetails SET lastchapter =
mukund WHERE bookpath=/mnt/sdcard/Download/What's so great about the doctrine of
grace.epub errors happens
the error is posted above
My table contains the field id, bookpath and lastchapter, book path contains the values
/mnt/sdcard/Download/What's so great about the doctrine of grace.epub
/mnt/sdcard/Download/1b4fdaac-f31d-41e8-9d15-26c9078d891f.epub
/mnt/sdcard/Download/Commentary on Romans.epub
and lastchapter contains the values nothing nothing nothing
id contains 1 2 3
why is the error occurring at "/" there is no hash in my update query it is only there at string which stores bookpath? Is this an error?
String literals in SQL need to be in '' quotes.
However, it's better to use ? placeholders for literals like this:
String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });
I believe your lastchapter & bookpath is of type String (TEXT). Hence when you are adding or updating it's value you should always use ' ( Single cot ) around it. Change your query to this,
String updateQuery = "UPDATE Bookdetails SET lastchapter ='" + test + "' WHERE bookpath='" +sentFilename + "'";
db.execSQL(updateQuery);
However, Direct Execution of SQL query is not advisable at developer.android.com hence you can use alternative way like below code,
String updateQuery = "UPDATE Bookdetails SET lastchapter=? WHERE bookpath=?";
db.execSQL(updateQuery, new String[] { test, sentFilename });

Update query in android

I am trying to update the database on the basis of incoming parameter but it is not updated.
i am using the following code:
public static void markFavoriteStation(String station, boolean favorite){
Log.d(AppConstants.TAG,"StationListDBIfc: +markFavoriteStation");
String Query = null;
mDb = bartDb.getWritableDatabase();
Query = "update stationlistTable set favorite ='1' where namewithabbr = '+station'";
mDb.rawQuery(Query, null);
Log.d(AppConstants.TAG,"StationListDBIfc: -markFavoriteStation");
}
I think you might have a malformed String definition. You should end the String before concatenating the "station" variable to it, like so:
Query = "update stationlistTable set favorite ='1' where namewithabbr = '" + station + "'";
I can't see any errors. I guess the SQL query has errors or the namewithabbr column doesn't contain what you expect. You should test it in the sqlite3 app.

Categories

Resources