2013-09-23 73 views
4

我开发的Android应用,其中我需要更新以基于一定的表中的列,其中clause.Here是下面的代码,SQLite数据库更新行机器人

public void updatethekeyofweeklycolumn(String profilename, String keystemp) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 

    values.put(Profile_keysofweekly, keystemp); 

    db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = "+profilename, null); 
} 

上面的代码与where子句一起工作正常,但是它的throw方法与where子句相关。我的查询是否错误?

+0

老兄,你为什么不接受人们给你的答案?这不像你应该给他们钱... – gunar

+2

伙计,放松。我刚才看到了你的答案。感谢它的工作。 – Dave

+1

我对栈交换的api没有太多的了解。我所做的只是如果答案有用就投票,如果答案合适,就接受答案。我无法接受不起作用并误导别人的答案,即使我不付钱对吗? :)。如果答案是正确的,我肯定会赞扬这个帮助。对不起,男人,不接受我的错误答案。 – Dave

回答

14

您需要转义配置文件名称。这个一个

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = ?", new String[] {profilename}); 
+11

不应该是db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY,values,Profile_Name_for_weekly +“=?”,new String [] {profilename})? – safari

+0

我认为应该。 –

0

搜索:让你无论是添加单'字符:

db.update(TABLE_PROFILE_SETTINGS_FOR_WEEKLY, values,Profile_Name_for_weekly +" = '"+ profilename + "'", null); 

或者,选择我会按照

db.update( “yourtable”,cvalue, “Profile_Name_for_weekly =”+“'”+ profilename +“'”,null);

1

对于更一般的帮助了解如何更新数据库行时,documentation竟是有益的这个时候:

SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

// New value for one column 
ContentValues values = new ContentValues(); 
values.put(FeedEntry.COLUMN_NAME_TITLE, title); 

// Which row to update, based on the ID 
String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; 
String[] selectionArgs = { String.valueOf(rowId) }; 

int count = db.update(
    FeedReaderDbHelper.FeedEntry.TABLE_NAME, 
    values, 
    selection, 
    selectionArgs); 

本页面也是有帮助的:Working with SQLite Database (CRUD operations) in Android

在我来说,我做了一个方法像这样:

public long updateTime(long rowId) { 

    // get current Unix epoc time in milliseconds 
    long date = System.currentTimeMillis(); 

    SQLiteDatabase db = helper.getWritableDatabase(); // helper is MyDatabaseHelper, a subclass database control class in which this updateTime method is resides 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(MyDatabaseHelper.DATE_TIME, date); // (column name, new row value) 
    String selection = MyDatabaseHelper.ID + " LIKE ?"; // where ID column = rowId (that is, selectionArgs) 
    String[] selectionArgs = { String.valueOf(rowId) }; 

    long id = db.update(MyDatabaseHelper.FAVORITE_TABLE_NAME, contentValues, selection, 
      selectionArgs); 
    db.close(); 
    return id; 
}