how to update sqlite db in Android? - android

I want to update one row based on multiple row value, After run the application getting error like = ( FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE Tablename SET sFollowed=?,Code=?,Name=? WHERE Vessel_Name= )
Here is my update method in MyDbhelper class
public boolean update_isFollowed(String strVesselName , String strVesselNotationNumber, String strIsFollowed) {
SQLiteDatabase db1 = getReadableDatabase();
ContentValues values=new ContentValues();
values.put(COL_VesselName,strVesselName );
values.put(COL_ClassCodeNumber , strVesselNotationNumber);
values.put(COL_IsFollowedVessels, strIsFollowed);
int id=db1.update(Table_VesselList,values,COL_ClassCodeNumber + "=" +strVesselNotationNumber +" and "+ COL_VesselName +"="+strVesselName,null);
return id > 0;
}

Do something like this
In your activity
private UpdateDataInDB mUpdateDataInDb;
mUpdateDataInDb = new UpdateDataInDB(activity);
// call the update method with updated strings
mUpdateDataInDb.updateCommentDetails(_name, heading, comment);
In Database class
public void updateCommentDetails(String name, String updatedHeading,
String updatedComment) {
// TODO Auto-generated method stub
mDatabase = mDatabase_handler.getReadableDatabase();
ContentValues args = new ContentValues();
args.put("Heading", updatedHeading);
args.put("Comment", updatedComment);
mDatabase.update("AddCommentTable", args,"Name" + "= ?", new String[]{ name });
args.clear();
}

Use proper where condition string with values as argument. like:
String where = COL_ClassCodeNumber+"=? AND "+COL_VesselName="?";
String[] whereArgs = new String[] {String.valueOf(strVesselNotationNumber),
String.valueOf(strVesselName)};
int id=db1.update(Table_VesselList,values,where,whereArgs);

Related

Update values of a specific column of a specific row. Android SQLite

I have a recyclerview cardholder that inflates using the values of 'NAME' from the table 'ORDERTABLE'. The cardholder also have an EditText which displays the values of column 'QUANTITY' in my SQLite database.
I also have a button to update the database for every changes in EditText.
I have this table ORDERTABLE
id NAME QUANTITY
1 Order1 1
2 Order2 1
3 Order3 1
4 Order4 1
Being more specific, how can i update the QUANTITY of Order2 on onButtonPressed with the new values of my EditText.
EDIT...
I have tried this code but nothing happened
Button to update values
public void addButtonClick(TextView tv_cardrowName_atc, TextView tv_currentPrice_atc, EditText et_quantity_atc, int position) {
int thisQuantity = Integer.parseInt(et_quantity_atc.getText().toString());
thisQuantity++;
String orderName = tv_cardrowName_atc.getText().toString();
String oldQuantity = et_quantity_atc.getText().toString();
String newQuantity = String.valueOf(thisQuantity);
sqliteCBLCAdapter.selectUpdateItem(orderName, oldQuantity, newQuantity);
et_quantity_atc.setText(String.valueOf(thisQuantity));
}
Update Method
public String selectUpdateItem(String orderName, String oldQuantity, String newQuantity) {
SQLiteDatabase sqLiteDatabase = sqLiteCBLC.getWritableDatabase();
String[] columns = {SQLiteCBLC.COL_ORDNAME, SQLiteCBLC.COL_QUANTITY};
Cursor cursor = sqLiteDatabase.query(SQLiteCBLC.TABLE_NAME, columns, SQLiteCBLC.COL_ORDNAME + " = '" + orderName + "'", null, null, null, null);
StringBuffer stringBuffer = new StringBuffer();
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(SQLiteCBLC.COL_ORDNAME);
int index2 = cursor.getColumnIndex(SQLiteCBLC.COL_QUANTITY);
String order = cursor.getString(index1);
String quantity = cursor.getString(index2);
ContentValues contentValues = new ContentValues();
contentValues.put(SQLiteCBLC.COL_QUANTITY, newQuantity);
String[] whereArgs = {quantity};
sqLiteDatabase.update(SQLiteCBLC.TABLE_NAME, contentValues, SQLiteCBLC.COL_QUANTITY + " =? ", whereArgs);
stringBuffer.append(order);
}
return stringBuffer.toString();
}
The easiest way for you to achieve this would be to use a SQL update query as follows:
From the SQLite Web Site:
The SQLite UPDATE Query is used to modify the existing records in a table. You can use a WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be updated.
The syntax for the update query is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
So in your case your sql update query would look some thing like this:
UPDATE ORDERTABLE SET QUANTITY = (INSERT VALUE OF YOUR EDIT TEXT) WHERE NAME = 'Order2'
You can then execute your query by using the execSQL() method of your SQLiteDatabase object that you have and passing in the sql query above as the string parameter.
You can try like this below code, In your case you while updating you are updating based on quantity, multiple order will have the same quantity. just check the order name and update it.
public void selectUpdateItem(String orderName, String oldQuantity, String newQuantity) {
if (TextUtils.isEmpty(order)) {
return;
}
ContentValues contentValues = new ContentValues();
final String whereClause = SQLiteCBLC.COL_ORDNAME + " =?";
final String[] whereArgs = {
orderName
};
// if you want to update with respect of quantity too. try this where and whereArgs below
//final String whereClause = SQLiteCBLC.COL_ORDNAME + " =? AND " + SQLiteCBLC.COL_QUANTITY + " =?";
//final String[] whereArgs = {
//orderName, String.valueOf(oldQuantity)
//};
contentValues.put(SQLiteCBLC.COL_QUANTITY, newQuantity);
SQLiteDatabase sqLiteDatabase = sqLiteCBLC.getWritableDatabase();
sqLiteDatabase.update(SQLiteCBLC.TABLE_NAME, contentValues,
whereClause, whereArgs);
}

Android row is not getting update

I want to update the row values with 0 or 1. Here is my update method.Where is wrong with my code .
dbhelper.updateStarFlag(actid, strStarFlagMark);
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getReadableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "='"+str_ActivityId+"'", null);
return i > 0;
}
If you want to update something, you need a writable database. I changed the query to look a little more like in the documentation for SQLiteDatabase
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getWriteableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "= ? ", new String[]{str_ActivityId});
return i > 0;
}
To update something in database, you need writeable database, not just readable.
Change below line:
SQLiteDatabase db1 = getReadableDatabase();
to :
SQLiteDatabase db1 = getWriteableDatabase();
Hope, this helps you.

SQLite no such column error

Hi can anyone please help me with below error in android sqlite ? really appreciate!
Caused by: android.database.sqlite.SQLiteException: no such column: House (code 1): , while compiling: select * from category where category =House
below is part of my code in which I have inserted "House" in the table
public void onCreate(SQLiteDatabase db) {
String CREATE_CATEGORY_TABLE = "CREATE TABLE category( " +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"category TEXT UNIQUE)";
db.execSQL(CREATE_CATEGORY_TABLE);
}
public void addCategory(String name){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("category", name);
db.insert(CATEGORY_TABLE, // table
null, //nullColumnHack
cv); // key/value -> keys = column names/ values = column values
db.close();}
public List getCategory(){
List<String> list=new LinkedList();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor =
db.rawQuery("select * from category where category =house" , null);
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
do {
String s = (cursor.getString(1));
list.add(s);
}while (cursor.moveToNext());
return list;
}
You need to wrap house with single quotes
db.rawQuery("select * from category where category = 'house'" , null);
In my case error was in trigger I wrote on table insert and update

accessing variable in activity from database

i have an interplay between the MainActivity and the DatabaseAdapter class where I'm trying to access an integer variable from the MainActivity. The code as follows;
in MainActivity...
int clam = 7;
public void onClick_UpdateJ(View v) {
myDb.updateJ();
}
in DbAdapter
public class {
.....
public void updateJ(){
int grape = "coli = ?;
ContentValues arges = new ContentValues();
arges.put(coli, 7);
// Insert it into the database.
db.update(DATABASE_TABLE, arges, grape, new int[]{1});
}
}
As you can see, I'm trying to update the coli data (integer) in the database from the current 1 to 7. How do I do this? I tried shifting everything in DbAdapter to MainActivity but the 'update' method is not recognized.
Thanks in advance.
public void updateJ(int value) {
ContentValues cv = new ContentValues();
cv.put("coli", value);
String where = "coli = ?";
whereArgs = new String[] { Integer.toString(1) };
db.update(DATABASE_TABLE, cv, where, whereArgs);
}
The update method takes 4 arguments:
table (String): the table name
values (ContentValues): the values to insert
selection (String): a SQLite WHERE clause. You already know that the ? character will be replaced by values in selectionArgs.
selectionArgs (String[]): values that replace ?s in the where clause

Updating a data base with a single field

I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
How to make an update for a specific column (according to its index) in that database?
db.update
(
MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?",
new String[] { idValue }
);
You can use:
ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);
KEY1,KEY2 ... are the name of the fields you want to update
value1,value2... are the new values of the fields you want to update
the where String is to tell in which row you need to update.

Categories

Resources