Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新特定行中的多个列SQL LITE_Java_Android_Sql_Database_Sqlite - Fatal编程技术网

Java 更新特定行中的多个列SQL LITE

Java 更新特定行中的多个列SQL LITE,java,android,sql,database,sqlite,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 " + CO

我有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);
}
我会让它保持原样


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

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

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 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()])        
        );
    }
}
  • 这假定您要更新所有列
如果只想更新某些列,但仍使用“一”方法,则:-

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

将:-

  • 更新行;
    • 将COL2替换为Fred
    • 不更改COL3(传递的字符串长度小于1)
    • 不更改COL4(传递空值)
    • 将COL5更改为法国
如果你只想更新旧版本???匹配的值,然后您可以使用:-

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 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()])        
        );
    }
}
public void updateAll(int-id,
字符串newName,字符串oldName,
字符串newAuth,字符串oldAuth,
字符串newUser,字符串oldUser,
字符串新位置、字符串旧位置){
ArrayList wherergs=新的ArrayList(Arrays.asList(String.valueOf(id));
StringBuilder whereclause=新的StringBuilder(COL1+“=?”);
if(oldName!=null&&oldName.length>0){
其中第条。附加(“和”+COL2+“=?”);
wherergs.add(旧名称);
}
如果(oldAuth!=null&&oldAuth.length>0){
其中第条。附加(“和”+COL3+“=?”);
wherergs.add(oldAuth);
}
if(oldUser!=null&&oldUser.length>0){
其中第条。附加(“和”+COL4+“=?”);
wherergs.add(旧用户);
}
if(oldLocation!=null&&oldLocation.length>0){
其中第条。附加(“和”+COL5+“=?”);
wherergs.add(旧位置);
}
ContentValues cv=新的ContentValues();
if(newName!=null&&newName.length()>0){
cv.put(COL2,新名称);
}
if(newAuth!=null&&newAuth.length()>0){
cv.put(COL3,newAuth);
}
如果(newLocation!=null&&newLocation.length()>0){
cv.put(COL4,新位置);
}
if(newUser!=null&&newUser.length()>0){
cv.put(COL5,新用户);
}
如果(cv.size()>0){
SqliteDatabase SqliteDatabase=this.getWriteableDatabase();
sqliteDatabase.update(表名称,
个人简历
whereclause.toString(),
wherergs.toArray(新字符串[wherergs.size()])
);
}
}

!请注意,该代码原则上是未经测试的,因此可能会有一些错误。

您是在单独的时间更新该行的4列,还是按顺序(一次)调用所有这4个方法?我将id的相同变量传递到每个方法中,因此这是非常冗余的。在不知道模式的情况下将其组合成一个函数会更有意义,因为
WHERE
子句不同,那么每个子句都是不同的行。现在,如果您的where中有冗余位(即,
id
是您唯一关心的事情),那么答案就不同了。