SQLite数据库Android,检查记录是否存在并保存
我有一个数据库,它为一个 然后,我必须确保,例如,如果我只更改标题,您应该更新记录,否则将正常插入 这是我的桌子:SQLite数据库Android,检查记录是否存在并保存,android,sqlite,row,Android,Sqlite,Row,我有一个数据库,它为一个 然后,我必须确保,例如,如果我只更改标题,您应该更新记录,否则将正常插入 这是我的桌子: public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE vasca(" + "_id INTEGER PRIMARY KEY,"+ "titoloNota TEXT NOT NULL,"+ "testoNota TEXT
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE vasca(" +
"_id INTEGER PRIMARY KEY,"+
"titoloNota TEXT NOT NULL,"+
"testoNota TEXT NOT NULL,"+
"dataNota DATE,"+
"coloreNota INTEGER NOT NULL,"+
"UNIQUE(id, titoloNota)"+ // to be reviewed
")";
db.execSQL(sql);
}
以及我的插入布尔方法:
public boolean insertNote(Nota nota){
// boolean inizializzat a false utilizzata come return
boolean resultInsert = false;
// repository dei dati in modalità scrittura
SQLiteDatabase dbLite = this.getWritableDatabase();
// utilizza un ContentValues come mappa di valori, dove le columns rappresentano le chiavi
ContentValues values = new ContentValues();
values.put(SQL_INSERT_OR_REPLACE, true);
values.put("titoloNota", nota.getTitolo());
values.put("testoNota", nota.getTesto());
values.put("dataNota", nota.getData()); // ritorna una string sdf.format(nota.getData())
values.put("coloreNota", nota.getColore());
// chiama il metodo insert su dbLite, Vasca è il nome della tabella
// poichè ritorna un long, se negativo significa che l'inserimento non è riuscito
long idInserimento = dbLite.insert("vasca", null, values);
// sfrutto la variabile long per fare un controllo se andato tutto ok
if( idInserimento <= -1 ){
Log.e(TAG, "Inserimento della nota non avvento");
resultInsert = false;
}else{
resultInsert = true;
// inserisce la nota passata al metodo nell'arrayList listNote chiamando il metodo add
}
dbLite.close();
return resultInsert;
}
public boolean insertNote(Nota-Nota){
//布尔值为一个假实用程序返回
布尔resultInsert=false;
//莫达利特·斯克里图拉的数据仓库
SQLiteDatabase dbLite=this.getWritableDatabase();
//价值观来自瓦洛里地图、基亚维广场
ContentValues=新的ContentValues();
value.put(SQL\u INSERT\u或\u REPLACE,true);
value.put(“titolota”,nota.getitolo());
value.put(“testoNota”,nota.getesto());
value.put(“dataNota”,nota.getData());//ritorna una string sdf.format(nota.getData())
value.put(“coloreNota”,nota.getColore());
//基亚玛·伊尔·梅托多(chiama il-metodo)插入苏德布利特(su dbLite),瓦斯卡·伊尔·诺姆·德拉·塔贝拉(Vascaèil nome dela tabella)
//波伊切里托纳很长一段时间,她对非利乌西托的看法是否定的
long idInserimento=dbLite.insert(“vasca”,null,value);
//SFRUTO la variabile long per fare un CONTROL se和TO tutto ok
如果可以在内存中进行此检查,则应避免查询数据库。编写一些方法,将注释与用户编辑进行比较
如果不是,则可以在数据库中查询除标题外具有所有相同字段的条目:
SQLiteDatabase db = mDbHelper.getWritableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
"_id",
"titoloNota"
};
// Define 'where' part of query.
String selection = "_id = ? AND testoNota = ? AND dataNota = ? AND coloreNota = ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { nota.getId(), nota.getTesto(), nota.getData(), nota.getColore() };
Cursor c = db.query(
"vasca", // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
if (c.moveToFirst()) {
// entry with the specified selection was found
int title = c.getString(c.getColumnIndex("titoloNota"));
// UPDATE if title changed
} else {
// no entry found - INSERT
}
您应该为更新记录创建一个单独的方法,或者根据标记创建单独的更新查询,该标记可以根据已填写的表单是否已更新或仅打开了空白表单的条件进行设置。如果使用已填写的表单打开同一表单,则可以通过逐个检查每个字段来检查打开的表单是否为空白或已填写数据以及空白表单的数据。如果空白表单已打开,则可以放置flag=“insert”并启动插入查询/方法,或者如果填写的表单已更新,并且用户已更改表单中的某些内容,可以通过比较以前的值来检查这些内容,则可以放置flag=“update”并以相同的方法启动相关的更新查询,或者您可以调用单独的更新方法。您解决了问题吗?没有,我正在处理!