繁体   English   中英

更新特定行中的多列SQL LITE

[英]Updating multiple columns in a certain row SQL LITE

我有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);
}

我会保留原样。

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

使用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。

例如

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

将 :-

  • 更新行;
    • Fred替换COL2,
    • 不更改COL3(传递的字符串的长度小于1)
    • 不更改COL4(空传递)
    • 将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()])        
        );
    }
}

!请注意,该代码是原则性的,尚未经过测试,因此可能存在一些错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM