Java Code Examples for io.requery.android.database.sqlite.SQLiteDatabase#insert()

The following examples show how to use io.requery.android.database.sqlite.SQLiteDatabase#insert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FtsDbHelper.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
static void insert(SQLiteDatabase db, EntityMessage message, String text) {
    Log.i("FTS insert id=" + message.id);
    List<Address> address = new ArrayList<>();
    if (message.from != null)
        address.addAll(Arrays.asList(message.from));
    if (message.to != null)
        address.addAll(Arrays.asList(message.to));
    if (message.cc != null)
        address.addAll(Arrays.asList(message.cc));
    if (message.bcc != null)
        address.addAll(Arrays.asList(message.bcc));

    delete(db, message.id);

    ContentValues cv = new ContentValues();
    cv.put("rowid", message.id);
    cv.put("account", message.account);
    cv.put("folder", message.folder);
    cv.put("time", message.received);
    cv.put("address", MessageHelper.formatAddresses(address.toArray(new Address[0]), true, false));
    cv.put("subject", message.subject == null ? "" : message.subject);
    cv.put("keyword", TextUtils.join(", ", message.keywords));
    cv.put("text", text);
    db.insert("message", SQLiteDatabase.CONFLICT_FAIL, cv);
}
 
Example 2
Source File: DBHelper.java    From fingen with Apache License 2.0 5 votes vote down vote up
public void updateLogProducts(SQLiteDatabase db) throws IOException {
    db.execSQL("DELETE FROM log_Products WHERE TransactionID < 0");

    Cursor cursor = db.rawQuery("SELECT _id, Amount FROM log_Transactions " +
            "WHERE _id not in (SELECT TransactionID FROM log_Products) AND Deleted = 0", null);

    ContentValues cv = new ContentValues();

    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    cv.clear();
                    cv.put(DBHelper.C_SYNC_FBID, "");
                    cv.put(DBHelper.C_SYNC_TS, -1);
                    cv.put(DBHelper.C_SYNC_DELETED, 0);
                    cv.put(DBHelper.C_SYNC_DIRTY, 0);
                    cv.put(DBHelper.C_SYNC_LASTEDITED, "");
                    cv.put(DBHelper.C_LOG_PRODUCTS_TRANSACTIONID, cursor.getLong(0));
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRODUCTID, 0);
                    cv.put(DBHelper.C_LOG_PRODUCTS_CATEGORY_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PROJECT_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRICE, cursor.getDouble(1));
                    cv.put(DBHelper.C_LOG_PRODUCTS_QUANTITY, 1);
                    db.insert(DBHelper.T_LOG_PRODUCTS, null, cv);
                    cursor.moveToNext();
                }
            }
        } finally {
            cursor.close();
        }
    }
}
 
Example 3
Source File: UpdateHelper.java    From fingen with Apache License 2.0 5 votes vote down vote up
public static void update32(SQLiteDatabase db) {
    db.execSQL("ALTER TABLE " + DBHelper.T_LOG_TRANSACTIONS + " ADD COLUMN "+DBHelper.C_LOG_TRANSACTIONS_SPLIT+" INTEGER DEFAULT 0;");

    db.execSQL(DBHelper.SQL_CREATE_TABLE_REF_PRODUCTS);
    db.execSQL(DBHelper.SQL_CREATE_TABLE_LOG_PRODUCTS);

    ContentValues cv = new ContentValues();
    cv.put(DBHelper.C_ID, 0);
    cv.put(DBHelper.C_REF_PRODUCTS_NAME, "default_product");
    db.insert(DBHelper.T_REF_PRODUCTS, "", cv);

    Cursor cursorTransactions = db.rawQuery("SELECT _id, Amount FROM log_Transactions WHERE Deleted = 0", null);
    if (cursorTransactions != null) {
        try {
            if (cursorTransactions.moveToFirst()) {
                while (!cursorTransactions.isAfterLast()) {
                    cv.clear();
                    cv.put(DBHelper.C_SYNC_FBID, "");
                    cv.put(DBHelper.C_SYNC_TS, -1);
                    cv.put(DBHelper.C_SYNC_DELETED, 0);
                    cv.put(DBHelper.C_SYNC_DIRTY, 0);
                    cv.put(DBHelper.C_SYNC_LASTEDITED, "");
                    cv.put(DBHelper.C_LOG_PRODUCTS_TRANSACTIONID, cursorTransactions.getLong(0));
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRODUCTID, 0);
                    cv.put(DBHelper.C_LOG_PRODUCTS_CATEGORY_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PROJECT_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRICE, cursorTransactions.getDouble(1));
                    cv.put(DBHelper.C_LOG_PRODUCTS_QUANTITY, 1);
                    db.insert(DBHelper.T_LOG_PRODUCTS, null, cv);
                    cursorTransactions.moveToNext();
                }
            }
        } finally {
            cursorTransactions.close();
        }
    }
    db.execSQL("CREATE INDEX [idx_Products] ON [log_Products] ([Deleted], [TransactionID], [ProductID]);");
}
 
Example 4
Source File: UpdateHelper.java    From fingen with Apache License 2.0 5 votes vote down vote up
public static void update33(SQLiteDatabase db) {
    db.execSQL("DELETE FROM log_Products WHERE TransactionID < 0");

    Cursor cursor = db.rawQuery("SELECT _id, Amount FROM log_Transactions " +
            "WHERE _id not in (SELECT TransactionID FROM log_Products) AND Deleted = 0", null);

    ContentValues cv = new ContentValues();

    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                while (!cursor.isAfterLast()) {
                    cv.clear();
                    cv.put(DBHelper.C_SYNC_FBID, "");
                    cv.put(DBHelper.C_SYNC_TS, -1);
                    cv.put(DBHelper.C_SYNC_DELETED, 0);
                    cv.put(DBHelper.C_SYNC_DIRTY, 0);
                    cv.put(DBHelper.C_SYNC_LASTEDITED, "");
                    cv.put(DBHelper.C_LOG_PRODUCTS_TRANSACTIONID, cursor.getLong(0));
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRODUCTID, 0);
                    cv.put(DBHelper.C_LOG_PRODUCTS_CATEGORY_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PROJECT_ID, -1);
                    cv.put(DBHelper.C_LOG_PRODUCTS_PRICE, cursor.getDouble(1));
                    cv.put(DBHelper.C_LOG_PRODUCTS_QUANTITY, 1);
                    db.insert(DBHelper.T_LOG_PRODUCTS, null, cv);
                    cursor.moveToNext();
                }
            }
        } finally {
            cursor.close();
        }
    }
}
 
Example 5
Source File: DBHelper.java    From fingen with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase db) {

    Log.d(TAG, SQL_CREATE_TABLE_REF_CURRENCIES);
    db.execSQL(SQL_CREATE_TABLE_REF_CURRENCIES);

    Cabbage cabbage;
    List<String> codes = Arrays.asList("RUB", "USD", "EUR", "UAH", "BYN", "KZT", "ABC");
    for (String code : codes) {
        cabbage = CabbageManager.createFromCode(code, mContext);
        if (cabbage != null) {
            db.insertOrThrow(T_REF_CURRENCIES, null, cabbage.getCV());
        }
    }

    Log.d(TAG, SQL_CREATE_TABLE_REF_ACCOUNTS);
    db.execSQL(SQL_CREATE_TABLE_REF_ACCOUNTS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_PROJECTS);
    db.execSQL(SQL_CREATE_TABLE_REF_PROJECTS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_DEPARTMENTS);
    db.execSQL(SQL_CREATE_TABLE_REF_DEPARTMENTS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_LOCATIONS);
    db.execSQL(SQL_CREATE_TABLE_REF_LOCATIONS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_CATEGORIES);
    db.execSQL(SQL_CREATE_TABLE_REF_CATEGORIES);

    Log.d(TAG, SQL_CREATE_TABLE_REF_PAYEES);
    db.execSQL(SQL_CREATE_TABLE_REF_PAYEES);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_TRANSACTIONS);
    db.execSQL(SQL_CREATE_TABLE_LOG_TRANSACTIONS);
    db.execSQL(I_LOG_TRANSACTIONS_IDX);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_INCOMING_SMS);
    db.execSQL(SQL_CREATE_TABLE_LOG_INCOMING_SMS);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_SMS_PARSER_PATTERNS);
    db.execSQL(SQL_CREATE_TABLE_LOG_SMS_PARSER_PATTERNS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_DEBTS);
    db.execSQL(SQL_CREATE_TABLE_REF_DEBTS);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_BUDGET);
    db.execSQL(SQL_CREATE_TABLE_LOG_BUDGET);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_BUDGET_DEBTS);
    db.execSQL(SQL_CREATE_TABLE_LOG_BUDGET_DEBTS);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_TEMPLATES);
    db.execSQL(SQL_CREATE_TABLE_LOG_TEMPLATES);

    Log.d(TAG, SQL_CREATE_TABLE_REF_SIMPLEDEBTS);
    db.execSQL(SQL_CREATE_TABLE_REF_SIMPLEDEBTS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_SENDERS);
    db.execSQL(SQL_CREATE_TABLE_REF_SENDERS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_ACCOUNTS_SETS);
    db.execSQL(SQL_CREATE_TABLE_REF_ACCOUNTS_SETS);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_ACCOUNTS_SETS);
    db.execSQL(SQL_CREATE_TABLE_LOG_ACCOUNTS_SETS);

    Log.d(TAG, SQL_CREATE_TABLE_REF_PRODUCTS);
    db.execSQL(SQL_CREATE_TABLE_REF_PRODUCTS);

    Log.d(TAG, SQL_CREATE_TABLE_LOG_PRODUCTS);
    db.execSQL(SQL_CREATE_TABLE_LOG_PRODUCTS);
    db.execSQL(I_LOG_PRODUCTS_IDX);

    ContentValues cv = new ContentValues();
    cv.put(C_ID, 0);
    cv.put(C_REF_PRODUCTS_NAME, "default_product");
    db.insert(T_REF_PRODUCTS, "", cv);

    db.execSQL(SQL_CREATE_TABLE_LOG_RB);
    db.execSQL(IDX_RB_ACCOUNTS);
    db.execSQL(IDX_RB_TRANSACTIONS);
    db.execSQL(IDX_RB_DATETIME);
}
 
Example 6
Source File: UpdateHelper.java    From fingen with Apache License 2.0 4 votes vote down vote up
public static void update18(SQLiteDatabase db, Context context) {
    //переходим со строкового хранения дат в таблице транзакций на числовое
    //Переименовали старую версию таблицы
    db.execSQL(String.format("ALTER TABLE %s RENAME TO t_transactions_old", "log_Transactions"));

    //Создали новую версию таблицы
    db.execSQL("CREATE TABLE log_Transactions (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, FBID TEXT, TS INTEGER, Deleted INTEGER, Dirty INTEGER, LastEdited TEXT, DateTime INTEGER NOT NULL, SrcAccount INTEGER NOT NULL REFERENCES ref_Accounts(_id) ON DELETE CASCADE ON UPDATE CASCADE, Payee INTEGER REFERENCES ref_Payees(_id) ON DELETE SET NULL ON UPDATE CASCADE, Category INTEGER REFERENCES ref_Categories(_id) ON DELETE SET NULL ON UPDATE CASCADE, Amount REAL NOT NULL, Project INTEGER REFERENCES ref_Projects(_id) ON DELETE SET NULL ON UPDATE CASCADE, SimpleDebt INTEGER REFERENCES ref_SimpleDebts(_id) ON DELETE SET NULL ON UPDATE CASCADE, Department INTEGER REFERENCES ref_Departments(_id) ON DELETE SET NULL ON UPDATE CASCADE, Location INTEGER REFERENCES ref_Locations(_id) ON DELETE SET NULL ON UPDATE CASCADE, Comment TEXT, File TEXT, DestAccount INTEGER NOT NULL REFERENCES ref_Accounts(_id) ON DELETE CASCADE ON UPDATE CASCADE, ExchangeRate REAL NOT NULL, AutoCreated INTEGER NOT NULL, Lon REAL, Lat REAL, Accuracy INTEGER);");

    Cursor cursor = db.query("t_transactions_old", "_id, DateTime, SrcAccount, Payee, Category, Amount, Project, Location, Comment, File, DestAccount, ExchangeRate, AutoCreated, Lat, Lon, Accuracy, Department, SimpleDebt".split(","), null, null, null, null, null);

    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                DateTimeFormatter dtf = DateTimeFormatter.getInstance(context);
                ContentValues values = new ContentValues();
                while (!cursor.isAfterLast()) {
                    values.clear();
                    values.put("DateTime", dtf.parseDateTimeSqlString(cursor.getString(cursor.getColumnIndex("DateTime"))).getTime());
                    values.put("SrcAccount", cursor.getLong(cursor.getColumnIndex("SrcAccount")));
                    values.put("Payee", cursor.getLong(cursor.getColumnIndex("Payee")));
                    values.put("Category", cursor.getLong(cursor.getColumnIndex("Category")));
                    values.put("Amount", cursor.getDouble(cursor.getColumnIndex("Amount")));
                    values.put("Project", cursor.getLong(cursor.getColumnIndex("Project")));
                    values.put("Department", cursor.getLong(cursor.getColumnIndex("Department")));
                    values.put("Location", cursor.getLong(cursor.getColumnIndex("Location")));
                    values.put("Comment", cursor.getString(cursor.getColumnIndex("Comment")));
                    values.put("File", cursor.getString(cursor.getColumnIndex("File")));
                    values.put("DestAccount", cursor.getLong(cursor.getColumnIndex("DestAccount")));
                    values.put("ExchangeRate", cursor.getDouble(cursor.getColumnIndex("ExchangeRate")));
                    values.put("AutoCreated", cursor.getInt(cursor.getColumnIndex("AutoCreated")) == 1);
                    if (!cursor.isNull(cursor.getColumnIndex("Lat"))) {
                        values.put("Lat", cursor.getDouble(cursor.getColumnIndex("Lat")));
                    } else {
                        values.put("Lat", 0);
                    }
                    if (!cursor.isNull(cursor.getColumnIndex("Lon"))) {
                        values.put("Lon", cursor.getDouble(cursor.getColumnIndex("Lon")));
                    } else {
                        values.put("Lon", 0);
                    }
                    if (!cursor.isNull(cursor.getColumnIndex("Accuracy"))) {
                        values.put("Accuracy", cursor.getInt(cursor.getColumnIndex("Accuracy")));
                    } else {
                        values.put("Accuracy", -1);
                    }
                    if (!cursor.isNull(cursor.getColumnIndex("SimpleDebt"))) {
                        values.put("SimpleDebt", cursor.getLong(cursor.getColumnIndex("SimpleDebt")));
                    } else {
                        values.put("SimpleDebt", -1);
                    }

                    db.insert("log_Transactions", null, values);

                    cursor.moveToNext();
                }
            }
        } finally {
            cursor.close();
        }
    }
    db.execSQL("DROP TABLE t_transactions_old");

    //создаем в таблицах поля, необходимые для работы синхронизации и меняем constraints (пересоздаем все таблицы)
    ContentValues cv = new ContentValues();
    cv.put("FBID", "");
    cv.put("TS", -1);
    cv.put("Deleted", 0);
    cv.put("Dirty", false);
    cv.put("LastEdited", "");
    for (int i = 0; i < tableInfo17.length; i++) {
        db.execSQL(String.format("ALTER TABLE %s RENAME TO %s_old", tableInfo17[i].getTableName(), tableInfo17[i].getTableName()));
        db.execSQL(tableInfo18[i].getTableCreateSQL());
        db.execSQL(String.format("INSERT INTO %s (%s) SELECT %s FROM %s_old", tableInfo18[i].getTableName(), tableInfo17[i].getTableFields(), tableInfo17[i].getTableFields(), tableInfo17[i].getTableName()));
        db.execSQL(String.format("DROP TABLE %s_old", tableInfo17[i].getTableName()));
        db.update(tableInfo18[i].getTableName(), cv, null, null);
    }
}