i am trying to run following code
mDb.beginTransaction();
String updateQuery ="INSERT INTO MAAccounts(userId, accountId, accountType, accountName, parentAccountId, currencyCode, isTransactionDefaultStatusOpen, currentBalance, monthlyBudget, createdOn, updatedOn) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
String[] valVars = {
stringToDB(account.userId),
integerToDB(account.accountId).toString(),
integerToDB(account.accountType.getValue()).toString(),
stringToDB(account.accountName),
integerToDB(account.parentAccountId),
stringToDB(account.currencyCode),
boolToDB(account.isTransactionDefaultStatusOpen).toString(),
CurrencyToDB(account.currentBalance).toString(),
CurrencyToDB(account.monthlyBudget).toString(),
dateToDB(now),
"false"};
// Cursor c = mDb.rawQueryWithFactory(null, updateQuery, valVars, null);
Cursor c = mDb.rawQuery(updateQuery, valVars);
try{
mDb.setTransactionSuccessful();
}catch (Exception e){
Log.e("Error in transaction", e.toString());
}finally{
mDb.endTransaction();
c.close();
}
}
The cursor it returns is null and even teh transaction is successful it won’t store the data i have provided into the database.
My Primary key consists of UserId, accountId, and accountType
No exceptions are being recorded. Any idea where i am doing mistake?
First problem. Your exception handling is a bit confused. You want to be catching exceptions which happen while executing your query, rather than just managing the transaction. I suspect if you fix this, you’ll see an exception being thrown.
It should look like this. Note that the query is now executed inside the
try {...}block.Second problem. You are not querying the database; you are inserting data. This operation should not be expected to return a result, so
rawQuery()is really the wrong thing to be using. You should investigateexecSQL()instead, or use theinsert()method which is much easier.