以下是我用来清除数据库中所有数据的代码
public Cursor readEntry() {
SQLiteDatabase db = helper.getWritableDatabase();
String[] allColumns = new String[] { data.NAME,
data._STATUS, data.WEIGHT, data.DATE};
Cursor c = db.query(data.TABLE_NAME, allColumns, null, null, null,
null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public boolean deleteRow(long id) {
SQLiteDatabase db = helper.getWritableDatabase();
String where = data.UID + "=" + id;
return db.delete(data.TABLE_NAME, where, null) != 0;
}
public void DeleteAll(){
Cursor c = readEntry();
long id = c.getColumnIndexOrThrow(data.UID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) id));
} while (c.moveToNext());
}
c.close();
}
我的问题是,如何使用NAME而不是使用ID,如何调用String方法?上面的代码只能调用方法data.DeleteAll();但是在String情况下,我该怎么称呼它?
答案 0 :(得分:0)
使用此:
public boolean deleteRow(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
String where = data.NAME + "=" + name;
return db.delete(data.TABLE_NAME, where, null) != 0;
}
public void DeleteAllUsingNames(){
Cursor c = readEntry();
long index = c.getColumnIndexOrThrow(data.NAME);
if (c.moveToFirst()) {
do {
deleteRow(c.getString((int) index);
} while (c.moveToNext());
}
c.close();
}
答案 1 :(得分:0)
您将采取以下措施:
public boolean deleteRow(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
String where = data.NAME + "=" + name;
return db.delete(data.TABLE_NAME, where, null) != 0;
}
答案 2 :(得分:0)
只需将id
替换为name
,即可:
public Cursor readEntry() {
SQLiteDatabase db = helper.getWritableDatabase();
String[] allColumns = new String[] { data.NAME,
data._STATUS, data.WEIGHT, data.DATE};
Cursor c = db.query(data.TABLE_NAME, allColumns, null, null, null,
null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public boolean deleteRow(String name) {
SQLiteDatabase db = helper.getWritableDatabase();
String where = data.NAME + " = " + "'" + name + "'";
return db.delete(data.TABLE_NAME, where, null) != 0;
}
public void DeleteAll(){
Cursor c = readEntry();
long id = c.getColumnIndexOrThrow(data.NAME);
if (c.moveToFirst()) {
do {
deleteRow(c.getString((int) id));
} while (c.moveToNext());
}
c.close();
}