Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a value in a column in sqlite

Tags:

android

sqlite

I need to update a value in a column from a certain table. I tried this :

 public void updateOneColumn(String TABLE_NAME, String Column, String rowId, String ColumnName, String newValue){
                 String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = "+newValue+" WHERE "+Column+ " = "+rowId;
                 db.beginTransaction();
                 SQLiteStatement stmt = db.compileStatement(sql);
                 try{
                     stmt.execute();
                     db.setTransactionSuccessful();
                 }finally{
                     db.endTransaction();
                 }

        }

and I call this method like this :

 db.updateOneColumn("roadmap", "id_roadmap",id,"sys_roadmap_status_mobile_id", "1");

which means that I want to set the value 1 in the column sys_roadmap_status_mobile_id when id_roadmap = id.

The problem is that nothing happens. Where is my mistake?

like image 317
Gabrielle Avatar asked Jul 19 '12 15:07

Gabrielle


People also ask

How do I change the value of a column in SQLite?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

How do I edit a SQLite table?

Summary. Use the ALTER TABLE statement to modify the structure of an existing table. Use ALTER TABLE table_name RENAME TO new_name statement to rename a table. Use ALTER TABLE table_name ADD COLUMN column_definition statement to add a column to a table.

Which SQLite statement is used to update data into table?

The SQLite UPDATE statement is used to update existing records in a table in a SQLite database.


1 Answers

Easy solution:

String sql = "UPDATE "+TABLE_NAME +" SET " + ColumnName+ " = '"+newValue+"' WHERE "+Column+ " = "+rowId;

Better solution:

ContentValues cv = new ContentValues();
cv.put(ColumnName, newValue);
db.update(TABLE_NAME, cv, Column + "= ?", new String[] {rowId});
like image 144
WarrenFaith Avatar answered Oct 03 '22 21:10

WarrenFaith