T Tavian:
Tengo esta función que debe insertar datos en la base de datos y SQlite falsa retorno si los datos no consigue sin embargo inserta la función devuelve cierto, pero cuando voy a comprobar el archivo .db no hay datos en la tabla.
public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
ContentValues contentValues = new ContentValues();
SQLiteDatabase db = this.getWritableDatabase();
//set data to columns
contentValues.put(COL_2, name);
contentValues.put(COL_3, emailAddress);
contentValues.put(COL_4, postalAddress);
contentValues.put(COL_5, password);
contentValues.put(COL_6, phoneNumber);
contentValues.put(COL_7, bloodType);
//inserts data into db
long result = db.insert(TABLE_NAME, null, contentValues);
//checks to see if data has been inserted correctly
if (result == -1) {
return false;
} else
return true;
}
La siguiente imagen muestra que la tabla de base de datos y se han creado correctamente.
devgianlu:
Debe llamar #beginTransaction()
, #setTransactionSuccessful()
y #endTransaction()
como se describe aquí .
Los cambios se pueden revertir si alguna transacción se finalizó sin que se marquen como limpia (llamando setTransactionSuccessful).
public boolean insertData(String name, String emailAddress, String postalAddress, String password, int phoneNumber, String bloodType) {
ContentValues contentValues = new ContentValues();
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
contentValues.put(COL_2, name);
contentValues.put(COL_3, emailAddress);
contentValues.put(COL_4, postalAddress);
contentValues.put(COL_5, password);
contentValues.put(COL_6, phoneNumber);
contentValues.put(COL_7, bloodType);
long result = db.insert(TABLE_NAME, null, contentValues);
db.setTransactionSuccessful();
return result != -1;
} finally {
db.endTransaction();
}
}