2011-08-02 184 views
1

我想使用下面的代码,但我无法更新我的表。更新表无法正常工作

db = dbHelper.getWritableDatabase(); 
int i = 1; 
String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = "+message_id; 
SQLiteStatement update; 
update = db.compileStatement(updateStatement); 
update.executeInsert(); 

相反的update.executeInsert(),我也试过update.executeI(),但它也不能工作。

+0

如果将错误它将帮助别人诊断问题 –

+0

在日志猫它显示的数据被更新的logcat的输出。但是当我从表中检索数据...数据没有更新 –

+0

你确定executeUpdateDelete()的返回值是1吗? –

回答

1

希望这会有所帮助

db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  

ContentView update = new ContentView(); 
update.put("fieldNameToUpdate","Value Here as a String " or integer); 
db.update("table", update,/*where clause*/ "id=2",null); 

db.close(); 

这对我有用。 您可以根据您的要求更改值。

感谢 沙阿..

0

我想,如果所有其他的事情是正确的问题是,在您的查询

问题可能出在该领域type.Suppose如果MESSAGE_ID不是number.Then使用像

String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = '"+message_id+"'"; 

同为领域的地位,如果它不是一个数字类型必须使用'

0

如果你想使用SQLiteStatementexecuteUpdateDelete()方法,这个工程:

String sql = "UPDATE table_name SET column_2=? WHERE column_1=?"; 
SQLiteStatement statement = db.compileStatement(sql); 

int id = 7; 
String stringValue = "hi there"; 

statement.bindString(1, stringValue); 
statement.bindLong(2, id); // These match to the two question marks in the sql string 

int numberOfRowsAffected = statement.executeUpdateDelete(); 

注:executeUpdateDelete()在API 11 See this Q&A介绍。

Fuller explanation on how to use prepared statements