在我的程序中,我使用SQLite数据库来存储数据,同时也允许用户更新现有记录。
我的代码帮助我更新现有记录,但是它创建了带有更新记录的重复条目,例如:如果更新任何记录7次,那么它为具有更新数据的相同更新记录创建7个条目。
在活动中使用以下代码行: -
saveStatus = myDb.UpdateData(
editImage.getText().toString(),
editPersonName.getText().toString());
数据库类代码: -
public long UpdateData(String strImageName, String strPersonName){
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
ContentValues Val = new ContentValues();
Val.put("ImageName", strImageName);
Val.put("PersonName", strPersonName);
long rows = db.update(TABLE_MEMBER, Val, "ImageName=?",
new String[] { String.valueOf(strImageName) });
db.close();
return rows; // return rows updated.
} catch (Exception e) {
return -1;
}
}
整个代码:
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(ImageName VARCHAR(100)," +
" PersonName VARCHAR(100));");
Log.d("CREATE TABLE","Create Table Successfully - classs");
}
// Insert Data
public long InsertData(String strImageName, String strPersonName) {
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
ContentValues Val = new ContentValues();
Val.put("ImageName", strImageName);
Val.put("PersonName", strPersonName);
long rows = db.insert(TABLE_MEMBER, null, Val);
db.close();
return rows; // return rows inserted.
} catch (Exception e) {
return -1;
}
}
// Update Data
public long UpdateData(String strImageName, String strPersonName){
// TODO Auto-generated method stub
try {
SQLiteDatabase db;
db = this.getWritableDatabase(); // Write Data
ContentValues Val = new ContentValues();
Val.put("ImageName", strImageName);
Val.put("PersonName", strPersonName);
long rows = db.update(TABLE_MEMBER, Val, "ImageName=?",
new String[] { String.valueOf(strImageName) });
db.close();
return rows; // return rows updated.
} catch (Exception e) {
return -1;
}
}
// Fetch data
public String[] SelectData(String strImageName) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[] { "*" },
"ImageName=?",
new String[] { String.valueOf(strImageName) }, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
// Check for data(s) using ImageName field
public boolean Exists(String strImageName) {
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.rawQuery("select 1 from DataTable where ImageName= ?",
new String[] { strImageName });
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}