Example usage for android.database.sqlite SQLiteDatabase execSQL

List of usage examples for android.database.sqlite SQLiteDatabase execSQL

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase execSQL.

Prototype

public void execSQL(String sql) throws SQLException 

Source Link

Document

Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.

Usage

From source file:org.totschnig.myexpenses.provider.TransactionDatabase.java

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {//  ww  w. j  a  v a  2s  .c  om
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ".");
        if (oldVersion < 17) {
            db.execSQL("drop table accounts");
            db.execSQL("CREATE TABLE accounts (_id integer primary key autoincrement, label text not null, "
                    + "opening_balance integer, description text, currency text not null);");
            //db.execSQL("ALTER TABLE expenses add column account_id integer");
        }

        if (oldVersion < 18) {
            db.execSQL(
                    "CREATE TABLE payee (_id integer primary key autoincrement, name text unique not null);");
            db.execSQL("ALTER TABLE expenses add column payee text");
        }

        if (oldVersion < 19) {
            db.execSQL("ALTER TABLE expenses add column transfer_peer text");
        }

        if (oldVersion < 20) {
            db.execSQL(
                    "CREATE TABLE transactions ( _id integer primary key autoincrement, comment text not null, "
                            + "date datetime not null, amount integer not null, cat_id integer, account_id integer, "
                            + "payee  text, transfer_peer integer default null);");
            db.execSQL("INSERT INTO transactions (comment,date,amount,cat_id,account_id,payee,transfer_peer)"
                    + " SELECT comment,date,CAST(ROUND(amount*100) AS INTEGER),cat_id,account_id,payee,transfer_peer FROM expenses");
            db.execSQL("DROP TABLE expenses");
            db.execSQL("ALTER TABLE accounts RENAME to accounts_old");
            db.execSQL("CREATE TABLE accounts (_id integer primary key autoincrement, label text not null, "
                    + "opening_balance integer, description text, currency text not null);");
            db.execSQL("INSERT INTO accounts (label,opening_balance,description,currency)"
                    + " SELECT label,CAST(ROUND(opening_balance*100) AS INTEGER),description,currency FROM accounts_old");
            db.execSQL("DROP TABLE accounts_old");
        }

        if (oldVersion < 21) {
            db.execSQL(
                    "CREATE TABLE paymentmethods (_id integer primary key autoincrement, label text not null, type integer default 0);");
            db.execSQL(
                    "CREATE TABLE accounttype_paymentmethod (type text, method_id integer, primary key (type,method_id));");
            ContentValues initialValues;
            long _id;
            for (PaymentMethod.PreDefined pm : PaymentMethod.PreDefined.values()) {
                initialValues = new ContentValues();
                initialValues.put("label", pm.name());
                initialValues.put("type", pm.paymentType);
                _id = db.insert("paymentmethods", null, initialValues);
                initialValues = new ContentValues();
                initialValues.put("method_id", _id);
                initialValues.put("type", "BANK");
                db.insert("accounttype_paymentmethod", null, initialValues);
            }
            db.execSQL("ALTER TABLE transactions add column payment_method_id integer");
            db.execSQL("ALTER TABLE accounts add column type text default 'CASH'");
        }

        if (oldVersion < 22) {
            db.execSQL("CREATE TABLE templates ( _id integer primary key autoincrement, comment text not null, "
                    + "amount integer not null, cat_id integer, account_id integer, payee text, transfer_peer integer default null, "
                    + "payment_method_id integer, title text not null);");
        }

        if (oldVersion < 23) {
            db.execSQL("ALTER TABLE templates RENAME to templates_old");
            db.execSQL("CREATE TABLE templates ( _id integer primary key autoincrement, comment text not null, "
                    + "amount integer not null, cat_id integer, account_id integer, payee text, transfer_peer integer default null, "
                    + "payment_method_id integer, title text not null, unique(account_id, title));");
            try {
                db.execSQL(
                        "INSERT INTO templates(comment,amount,cat_id,account_id,payee,transfer_peer,payment_method_id,title)"
                                + " SELECT comment,amount,cat_id,account_id,payee,transfer_peer,payment_method_id,title FROM templates_old");
            } catch (SQLiteConstraintException e) {
                Log.e(TAG, e.getLocalizedMessage());
                //theoretically we could have entered duplicate titles for one account
                //we silently give up in that case (since this concerns only a narrowly distributed alpha version)
            }
            db.execSQL("DROP TABLE templates_old");
        }

        if (oldVersion < 24) {
            db.execSQL("ALTER TABLE templates add column usages integer default 0");
        }

        if (oldVersion < 25) {
            //for transactions that were not transfers, transfer_peer was set to null in transactions, but to 0 in templates
            db.execSQL("update transactions set transfer_peer=0 WHERE transfer_peer is null;");
        }

        if (oldVersion < 26) {
            db.execSQL("alter table accounts add column color integer default -6697984");
        }

        if (oldVersion < 27) {
            db.execSQL("CREATE TABLE feature_used (feature text not null);");
        }

        if (oldVersion < 28) {
            db.execSQL("ALTER TABLE transactions RENAME to transactions_old");
            db.execSQL(
                    "CREATE TABLE transactions(_id integer primary key autoincrement, comment text, date datetime not null, amount integer not null, "
                            + "cat_id integer references categories(_id), account_id integer not null references accounts(_id),payee text, "
                            + "transfer_peer integer references transactions(_id), transfer_account integer references accounts(_id), "
                            + "method_id integer references paymentmethods(_id));");
            db.execSQL(
                    "INSERT INTO transactions (_id,comment,date,amount,cat_id,account_id,payee,transfer_peer,transfer_account,method_id) "
                            + "SELECT _id,comment,date,amount, "
                            + "CASE WHEN transfer_peer THEN null ELSE CASE WHEN cat_id THEN cat_id ELSE null END END, "
                            + "account_id,payee, "
                            + "CASE WHEN transfer_peer THEN transfer_peer ELSE null END, "
                            + "CASE WHEN transfer_peer THEN cat_id ELSE null END, "
                            + "CASE WHEN payment_method_id THEN payment_method_id ELSE null END "
                            + "FROM transactions_old");
            db.execSQL("ALTER TABLE accounts RENAME to accounts_old");
            db.execSQL(
                    "CREATE TABLE accounts (_id integer primary key autoincrement, label text not null, opening_balance integer, description text, "
                            + "currency text not null, type text not null check (type in ('CASH','BANK','CCARD','ASSET','LIABILITY')) default 'CASH', color integer default -3355444);");
            db.execSQL("INSERT INTO accounts (_id,label,opening_balance,description,currency,type,color) "
                    + "SELECT _id,label,opening_balance,description,currency,type,color FROM accounts_old");
            //previously templates where not deleted if referred to accounts were deleted
            db.execSQL(
                    "DELETE FROM templates where account_id not in (SELECT _id FROM accounts) or (cat_id != 0 and transfer_peer = 1 and cat_id not in (SELECT _id from accounts))");
            db.execSQL("ALTER TABLE templates RENAME to templates_old");
            db.execSQL(
                    "CREATE TABLE templates ( _id integer primary key autoincrement, comment text not null, amount integer not null, "
                            + "cat_id integer references categories(_id), account_id integer not null references accounts(_id),payee text, "
                            + "transfer_peer boolean default false, transfer_account integer references accounts(_id),method_id integer references paymentmethods(_id), "
                            + "title text not null, usages integer default 0, unique(account_id,title));");
            db.execSQL(
                    "INSERT INTO templates (_id,comment,amount,cat_id,account_id,payee,transfer_peer,transfer_account,method_id,title,usages) "
                            + "SELECT _id,comment,amount,"
                            + "CASE WHEN transfer_peer THEN null ELSE CASE WHEN cat_id THEN cat_id ELSE null END END, "
                            + "account_id,payee, " + "CASE WHEN transfer_peer THEN 1 ELSE 0 END, "
                            + "CASE WHEN transfer_peer THEN cat_id ELSE null END, "
                            + "CASE WHEN payment_method_id THEN payment_method_id ELSE null END, "
                            + "title,usages FROM templates_old");
            db.execSQL("ALTER TABLE categories RENAME to categories_old");
            db.execSQL(
                    "CREATE TABLE categories (_id integer primary key autoincrement, label text not null, parent_id integer references categories(_id), "
                            + "usages integer default 0, unique (label,parent_id));");
            db.execSQL("INSERT INTO categories (_id,label,parent_id,usages) "
                    + "SELECT _id,label,CASE WHEN parent_id THEN parent_id ELSE null END,usages FROM categories_old");
            db.execSQL("ALTER TABLE paymentmethods RENAME to paymentmethods_old");
            db.execSQL(
                    "CREATE TABLE paymentmethods (_id integer primary key autoincrement, label text not null, type integer check (type in (-1,0,1)) default 0);");
            db.execSQL(
                    "INSERT INTO paymentmethods (_id,label,type) SELECT _id,label,type FROM paymentmethods_old");
            db.execSQL("ALTER TABLE accounttype_paymentmethod RENAME to accounttype_paymentmethod_old");
            db.execSQL(
                    "CREATE TABLE accounttype_paymentmethod (type text not null check (type in ('CASH','BANK','CCARD','ASSET','LIABILITY')), method_id integer references paymentmethods (_id), primary key (type,method_id));");
            db.execSQL(
                    "INSERT INTO accounttype_paymentmethod (type,method_id) SELECT type,method_id FROM accounttype_paymentmethod_old");
            db.execSQL("DROP TABLE transactions_old");
            db.execSQL("DROP TABLE accounts_old");
            db.execSQL("DROP TABLE templates_old");
            db.execSQL("DROP TABLE categories_old");
            db.execSQL("DROP TABLE paymentmethods_old");
            db.execSQL("DROP TABLE accounttype_paymentmethod_old");
            //Changes to handle
            //1) Transfer account no longer stored as cat_id but in transfer_account (in transactions and templates)
            //2) parent_id for categories uses foreign key on itself, hence root categories have null instead of 0 as parent_id
            //3) catId etc now need to be null instead of 0
            //4) transactions payment_method_id renamed to method_id
        }

        if (oldVersion < 29) {
            db.execSQL("ALTER TABLE transactions add column status integer default 0");
        }

        if (oldVersion < 30) {
            db.execSQL("ALTER TABLE transactions add column parent_id integer references transactions (_id)");
            //      db.execSQL("CREATE VIEW committed AS SELECT * FROM transactions WHERE status != 2;");
            //      db.execSQL("CREATE VIEW uncommitted AS SELECT * FROM transactions WHERE status = 2;");
            ContentValues initialValues = new ContentValues();
            initialValues.put("_id", 0);
            initialValues.put("parent_id", 0);
            initialValues.put("label", "__SPLIT_TRANSACTION__");
            db.insert("categories", null, initialValues);
        }

        if (oldVersion < 31) {
            //in an alpha version distributed on Google Play, we had SPLIT_CATID as -1
            ContentValues initialValues = new ContentValues();
            initialValues.put("_id", 0);
            initialValues.put("parent_id", 0);
            db.update("categories", initialValues, "_id=-1", null);
        }

        if (oldVersion < 32) {
            db.execSQL("ALTER TABLE accounts add column grouping text not null check (grouping in "
                    + "('NONE','DAY','WEEK','MONTH','YEAR')) default 'NONE'");
        }

        if (oldVersion < 33) {
            db.execSQL("ALTER TABLE accounts add column usages integer default 0");
            db.execSQL(
                    "UPDATE accounts SET usages = (SELECT count(*) FROM transactions WHERE account_id = accounts._id AND parent_id IS null)");
        }

        if (oldVersion < 34) {
            //fix for https://github.com/mtotschnig/MyExpenses/issues/69
            db.execSQL(
                    "UPDATE transactions set date = (SELECT date from transactions parent WHERE parent._id = transactions.parent_id) WHERE parent_id IS NOT null");
        }

        if (oldVersion < 35) {
            db.execSQL(
                    "ALTER TABLE transactions add column cr_status text not null check (cr_status in ('UNRECONCILED','CLEARED','RECONCILED')) default 'UNRECONCILED'");
        }

        if (oldVersion < 36) {
            //move payee field in transactions from text to foreign key
            db.execSQL("ALTER TABLE transactions RENAME to transactions_old");
            db.execSQL("CREATE TABLE transactions (" + " _id integer primary key autoincrement,"
                    + " comment text, date datetime not null," + " amount integer not null,"
                    + " cat_id integer references categories(_id),"
                    + " account_id integer not null references accounts(_id),"
                    + " payee_id integer references payee(_id),"
                    + " transfer_peer integer references transactions(_id),"
                    + " transfer_account integer references accounts(_id),"
                    + " method_id integer references paymentmethods(_id),"
                    + " parent_id integer references transactions(_id)," + " status integer default 0,"
                    + " cr_status text not null check (cr_status in ('UNRECONCILED','CLEARED','RECONCILED')) default 'RECONCILED')");
            //insert all payees that are stored in transactions, but are not in payee
            db.execSQL(
                    "INSERT INTO payee (name) SELECT DISTINCT payee FROM transactions_old WHERE payee != '' AND NOT exists (SELECT 1 FROM payee WHERE name=transactions_old.payee)");
            db.execSQL("INSERT INTO transactions "
                    + "(_id,comment,date,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,parent_id,status,cr_status) "
                    + "SELECT " + "_id, " + "comment, " + "date, " + "amount, " + "cat_id, " + "account_id, "
                    + "(SELECT _id from payee WHERE name = payee), " + "transfer_peer, " + "transfer_account, "
                    + "method_id," + "parent_id," + "status," + "cr_status " + "FROM transactions_old");
            db.execSQL("DROP TABLE transactions_old");

            //move payee field in templates from text to foreign key
            db.execSQL("ALTER TABLE templates RENAME to templates_old");
            db.execSQL("CREATE TABLE templates (" + " _id integer primary key autoincrement," + " comment text,"
                    + " amount integer not null," + " cat_id integer references categories(_id),"
                    + " account_id integer not null references accounts(_id),"
                    + " payee_id integer references payee(_id)," + " transfer_peer boolean default false,"
                    + " transfer_account integer references accounts(_id),"
                    + " method_id integer references paymentmethods(_id)," + " title text not null,"
                    + " usages integer default 0," + " unique(account_id,title));");
            //insert all payees that are stored in templates, but are not in payee
            db.execSQL(
                    "INSERT INTO payee (name) SELECT DISTINCT payee FROM templates_old WHERE payee != '' AND NOT exists (SELECT 1 FROM payee WHERE name=templates_old.payee)");
            db.execSQL("INSERT INTO templates "
                    + "(_id,comment,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,title,usages) "
                    + "SELECT " + "_id, " + "comment, " + "amount, " + "cat_id, " + "account_id, "
                    + "(SELECT _id from payee WHERE name = payee), " + "transfer_peer, " + "transfer_account, "
                    + "method_id," + "title," + "usages " + "FROM templates_old");
            db.execSQL("DROP TABLE templates_old");

            db.execSQL("DROP VIEW IF EXISTS committed");
            db.execSQL("DROP VIEW IF EXISTS uncommitted");
            //for the definition of the view, it is safe to rely on the constants,
            //since we will not alter the view, but drop it, and recreate it, if needed
            //      String viewTransactions = VIEW_DEFINITION(TABLE_TRANSACTIONS);
            //      db.execSQL("CREATE VIEW transactions_committed "  + viewTransactions + " WHERE " + KEY_STATUS + " != " + STATUS_UNCOMMITTED + ";");
            //      db.execSQL("CREATE VIEW transactions_uncommitted" + viewTransactions + " WHERE " + KEY_STATUS +  " = " + STATUS_UNCOMMITTED + ";");
            //      db.execSQL("CREATE VIEW transactions_all" + viewTransactions);
            //      db.execSQL("CREATE VIEW templates_all" +  VIEW_DEFINITION(TABLE_TEMPLATES));
        }

        if (oldVersion < 37) {
            db.execSQL("ALTER TABLE transactions add column number text");
            db.execSQL("ALTER TABLE paymentmethods add column is_numbered boolean default 0");
            ContentValues initialValues = new ContentValues();
            initialValues.put("is_numbered", true);
            db.update("paymentmethods", initialValues, "label = ?", new String[] { "CHEQUE" });
        }

        if (oldVersion < 38) {
            db.execSQL("ALTER TABLE templates add column plan_id integer");
            db.execSQL("ALTER TABLE templates add column plan_execution boolean default 0");
        }

        if (oldVersion < 39) {
            //      db.execSQL("CREATE VIEW transactions_extended" + VIEW_DEFINITION_EXTENDED(TABLE_TRANSACTIONS) + " WHERE " + KEY_STATUS + " != " + STATUS_UNCOMMITTED + ";");
            //      db.execSQL("CREATE VIEW templates_extended" +  VIEW_DEFINITION_EXTENDED(TABLE_TEMPLATES));
            db.execSQL(
                    "CREATE TABLE currency (_id integer primary key autoincrement, code text unique not null);");
            insertCurrencies(db);
        }

        if (oldVersion < 40) {
            //added currency to extended view
            db.execSQL("DROP VIEW IF EXISTS transactions_extended");
            db.execSQL("DROP VIEW IF EXISTS templates_extended");
            //      db.execSQL("CREATE VIEW transactions_extended" + VIEW_DEFINITION_EXTENDED(TABLE_TRANSACTIONS) + " WHERE " + KEY_STATUS + " != " + STATUS_UNCOMMITTED + ";");
            //      db.execSQL("CREATE VIEW templates_extended" +  VIEW_DEFINITION_EXTENDED(TABLE_TEMPLATES));
        }

        if (oldVersion < 41) {
            db.execSQL("CREATE TABLE planinstance_transaction "
                    + "(template_id integer references templates(_id), " + "instance_id integer, "
                    + "transaction_id integer references transactions(_id), "
                    + "primary key (instance_id,transaction_id));");
        }

        if (oldVersion < 42) {
            //migrate date field to unix time stamp (UTC)
            db.execSQL("ALTER TABLE transactions RENAME to transactions_old");
            db.execSQL("CREATE TABLE transactions (" + " _id integer primary key autoincrement,"
                    + " comment text, date datetime not null," + " amount integer not null,"
                    + " cat_id integer references categories(_id),"
                    + " account_id integer not null references accounts(_id),"
                    + " payee_id integer references payee(_id),"
                    + " transfer_peer integer references transactions(_id),"
                    + " transfer_account integer references accounts(_id),"
                    + " method_id integer references paymentmethods(_id),"
                    + " parent_id integer references transactions(_id)," + " status integer default 0,"
                    + " cr_status text not null check (cr_status in ('UNRECONCILED','CLEARED','RECONCILED')) default 'RECONCILED',"
                    + " number text)");
            db.execSQL("INSERT INTO transactions "
                    + "(_id,comment,date,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,parent_id,status,cr_status,number) "
                    + "SELECT " + "_id, " + "comment, " + "strftime('%s',date,'utc'), " + "amount, "
                    + "cat_id, " + "account_id, " + "payee_id, " + "transfer_peer, " + "transfer_account, "
                    + "method_id," + "parent_id," + "status," + "cr_status, " + "number "
                    + "FROM transactions_old");
            db.execSQL("DROP TABLE transactions_old");
        }

        if (oldVersion < 43) {
            db.execSQL("UPDATE accounts set currency = 'ZMW' WHERE currency = 'ZMK'");
            db.execSQL("UPDATE currency set code = 'ZMW' WHERE code = 'ZMK'");
        }

        if (oldVersion < 44) {
            //add ON DELETE CASCADE
            //accounts table sort_key column
            db.execSQL("ALTER TABLE planinstance_transaction RENAME to planinstance_transaction_old");
            db.execSQL("CREATE TABLE planinstance_transaction "
                    + "(template_id integer references templates(_id) ON DELETE CASCADE, "
                    + "instance_id integer, "
                    + "transaction_id integer references transactions(_id) ON DELETE CASCADE, "
                    + "primary key (instance_id,transaction_id));");
            db.execSQL("INSERT INTO planinstance_transaction " + "(template_id,instance_id,transaction_id)"
                    + "SELECT " + "template_id,instance_id,transaction_id FROM planinstance_transaction_old");
            db.execSQL("DROP TABLE planinstance_transaction_old");
            db.execSQL("ALTER TABLE transactions RENAME to transactions_old");
            db.execSQL("CREATE TABLE transactions (" + " _id integer primary key autoincrement,"
                    + " comment text, date datetime not null," + " amount integer not null,"
                    + " cat_id integer references categories(_id),"
                    + " account_id integer not null references accounts(_id) ON DELETE CASCADE,"
                    + " payee_id integer references payee(_id),"
                    + " transfer_peer integer references transactions(_id),"
                    + " transfer_account integer references accounts(_id),"
                    + " method_id integer references paymentmethods(_id),"
                    + " parent_id integer references transactions(_id) ON DELETE CASCADE,"
                    + " status integer default 0,"
                    + " cr_status text not null check (cr_status in ('UNRECONCILED','CLEARED','RECONCILED')) default 'RECONCILED',"
                    + " number text)");
            db.execSQL("INSERT INTO transactions "
                    + "(_id,comment,date,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,parent_id,status,cr_status,number) "
                    + "SELECT " + "_id, " + "comment, " + "date, " + "amount, " + "cat_id, " + "account_id, "
                    + "payee_id, " + "transfer_peer, " + "transfer_account, " + "method_id," + "parent_id,"
                    + "status," + "cr_status, " + "number " + "FROM transactions_old");
            db.execSQL("DROP TABLE transactions_old");
            db.execSQL("ALTER TABLE templates RENAME to templates_old");
            db.execSQL("CREATE TABLE templates (" + " _id integer primary key autoincrement," + " comment text,"
                    + " amount integer not null," + " cat_id integer references categories(_id),"
                    + " account_id integer not null references accounts(_id) ON DELETE CASCADE,"
                    + " payee_id integer references payee(_id)," + " transfer_peer boolean default 0,"
                    + " transfer_account integer references accounts(_id) ON DELETE CASCADE,"
                    + " method_id integer references paymentmethods(_id)," + " title text not null,"
                    + " usages integer default 0," + " plan_id integer, "
                    + " plan_execution boolean default 0, " + " unique(account_id,title));");
            db.execSQL("INSERT INTO templates "
                    + "(_id,comment,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,title,usages,plan_id,plan_execution) "
                    + "SELECT " + "_id, " + "comment, " + "amount, " + "cat_id, " + "account_id, "
                    + "payee_id, " + "transfer_peer, " + "transfer_account, " + "method_id," + "title,"
                    + "usages, " + "plan_id, " + "plan_execution " + "FROM templates_old");
            db.execSQL("ALTER TABLE accounts add column sort_key integer");
        }

        if (oldVersion < 45) {
            db.execSQL("ALTER TABLE accounts add column exclude_from_totals boolean default 0");
            //added  to extended view
            db.execSQL("DROP VIEW IF EXISTS transactions_extended");
            db.execSQL("DROP VIEW IF EXISTS templates_extended");
            //      db.execSQL("CREATE VIEW transactions_extended" + VIEW_DEFINITION_EXTENDED(TABLE_TRANSACTIONS) + " WHERE " + KEY_STATUS + " != " + STATUS_UNCOMMITTED + ";");
            //      db.execSQL("CREATE VIEW templates_extended" +  VIEW_DEFINITION_EXTENDED(TABLE_TEMPLATES));
        }

        if (oldVersion < 46) {
            db.execSQL("ALTER TABLE payee add column name_normalized text");
            Cursor c = db.query("payee", new String[] { "_id", "name" }, null, null, null, null, null);
            if (c != null) {
                if (c.moveToFirst()) {
                    ContentValues v = new ContentValues();
                    while (c.getPosition() < c.getCount()) {
                        v.put("name_normalized", Utils.normalize(c.getString(1)));
                        db.update("payee", v, "_id = " + c.getLong(0), null);
                        c.moveToNext();
                    }
                }
                c.close();
            }
        }

        if (oldVersion < 47) {
            db.execSQL("ALTER TABLE templates add column uuid text");
            db.execSQL(EVENT_CACHE_CREATE);
        }

        if (oldVersion < 48) {
            //added method_label to extended view
            //do not comment out, since it is needed by the uuid update
            refreshViews(db);
            //need to inline to protect against later renames

            if (oldVersion < 47) {
                String[] projection = new String[] { "templates._id", "amount", "comment", "cat_id",
                        "CASE WHEN " + "  " + "transfer_peer" + " " + " THEN " + "  (SELECT " + "label"
                                + " FROM " + "accounts" + " WHERE " + "_id" + " = " + "transfer_account" + ") "
                                + " ELSE " + " CASE WHEN " + " (SELECT " + "parent_id" + " FROM " + "categories"
                                + " WHERE " + "_id" + " = " + "cat_id" + ") " + " THEN " + " (SELECT " + "label"
                                + " FROM " + "categories" + " WHERE " + "_id" + " = " + " (SELECT "
                                + "parent_id" + " FROM " + "categories" + " WHERE " + "_id" + " = " + "cat_id"
                                + ")) " + "  || ' : ' || " + " (SELECT " + "label" + " FROM " + "categories"
                                + " WHERE " + "_id" + " = " + "cat_id" + ") " + " ELSE" + " (SELECT " + "label"
                                + " FROM " + "categories" + " WHERE " + "_id" + " = " + "cat_id" + ") "
                                + " END " + " END AS  " + "label",
                        "name", "transfer_peer", "transfer_account", "account_id", "method_id",
                        "paymentmethods.label AS method_label", "title", "plan_id", "plan_execution", "uuid",
                        "currency" };
                SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                qb.setTables("templates LEFT JOIN payee ON payee_id = payee._id"
                        + " LEFT JOIN accounts ON account_id = accounts._id"
                        + " LEFT JOIN paymentmethods ON method_id = paymentmethods._id");
                Cursor c = qb.query(db, projection, null, null, null, null, null);
                if (c != null) {
                    if (c.moveToFirst()) {
                        ContentValues templateValues = new ContentValues(), eventValues = new ContentValues();
                        String planCalendarId = MyApplication.getInstance().checkPlanner();
                        while (c.getPosition() < c.getCount()) {
                            Template t = new Template(c);
                            templateValues.put(DatabaseConstants.KEY_UUID, t.getUuid());
                            long templateId = c.getLong(c.getColumnIndex("_id"));
                            long planId = c.getLong(c.getColumnIndex("plan_id"));
                            eventValues.put(Events.DESCRIPTION, t.compileDescription(mCtx));
                            db.update("templates", templateValues, "_id = " + templateId, null);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                                try {
                                    mCtx.getContentResolver().update(Events.CONTENT_URI, eventValues,
                                            Events._ID + "= ? AND " + Events.CALENDAR_ID + " = ?",
                                            new String[] { String.valueOf(planId), planCalendarId });
                                } catch (Exception e) {
                                    //fails with IllegalArgumentException on 2.x devices,
                                    //since the same uri works for inserting and querying
                                    //but also on HUAWEI Y530-U00 with 4.3
                                    //probably SecurityException could arise here
                                }
                            }
                            c.moveToNext();
                        }
                    }
                    c.close();
                }
            }
        }

        if (oldVersion < 49) {
            //forgotten to drop in previous upgrade
            db.execSQL("DROP TABLE IF EXISTS templates_old");
        }

        if (oldVersion < 50) {
            db.execSQL("ALTER TABLE transactions add column picture_id text");
            db.execSQL("DROP TABLE IF EXISTS feature_used");
        }

        if (oldVersion < 51) {
            File pictureDir = Utils.getPictureDir(false);
            //fallback if not mounted
            if (pictureDir == null) {
                pictureDir = new File(
                        Environment.getExternalStorageDirectory().getPath() + "/Android/data/"
                                + MyApplication.getInstance().getPackageName() + "/files",
                        Environment.DIRECTORY_PICTURES);
            }
            if (!pictureDir.exists()) {
                AcraHelper.report(new Exception("Unable to calculate pictureDir during upgrade"));
            }
            //if pictureDir does not exist, we use its URI nonetheless, in order to have the data around
            //for potential trouble handling
            String prefix = Uri.fromFile(pictureDir).toString() + "/";
            String postfix = ".jpg";
            //if picture_id concat expression will also be null
            db.execSQL("UPDATE transactions set picture_id = '" + prefix + "'||picture_id||'" + postfix + "'");

            db.execSQL("CREATE TABLE stale_uris ( picture_id text);");
            db.execSQL(
                    "CREATE TRIGGER cache_stale_uri BEFORE DELETE ON transactions WHEN old.picture_id NOT NULL "
                            + " BEGIN INSERT INTO stale_uris VALUES (old.picture_id); END");
        }

        if (oldVersion < 52) {
            db.execSQL("CREATE INDEX transactions_cat_id_index on transactions(cat_id)");
            db.execSQL("CREATE INDEX templates_cat_id_index on templates(cat_id)");
        }

        if (oldVersion < 53) {
            //add VOID status
            db.execSQL("ALTER TABLE transactions RENAME to transactions_old");
            db.execSQL("CREATE TABLE " + "transactions" + "( " + "_id" + " integer primary key autoincrement, "
                    + "comment" + " text, " + "date" + " datetime not null, " + "amount" + " integer not null, "
                    + "cat_id" + " integer references " + "categories" + "(" + "_id" + "), " + "account_id"
                    + " integer not null references " + "accounts" + "(" + "_id" + ") ON DELETE CASCADE,"
                    + "payee_id" + " integer references " + "payee" + "(" + "_id" + "), " + "transfer_peer"
                    + " integer references " + "transactions" + "(" + "_id" + "), " + "transfer_account"
                    + " integer references " + "accounts" + "(" + "_id" + ")," + "method_id"
                    + " integer references " + "paymentmethods" + "(" + "_id" + ")," + "parent_id"
                    + " integer references " + "transactions" + "(" + "_id" + ") ON DELETE CASCADE, " + "status"
                    + " integer default 0, " + "cr_status" + " text not null check (" + "cr_status"
                    + " in ('UNRECONCILED','CLEARED','RECONCILED','VOID')) default 'RECONCILED', " + "number"
                    + " text, " + "picture_id" + " text);");
            db.execSQL("INSERT INTO transactions "
                    + "(_id,comment,date,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,parent_id,status,cr_status,number,picture_id) "
                    + "SELECT " + "_id, " + "comment, " + "date, " + "amount, " + "cat_id, " + "account_id, "
                    + "payee_id, " + "transfer_peer, " + "transfer_account, " + "method_id," + "parent_id,"
                    + "status," + "cr_status, " + "number, " + "picture_id " + "FROM transactions_old");
            db.execSQL("DROP TABLE transactions_old");
            db.execSQL(
                    "CREATE TRIGGER cache_stale_uri BEFORE DELETE ON transactions WHEN old.picture_id NOT NULL "
                            + " BEGIN INSERT INTO stale_uris VALUES (old.picture_id); END");
            db.execSQL("CREATE INDEX transactions_cat_id_index on transactions(cat_id)");
        }

        if (oldVersion < 54) {
            db.execSQL("DROP TRIGGER cache_stale_uri");
            db.execSQL("CREATE TRIGGER cache_stale_uri " + "AFTER DELETE ON " + "transactions" + " "
                    + "WHEN old." + "picture_id" + " NOT NULL " + "AND NOT EXISTS " + "(SELECT 1 FROM "
                    + "transactions" + " " + "WHERE " + "picture_id" + " = old." + "picture_id" + ") "
                    + "BEGIN INSERT INTO " + "stale_uris" + " VALUES (old." + "picture_id" + "); END");
            //all Accounts with old default color are updated to the new one
            db.execSQL(String.format(Locale.US, "UPDATE accounts set color = %d WHERE color = %d", 0xff009688,
                    0xff99CC00));
        }

        if (oldVersion < 55) {
            db.execSQL("ALTER TABLE categories add column label_normalized text");
            Cursor c = db.query("categories", new String[] { "_id", "label" }, null, null, null, null, null);
            if (c != null) {
                if (c.moveToFirst()) {
                    ContentValues v = new ContentValues();
                    while (c.getPosition() < c.getCount()) {
                        v.put("label_normalized", Utils.normalize(c.getString(1)));
                        db.update("categories", v, "_id = " + c.getLong(0), null);
                        c.moveToNext();
                    }
                }
                c.close();
            }
        }

        if (oldVersion < 56) {
            db.execSQL("ALTER TABLE templates add column last_used datetime");
            db.execSQL("ALTER TABLE categories add column last_used datetime");
            db.execSQL("ALTER TABLE accounts add column last_used datetime");
            db.execSQL("CREATE TRIGGER sort_key_default AFTER INSERT ON accounts "
                    + "BEGIN UPDATE accounts SET sort_key = (SELECT coalesce(max(sort_key),0) FROM accounts) + 1 "
                    + "WHERE _id = NEW._id; END");
            //The sort key could be set by user in previous versions, now it is handled internally
            Cursor c = db.query("accounts", new String[] { "_id", "sort_key" }, null, null, null, null,
                    "sort_key ASC");
            boolean hasAccountSortKeySet = false;
            if (c != null) {
                if (c.moveToFirst()) {
                    ContentValues v = new ContentValues();
                    while (c.getPosition() < c.getCount()) {
                        v.put("sort_key", c.getPosition() + 1);
                        db.update("accounts", v, "_id = ?", new String[] { c.getString(0) });
                        if (c.getInt(1) != 0)
                            hasAccountSortKeySet = true;
                        c.moveToNext();
                    }
                }
                c.close();
            }
            String legacy = PrefKey.SORT_ORDER_LEGACY.getString("USAGES");
            PrefKey.SORT_ORDER_TEMPLATES.putString(legacy);
            PrefKey.SORT_ORDER_CATEGORIES.putString(legacy);
            PrefKey.SORT_ORDER_ACCOUNTS.putString(hasAccountSortKeySet ? "CUSTOM" : legacy);
            PrefKey.SORT_ORDER_LEGACY.remove();
        }
    } catch (SQLException e) {
        throw Utils.hasApiLevel(Build.VERSION_CODES.JELLY_BEAN)
                ? new SQLiteUpgradeFailedException("Database upgrade failed", e)
                : e;
    }

    if (oldVersion < 57) {
        //fix custom app uris
        if (ContextCompat.checkSelfPermission(mCtx,
                Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
            Cursor c = db.query("templates", new String[] { "_id", "plan_id" }, "plan_id IS NOT null", null,
                    null, null, null);
            if (c != null) {
                if (c.moveToFirst()) {
                    while (!c.isAfterLast()) {
                        Plan.updateCustomAppUri(c.getLong(1), Template.buildCustomAppUri(c.getLong(0)));
                        c.moveToNext();
                    }
                }
                c.close();
            }
        }

        //Drop unique constraint on templates

        db.execSQL("ALTER TABLE templates RENAME to templates_old");
        db.execSQL("CREATE TABLE templates (" + " _id integer primary key autoincrement," + " comment text,"
                + " amount integer not null," + " cat_id integer references categories(_id),"
                + " account_id integer not null references accounts(_id) ON DELETE CASCADE,"
                + " payee_id integer references payee(_id)," + " transfer_peer boolean default 0,"
                + " transfer_account integer references accounts(_id) ON DELETE CASCADE,"
                + " method_id integer references paymentmethods(_id)," + " title text not null,"
                + " usages integer default 0," + " plan_id integer, " + " plan_execution boolean default 0, "
                + " uuid text, " + " last_used datetime);");
        db.execSQL("INSERT INTO templates "
                + "(_id,comment,amount,cat_id,account_id,payee_id,transfer_peer,transfer_account,method_id,title,usages,plan_id,plan_execution,uuid,last_used) "
                + "SELECT " + "_id, " + "comment, " + "amount, " + "cat_id, " + "account_id, " + "payee_id, "
                + "transfer_peer, " + "transfer_account, " + "method_id," + "title," + "usages, " + "plan_id, "
                + "plan_execution, uuid, last_used " + "FROM templates_old");
        db.execSQL("DROP TABLE templates_old");
        //Recreate changed views
        refreshViews(db);
    }

    if (oldVersion < 58) {
        //cache fraction digits
        Cursor c = db.rawQuery("SELECT distinct currency from accounts", null);
        if (c != null) {
            if (c.moveToFirst()) {
                while (!c.isAfterLast()) {
                    Money.ensureFractionDigitsAreCached(Utils.getSaveInstance(c.getString(0)));
                    c.moveToNext();
                }
            }
            c.close();
        }
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java

private void dropAll(SQLiteDatabase db) {
    db.execSQL("DROP TABLE IF EXISTS " + MyInfo.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + DbObject.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + Contact.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + Subscriber.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + Group.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + GroupMember.TABLE);
    db.execSQL("DROP TABLE IF EXISTS " + DbRelation.TABLE);
}

From source file:org.cgiar.ilri.odk.pull.backend.services.FetchFormDataService.java

/**
 * Dumps data provided the rows variable into the specified database. The location of the database
 * is in the form's media folder in ODK's SDCard's folder.
 *
 * Indexes in {@param rows} are expected to correspond to rows in {@param org.cgiar.ilri.odk.pull.backend.carriers.Form.DB_DATA_TABLE} for {@param fileName}.
 * Each JSONArray element should be a JSONObject with children being column values (with keys being column names).
 * Make sure all JSONObjects in the JSONArray have the same number of key-value pairs.
 *
 * @param fileName  Then name to be given to the Database (without the .db suffix)
 * @param rows      The {@link org.json.JSONArray} object containing the data
 * @return  TRUE if database created successfully
 *//*  www .  j a v  a  2  s  . co m*/
private boolean saveDataInDb(String fileName, JSONArray rows) {
    boolean result = false;
    //TODO: only do this if ODK Collect is not using this file
    String pathToFile = Form.BASE_ODK_LOCATION + formName + Form.EXTERNAL_ITEM_SET_SUFFIX;
    /*File existingDb = new File(pathToFile+File.separator+fileName+Form.SUFFIX_DB);
    existingDb.delete();*/
    final DatabaseHelper databaseHelper = new DatabaseHelper(this, fileName, 1, pathToFile);
    SQLiteDatabase db = null;
    try {
        db = databaseHelper.getWritableDatabase();
    } catch (SQLiteException e) {//probably because the existing .db file is corrupt
        e.printStackTrace();
        Log.w(TAG, "Unable to open database in " + pathToFile + File.separator + fileName + Form.SUFFIX_DB
                + " most likely because the database is corrupt. Trying to recreate db file");
        File existingDbFile = new File(pathToFile + File.separator + fileName + Form.SUFFIX_DB);
        existingDbFile.delete();
        File existingDbJournalFile = new File(
                pathToFile + File.separator + fileName + Form.SUFFIX_DB + Form.SUFFIX_JOURNAL);
        existingDbJournalFile.delete();
        try {
            db = databaseHelper.getWritableDatabase();
        } catch (SQLiteException e1) {
            Log.e(TAG,
                    "Unable to recreate " + pathToFile + File.separator + fileName + Form.SUFFIX_DB + "  file");
            e1.printStackTrace();
        }
    }
    if (rows.length() > 0 && db != null) {
        try {
            List<String> columns = new ArrayList<String>();
            List<String> indexes = new ArrayList<String>();
            Iterator<String> iterator = rows.getJSONObject(0).keys();
            //recreate the tables
            db.execSQL("drop table if exists " + Form.DB_METADATA_TABLE);
            String createMetaTableString = "create table " + Form.DB_METADATA_TABLE + " ("
                    + Form.DB_META_LOCALE_FIELD + " " + Form.DB_META_LOCALE_FIELD_TYPE + ")";
            db.execSQL(createMetaTableString);
            databaseHelper.runInsertQuery(Form.DB_METADATA_TABLE, new String[] { Form.DB_META_LOCALE_FIELD },
                    new String[] { Form.DB_DEFAULT_LOCALE }, -1, db);
            db.execSQL("drop table if exists " + Form.DB_DATA_TABLE);
            String createTableString = "create table " + Form.DB_DATA_TABLE + " (";
            while (iterator.hasNext()) {
                String currKey = iterator.next();
                if (columns.size() > 0) {//this is the first column
                    createTableString = createTableString + ", ";
                }
                createTableString = createTableString + Form.DB_DATA_COLUMN_PREFIX + currKey + " "
                        + Form.DB_DATA_COLUMN_TYPE;
                columns.add(currKey);
                if (currKey.endsWith(Form.SUFFIX_INDEX_FIELD)) {
                    Log.d(TAG, fileName + " has an index column " + currKey);
                    indexes.add(currKey);
                }
            }
            //only continue if we have at least one column
            if (columns.size() > 0) {
                createTableString = createTableString + ", " + Form.DB_DATA_SORT_FIELD + " "
                        + Form.DB_DATA_SORT_COLUMN_TYPE + ")";
                db.execSQL(createTableString);
                for (int index = 0; index < indexes.size(); index++) {
                    db.execSQL("create index " + indexes.get(index) + Form.SUFFIX_INDEX + " on "
                            + Form.DB_DATA_TABLE + "(" + Form.DB_DATA_COLUMN_PREFIX + indexes.get(index) + ")");
                }
                for (int rowIndex = 0; rowIndex < rows.length(); rowIndex++) {
                    JSONObject currRow = rows.getJSONObject(rowIndex);
                    String[] currColumns = new String[columns.size() + 1];
                    String[] currValues = new String[columns.size() + 1];
                    for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
                        currColumns[columnIndex] = Form.DB_DATA_COLUMN_PREFIX + columns.get(columnIndex);
                        currValues[columnIndex] = currRow.getString(columns.get(columnIndex));
                    }
                    currColumns[columns.size()] = Form.DB_DATA_SORT_FIELD;
                    currValues[columns.size()] = String.valueOf((double) rowIndex);//TODO: not sure if should be float or double
                    databaseHelper.runInsertQuery(Form.DB_DATA_TABLE, currColumns, currValues, -1, db);//do not add unique key field index in argument list. Will end up being an extra query
                }
                result = true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.w(TAG, "Provided jsonArray to be dumped into a db is empty");
    }
    db.close();
    //copy db to the ADB push directory
    File adbFormDir = new File(
            Form.BASE_ODK_LOCATION + formName.replaceAll("[^A-Za-z0-9]", "_") + Form.EXTERNAL_ITEM_SET_SUFFIX);
    if (!adbFormDir.exists() || !adbFormDir.isDirectory()) {
        adbFormDir.setWritable(true);
        adbFormDir.setReadable(true);
        Log.i(TAG, "Trying to create dir " + adbFormDir.getPath());
    }
    File sourceDbFile = new File(pathToFile + File.separator + fileName + Form.SUFFIX_DB);
    File destDbFile = new File(Form.BASE_ODK_LOCATION + formName.replaceAll("[^A-Za-z0-9]", "_")
            + Form.EXTERNAL_ITEM_SET_SUFFIX + File.separator + fileName + Form.SUFFIX_DB);
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(sourceDbFile);
        out = new FileOutputStream(destDbFile);
        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:net.willwebberley.gowertides.utils.WeatherDatabase.java

public Boolean insertWeatherData(String data, SQLiteDatabase db) {
    try {//  ww w.j  a  v a  2s . c o m
        JSONArray jsonArray = new JSONArray(data);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject array = jsonArray.getJSONObject(i);
            JSONObject jsonObject = array.getJSONObject("weather");

            long timestamp = jsonObject.getLong("timestamp");
            int year = jsonObject.getInt("year");
            int month = jsonObject.getInt("month");
            int day = jsonObject.getInt("day");
            int max_temp_c = jsonObject.getInt("max_temp_c");
            int max_temp_f = jsonObject.getInt("max_temp_f");
            int min_temp_c = jsonObject.getInt("min_temp_c");
            int min_temp_f = jsonObject.getInt("min_temp_f");
            int wind_speed_miles = jsonObject.getInt("wind_speed_miles");
            int wind_speed_km = jsonObject.getInt("wind_speed_km");
            String wind_direction = jsonObject.getString("wind_direction");
            int wind_degree = jsonObject.getInt("wind_degree");
            String icon_url = jsonObject.getString("icon_url");
            String description = jsonObject.getString("weather_description");
            Double precipitation = jsonObject.getDouble("precipitation");

            String inS = "INSERT INTO weather VALUES(" + timestamp + "," + year + "," + month + "," + day + ","
                    + max_temp_c + "," + max_temp_f + "," + min_temp_c + "," + min_temp_f + ","
                    + wind_speed_miles + "," + wind_speed_km + ",'" + wind_direction + "'," + wind_degree + ",'"
                    + icon_url + "','" + description + "'," + precipitation + ")";
            db.execSQL(inS);
        }
    } catch (Exception e) {
        System.out.println(e);
        return false;
    }
    return true;
}

From source file:com.cyanogenmod.eleven.provider.LocalizedStore.java

public void onCreate(final SQLiteDatabase db) {

    String[] tables = new String[] {
            "CREATE TABLE IF NOT EXISTS " + SongSortColumns.TABLE_NAME + "(" + SongSortColumns.ID
                    + " INTEGER PRIMARY KEY," + SongSortColumns.ARTIST_ID + " INTEGER NOT NULL,"
                    + SongSortColumns.ALBUM_ID + " INTEGER NOT NULL," + SongSortColumns.NAME
                    + " TEXT COLLATE LOCALIZED," + SongSortColumns.NAME_LABEL + " TEXT,"
                    + SongSortColumns.NAME_BUCKET + " INTEGER);",

            "CREATE TABLE IF NOT EXISTS " + AlbumSortColumns.TABLE_NAME + "(" + AlbumSortColumns.ID
                    + " INTEGER PRIMARY KEY," + AlbumSortColumns.ARTIST_ID + " INTEGER NOT NULL,"
                    + AlbumSortColumns.NAME + " TEXT COLLATE LOCALIZED," + AlbumSortColumns.NAME_LABEL
                    + " TEXT," + AlbumSortColumns.NAME_BUCKET + " INTEGER);",

            "CREATE TABLE IF NOT EXISTS " + ArtistSortColumns.TABLE_NAME + "(" + ArtistSortColumns.ID
                    + " INTEGER PRIMARY KEY," + ArtistSortColumns.NAME + " TEXT COLLATE LOCALIZED,"
                    + ArtistSortColumns.NAME_LABEL + " TEXT," + ArtistSortColumns.NAME_BUCKET + " INTEGER);", };

    for (String table : tables) {
        if (DEBUG) {
            Log.d(TAG, "Creating table: " + table);
        }/*from w  w w. ja  v  a2s . c  o m*/
        db.execSQL(table);
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java

private void createIndex(SQLiteDatabase db, String type, String name, String tableName, String col) {
    String s = "CREATE " + type + " " + name + " on " + tableName + " (" + col + ")";
    Log.i(TAG, s);//www.  j  ava  2  s .  c o m
    db.execSQL(s);
}

From source file:ru.orangesoftware.financisto2.db.DatabaseAdapter.java

private void runInTransaction(String sql, long[] ids) {
    SQLiteDatabase db = db();
    db.beginTransaction();/*from  w  ww.  java 2s  .c  om*/
    try {
        int count = ids.length;
        int bucket = 100;
        int num = 1 + count / bucket;
        for (int i = 0; i < num; i++) {
            int x = bucket * i;
            int y = Math.min(count, bucket * (i + 1));
            String script = createSql(sql, ids, x, y);
            db.execSQL(script);
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}

From source file:org.digitalcampus.oppia.application.DbHelper.java

public void createSearchTable(SQLiteDatabase db) {
    String sql = "CREATE VIRTUAL TABLE " + SEARCH_TABLE + " USING FTS3 (" + SEARCH_C_TEXT + " text, "
            + SEARCH_C_COURSETITLE + " text, " + SEARCH_C_SECTIONTITLE + " text, " + SEARCH_C_ACTIVITYTITLE
            + " text " + ")";
    db.execSQL(sql);
}

From source file:org.digitalcampus.oppia.application.DbHelper.java

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion < 7) {
        db.execSQL("drop table if exists " + COURSE_TABLE);
        db.execSQL("drop table if exists " + ACTIVITY_TABLE);
        db.execSQL("drop table if exists " + TRACKER_LOG_TABLE);
        db.execSQL("drop table if exists " + QUIZATTEMPTS_TABLE);
        createCourseTable(db);/*from ww  w  .  j  av a 2 s .co  m*/
        createActivityTable(db);
        createLogTable(db);
        createQuizAttemptsTable(db);
        return;
    }

    if (oldVersion <= 7 && newVersion >= 8) {
        String sql = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_STARTDATE
                + " datetime null;";
        db.execSQL(sql);
        sql = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_ENDDATE + " datetime null;";
        db.execSQL(sql);
    }

    if (oldVersion <= 8 && newVersion >= 9) {
        String sql = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_SCHEDULE + " int null;";
        db.execSQL(sql);
    }

    if (oldVersion <= 9 && newVersion >= 10) {
        String sql = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_TITLE + " text null;";
        db.execSQL(sql);
    }

    // This is a fix as previous versions may not have upgraded db tables correctly
    if (oldVersion <= 10 && newVersion >= 11) {
        String sql1 = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_STARTDATE
                + " datetime null;";
        String sql2 = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_ENDDATE + " datetime null;";
        String sql3 = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_SCHEDULE + " int null;";
        String sql4 = "ALTER TABLE " + ACTIVITY_TABLE + " ADD COLUMN " + ACTIVITY_C_TITLE + " text null;";
        try {
            db.execSQL(sql1);
        } catch (Exception e) {

        }
        try {
            db.execSQL(sql2);
        } catch (Exception e) {

        }
        try {
            db.execSQL(sql3);
        } catch (Exception e) {

        }
        try {
            db.execSQL(sql4);
        } catch (Exception e) {

        }
    }

    if (oldVersion <= 11 && newVersion >= 12) {
        String sql = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_LANGS + " text null;";
        db.execSQL(sql);
        sql = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_IMAGE + " text null;";
        db.execSQL(sql);
    }

    if (oldVersion <= 12 && newVersion >= 13) {
        String sql = "ALTER TABLE " + TRACKER_LOG_TABLE + " ADD COLUMN " + TRACKER_LOG_C_COMPLETED
                + " integer default 0;";
        db.execSQL(sql);
    }
    // skip jump from 13 to 14
    if (oldVersion <= 14 && newVersion >= 15) {
        ContentValues values = new ContentValues();
        values.put(TRACKER_LOG_C_COMPLETED, true);
        db.update(TRACKER_LOG_TABLE, values, null, null);
    }

    if (oldVersion <= 15 && newVersion >= 16) {
        String sql = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_DESC + " text null;";
        db.execSQL(sql);
    }

    if (oldVersion <= 16 && newVersion >= 17) {
        String sql = "ALTER TABLE " + COURSE_TABLE + " ADD COLUMN " + COURSE_C_ORDER_PRIORITY
                + " integer default 0;";
        db.execSQL(sql);
    }

    if (oldVersion <= 17 && newVersion >= 18) {
        //create search table
        this.createSearchTable(db);

        // alter quiz results table
        String sql1 = "ALTER TABLE " + QUIZATTEMPTS_TABLE + " ADD COLUMN " + QUIZATTEMPTS_C_USERID
                + " integer default 0;";
        db.execSQL(sql1);

        // alter tracker table
        String sql2 = "ALTER TABLE " + TRACKER_LOG_TABLE + " ADD COLUMN " + TRACKER_LOG_C_USERID
                + " integer default 0;";
        db.execSQL(sql2);

        // create user table
        this.createUserTable(db);

    }

    if (oldVersion <= 18 && newVersion >= 19) {

        // alter quiz results table
        String sql1 = "ALTER TABLE " + QUIZATTEMPTS_TABLE + " ADD COLUMN " + QUIZATTEMPTS_C_SCORE
                + " real default 0;";
        db.execSQL(sql1);
        String sql2 = "ALTER TABLE " + QUIZATTEMPTS_TABLE + " ADD COLUMN " + QUIZATTEMPTS_C_PASSED
                + " integer default 0;";
        db.execSQL(sql2);

        // alter user table
        String sql3 = "ALTER TABLE " + USER_TABLE + " ADD COLUMN " + USER_C_LAST_LOGIN_DATE + " datetime null;";
        db.execSQL(sql3);
        String sql4 = "ALTER TABLE " + USER_TABLE + " ADD COLUMN " + USER_C_NO_LOGINS + " integer default 0;";
        db.execSQL(sql4);
        //create client table
        db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TABLE);
        this.createClientTable(db);
    }

    if (oldVersion <= 19 && newVersion >= 20) {

        db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TRACKER_TABLE);
        this.createClientTrackerTable(db);

        String sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_C_AGEYOUNGESTCHILD
                + " integer default 0;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_C_HUSBANDNAME + " text null ;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_C_METHODNAME + " text null ;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_CLOSE_CASE + " integer default 0;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_DELETE_RECORD + " integer default 0;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_ADAPTED_METHOD_NAME + " TEXT null;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_ADAPTED_METHOD_TIME + " integer;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        sql = "ALTER TABLE " + CLIENT_TABLE + " ADD COLUMN " + CLIENT_LAST_CREATED + " integer default 0;";
        try {
            db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
        }
        // alter quiz results table
        String sql1 = "ALTER TABLE " + QUIZATTEMPTS_TABLE + " ADD COLUMN " + QUIZATTEMPTS_C_MAXSCORE
                + " real default 0;";
        db.execSQL(sql1);
    }

    if (oldVersion <= 20 && newVersion >= 21) {
        db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TABLE);
        this.createClientTable(db);
        // alter quiz results table
        String sql1 = "ALTER TABLE " + QUIZATTEMPTS_TABLE + " ADD COLUMN " + QUIZATTEMPTS_C_ACTIVITY_DIGEST
                + " text;";
        db.execSQL(sql1);
    }

    if (oldVersion <= 21 && newVersion >= 22) {
        // add points and badges columns
        String sql1 = "ALTER TABLE " + USER_TABLE + " ADD COLUMN " + USER_C_POINTS + " integer default 0;";
        db.execSQL(sql1);
        String sql2 = "ALTER TABLE " + USER_TABLE + " ADD COLUMN " + USER_C_BADGES + " integer default 0;";
        db.execSQL(sql2);

    }
    if (oldVersion <= 22 && newVersion >= 23) {
        // update courses
        db.execSQL("drop table if exists " + COURSE_TABLE);
        db.execSQL("drop table if exists " + ACTIVITY_TABLE);
        db.execSQL("drop table if exists " + TRACKER_LOG_TABLE);
        db.execSQL("drop table if exists " + QUIZATTEMPTS_TABLE);
        createCourseTable(db);
        createActivityTable(db);
        createLogTable(db);
        createQuizAttemptsTable(db);
    }
}

From source file:org.ttrssreader.controllers.DBHelper.java

void markUnsynchronizedStates(Collection<Integer> ids, String mark, int state) {
    if (!isDBAvailable())
        return;/* ww w.  java2s  .  c  o m*/

    SQLiteDatabase db = getOpenHelper().getWritableDatabase();
    writeLock(true);
    db.beginTransaction();
    try {
        for (Integer id : ids) {
            // First update, then insert. If row exists it gets updated and second call ignores it, else the second
            // call inserts it.
            db.execSQL(String.format("UPDATE %s SET %s=%s WHERE id=%s", TABLE_MARK, mark, state, id));
            db.execSQL(String.format("INSERT OR IGNORE INTO %s (id, %s) VALUES (%s, %s)", TABLE_MARK, mark, id,
                    state));
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        writeLock(false);
    }
}