更新某一行SQL LITE中的多个列

时间:2018-01-08 23:04:02

标签: java android sql database sqlite

我有4种方法可以更新某一行中不同的数据部分。 (我这样做是因为我是SQ Lite和Android的新手)。我如何将所有这些压缩成一种方法?

public void updateInt(int id,String newName, String oldName){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
            " = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
            " AND " + COL2 + " = '" + oldName + "'";
    sqLiteDatabase.execSQL(query);
}

public void updateAuth(int id,String newAuth, String oldAuth){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL3 +
            " = '" + newAuth + "' WHERE " + COL1 + " = '" + id + "'" +
            " AND " + COL3 + " = '" + oldAuth + "'";
    sqLiteDatabase.execSQL(query);
}

public void updateUser(int id,String newUser, String oldUser){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL5 +
            " = '" + newUser + "' WHERE " + COL1 + " = '" + id + "'" +
            " AND " + COL5 + " = '" + oldUser + "'";
    sqLiteDatabase.execSQL(query);
}

public void updateLocation(int id,String newLocation, String oldLocation){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    String query = "UPDATE " + TABLE_NAME + " SET " + COL4 +
            " = '" + newLocation + "' WHERE " + COL1 + " = '" + id + "'" +
            " AND " + COL4 + " = '" + oldLocation + "'";
    sqLiteDatabase.execSQL(query);
}

2 个答案:

答案 0 :(得分:2)

我原样保留原样。

由于每个表达式在不同的行中设置不同的内容(所有WHERE条件都不同),因此无法真正合并。

答案 1 :(得分:0)

使用SQLitedatbase update 便捷方法,您可以使用: -

public void updateAll(int id, 
        String newName, 
        String newAuth, 
        String newUser, 
        String newLocation) {

    ContentValues cv = new ContentValues();
    cv.put(COL2,newName);
    cv.put(COL3,newAuth);
    cv.put(COL4,newLocation);
    cv.put(COL5,newUser);
    SqliteDatabase sqliteDatabase = this.getWriteableDatabase();
    sqliteDatabase.update(TABLE_NAME,
        cv,
        COL1+"=?",
        new String[]{Long.toString(id)}
    );
}
  • 这假定您要更新所有列。

如果您只想更新某些列但仍使用一种方法,则: -

public void updateAll(int id, 
        String newName, String oldName
        String newAuth, 
        String newUser, 
        String newLocation) {

    ContentValues cv = new ContentValues();
    if (newName != null && newName.length() > 0) {
        cv.put(COL2,newName);
    }
    if (newAuth != null && newAuth.length() > 0) {
        cv.put(COL3,newAuth);
    }
    if (newLocation != null && newLocation.length() > 0) { 
        cv.put(COL4,newLocation);
    }
    if (newUser != null && newUser.length() > 0) {
        cv.put(COL5,newUser);
    }
    if (cv.size() > 0) {
        SqliteDatabase sqliteDatabase = this.getWriteableDatabase();
        sqliteDatabase.update(TABLE_NAME,
            cv,
            COL1+"=?",
            new String[]{Long.toString(id)}
        );
    }
}
  • 这再次假定您知道相应的ID。

e.g。

yourdbhelper.updateAll(id, "Fred","",null,"France");

会: -

  • 更新行;
    • Fred 替换COL2,
    • 不更改COL3(传递的字符串的长度小于1)
    • 不更改COL4(null传递)
    • 将COL5更改为法国

如果你想只更新旧???匹配的值然后您可以使用: -

public void updateAll(int id, 
    String newName, String oldName,
    String newAuth, String oldAuth,
    String newUser, String oldUser,
    String newLocation, String oldLocation) {

    ArrayList<String> whereargs = new ArrayList<>(Arrays.asList(String.valueOf(id)));
    StringBuilder whereclause = new StringBuilder(COL1+"=?");

    if(oldName != null && oldName.length > 0) {
        whereclause.append(" AND " + COL2 + "=?");
        whereargs.add(oldName);
    }
    if(oldAuth != null && oldAuth.length > 0) {
        whereclause.append(" AND " + COL3 + "=?");
        whereargs.add(oldAuth);
    }

    if(oldUser != null && oldUser.length > 0) {
        whereclause.append(" AND " + COL4 + "=?");
        whereargs.add(oldUser);
    }

    if(oldLocation != null && oldLocation.length > 0) {
        whereclause.append(" AND " + COL5 + "=?");
        whereargs.add(oldLocation);
    }

    ContentValues cv = new ContentValues();
    if (newName != null && newName.length() > 0) {
        cv.put(COL2,newName);
    }
    if (newAuth != null && newAuth.length() > 0) {
        cv.put(COL3,newAuth);
    }
    if (newLocation != null && newLocation.length() > 0) { 
        cv.put(COL4,newLocation);
    }
    if (newUser != null && newUser.length() > 0) {
        cv.put(COL5,newUser);
    }
    if (cv.size() > 0) {
        SqliteDatabase sqliteDatabase = this.getWriteableDatabase();
        sqliteDatabase.update(TABLE_NAME,
            cv,
            whereclause.toString(),
            whereargs.toArray(new String[whereargs.size()])        
        );
    }
}

!请注意代码是原则性的,并且未经过测试,因此可能会出现一些错误。