- db.execSQL("replace into account_values(profile, account, " +
- "currency, value, generation) values(?, ?, ?, ?, ?);",
- new Object[]{uuid, name, Misc.emptyIsNull(currency), amount, generation});
+ long accId = findAddAccount(db, name);
+
+ db.execSQL("replace into account_values(account_id, " +
+ "currency, value, generation) values(?, ?, ?, ?);",
+ new Object[]{accId, Misc.emptyIsNull(currency), amount, generation});
+ }
+ private long findAddAccount(SQLiteDatabase db, String accountName) {
+ try (Cursor c = db.rawQuery("select id from accounts where profile_id=? and name=?",
+ new String[]{String.valueOf(id), accountName}))
+ {
+ if (c.moveToFirst())
+ return c.getLong(0);
+
+ }
+
+ try (Cursor c = db.rawQuery(
+ "insert into accounts(profile_id, name, name_upper) values(?, ?, ?) returning id",
+ new String[]{String.valueOf(id), accountName, accountName.toUpperCase()}))
+ {
+ c.moveToFirst();
+ return c.getInt(0);
+ }