SQLite数据库Android,检查记录是否存在并保存

时间:2015-04-07 18:10:36

标签: android sqlite row

我有一个数据库,它扩展了SQLiteOnHelper的应用程序 然后我必须确保,例如,如果我更改仅标题,您应该更新记录,否则正常插入。

这是我的表:

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;
}

我认为我们需要进行查询。 任何帮助或建议表示赞赏:)

2 个答案:

答案 0 :(得分:0)

如果可以在内存中进行此检查,则应该避免查询数据库。编写一些方法,将笔记与用户编辑进行比较。

如果没有,您可以在数据库中查询具有除标题之外的所有相同字段的条目:

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
}

答案 1 :(得分:0)

您应该为更新记录创建单独的方法,或者根据标志创建单独的更新查询,该标志可以在填充表单已更新或仅打开空白表单的条件下设置。如果打开包含填充数据的相同表单以及空白表单,则可以通过逐个检查每个字段来检查打开的表单是空白还是填充。如果空白表单已打开,您可以将标志=&#34;插入&#34;并且激活插入查询/方法或者如果填充的表单已更新并且用户已经更改了该表单中可以通过比较先前值来检查的内容,则可以将标志=&#34;更新&#34;并以相同的方法触发相关的更新查询,或者您可以调用单独的更新方法。

相关问题