如何在Android中更新sqlite db?

时间:2015-08-03 11:47:42

标签: android sqlite

我想基于多行值更新一行,运行应用程序后获取错误,如=( FATAL EXCEPTION:main     android.database.sqlite.SQLiteException:near" =&#34 ;:语法错误(代码1):,编译时:UPDATE Tablename SET sFollowed = ?, Code = ?, Name =? WHERE Vessel_Name =

这是我在MyDbhelper类中的更新方法

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;
    }

2 个答案:

答案 0 :(得分:1)

做这样的事情

在您的活动中

private UpdateDataInDB mUpdateDataInDb;
mUpdateDataInDb = new UpdateDataInDB(activity);

// call the update method with updated strings
mUpdateDataInDb.updateCommentDetails(_name, heading, comment);

在数据库类

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();
}

答案 1 :(得分:0)

使用带有值作为参数的条件字符串。像:

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);