2015-12-18 55 views
-2

谁能帮我固定在我的查询问题?SQLite的 - 更新多个表

我试图调用一个函数来更新其在"col"列的"1"值的多个表,但它不工作,

创建my_function

public long updateNow() { 
     mDbHelper = new DatabaseHelper(mContext); 
     mDb = mDbHelper.getWritableDatabase(); 

     int doneClear = 0; 
     String base_value = "1"; // update all column "col" where value of 1 

     ContentValues initialValues = new ContentValues(); 
     initialValues.put("col", "0"); // Update all column "col" with 0 

     doneClear = mDb.update(SQLITE_TABLE_ONE, initialValues, "col=" + base_value, null); 
     doneClear = mDb.update(SQLITE_TABLE_TWO, initialValues, "col=" + base_value, null); 
     doneClear = mDb.update(SQLITE_TABLE_THREE, initialValues, "col=" + base_value, null); 
     doneClear = mDb.update(SQLITE_TABLE_FOUR, initialValues, "col=" + base_value, null); 
     doneClear = mDb.update(SQLITE_TABLE_FIVE, initialValues, "col=" + base_value, null); 
     doneClear = mDb.update(SQLITE_TABLE_SIX, initialValues, "col=" + base_value, null); 
     Log.w(TAG, Integer.toString(doneClear)); 
     return doneClear; 
    } 

此代码不能正常工作,我不明白为什么。
什么也没有发生在col列。

无论如何,该代码是基于我以前的查询,这是工作的罚款。

working_update_query

public long updates(String recent_value, String _id, String table_name) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put("recent_value", recent_value); 
     Log.w(TAG, String.valueOf(initialValues) + " WITH ID OF " + _id + " IN TABLE OF " + table_name); 
     return mDb.update(table_name, initialValues, "_id=" + _id, null); 
    } 

我在这里停留了将近1个小时,不能找出问题。
任何人都可以帮助我吗?

+0

严重...你应该盯在'“_id =” + _id',直到你得到它... **下一次定义** *它不工作* – Selvin

回答

0

试试这个使用通配符适当地注入更新的参数,如:

int updateCount = db.update(
    TABLE_NAME, 
    newValues, 
    "col = ?", 
    new String[] {String.valueOf(colValue)} 
); 

对于一个快速样品检出this tutorial

+0

谢谢!得到它了! – bernzkie