[英]Updating multiple columns in a certain row SQL LITE
I have 4 methods all to update separate parts of data in a certain row. 我有4种方法来更新某一行中数据的单独部分。 (I did this because I'm new to SQ Lite and Android).
(我这样做是因为我是SQ Lite和Android的新手)。 How would I condense all of this into one method?
我如何将所有这些浓缩为一种方法?
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);
}
I'd leave it as is. 我会保留原样。
Since each expression sets different things in different rows (all of the WHERE
conditions are different) it can't really be merged. 由于每个表达式在不同的行中设置不同的内容(所有
WHERE
条件都不同),因此它实际上无法合并。
Using the SQLitedatbase
update
convenience method, you could use :- 使用
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)}
);
}
If you wanted to just update certain columns but still use the one method, then :- 如果您只想更新某些列但仍使用一种方法,则:-
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)}
);
}
}
eg 例如
yourdbhelper.updateAll(id, "Fred","",null,"France");
would :- 将 :-
If you wanted to update only if the old??? 如果您只想更新旧的??? values matched then you could use :-
匹配的值,则可以使用:-
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()])
);
}
}
!Note the code is in-principle and hasn't been tested so it may have some errors. !请注意,该代码是原则性的,尚未经过测试,因此可能存在一些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.