Example usage for android.database.sqlite SQLiteDatabase openDatabase

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

Introduction

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

Prototype

public static SQLiteDatabase openDatabase(@NonNull String path, @Nullable CursorFactory factory,
        @DatabaseOpenFlags int flags) 

Source Link

Document

Open the database according to the flags #OPEN_READWRITE #OPEN_READONLY #CREATE_IF_NECESSARY and/or #NO_LOCALIZED_COLLATORS .

Usage

From source file:com.panoskrt.dbadapter.DBAdapter.java

public SQLiteDatabase openDB() {
    database = SQLiteDatabase.openDatabase(dbPath + dbName, null, SQLiteDatabase.OPEN_READONLY);
    return database;
}

From source file:org.liberty.android.fantastischmemopro.DatabaseHelper.java

public static void createEmptyDatabase(String path, String name) throws IOException, SQLException {
    File dbfile = new File(path + "/" + name);
    if (dbfile.exists()) {
        // throw new IOException("DB already exist");
        /* Create a backup and overwrite  it instead poping up an error */
        File backupFile = new File(dbfile.getAbsolutePath().replaceAll(".db$", ".old.db"));
        if (backupFile.exists()) {
            backupFile.delete();//from ww  w .j  a v a2s  .  c  o m
        }
        dbfile.renameTo(backupFile);
    }
    SQLiteDatabase database = SQLiteDatabase.openDatabase(path + "/" + name, null,
            SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);

    database.execSQL(
            "CREATE TABLE dict_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, question TEXT, answer TEXT, note TEXT, category TEXT)");
    database.execSQL(
            "CREATE TABLE learn_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, date_learn, interval, grade INTEGER, easiness REAL, acq_reps INTEGER, ret_reps INTEGER, lapses INTEGER, acq_reps_since_lapse INTEGER, ret_reps_since_lapse INTEGER)");
    database.execSQL("CREATE TABLE control_tbl(ctrl_key TEXT, value TEXT)");

    database.beginTransaction();
    try {
        database.execSQL("DELETE FROM learn_tbl");
        database.execSQL("INSERT INTO learn_tbl(_id) SELECT _id FROM dict_tbl");
        database.execSQL(
                "UPDATE learn_tbl SET date_learn = '2010-01-01', interval = 0, grade = 0, easiness = 2.5, acq_reps = 0, ret_reps  = 0, lapses = 0, acq_reps_since_lapse = 0, ret_reps_since_lapse = 0");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_font_size', '24')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_font_size', '24')");
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
        database.close();
    }

}

From source file:com.codebutler.farebot.card.felica.DBUtil.java

public SQLiteDatabase openDatabase() throws SQLException, IOException {
    if (mDatabase != null) {
        return mDatabase;
    }// www  . j a  va  2 s.c o  m

    if (!this.hasDatabase()) {
        this.copyDatabase();
    }
    mDatabase = SQLiteDatabase.openDatabase(new File(DB_PATH, DB_NAME).getPath(), null,
            SQLiteDatabase.OPEN_READONLY);
    return mDatabase;

}

From source file:com.example.ryutasakamoto.readfelica.util.DBUtil.java

public SQLiteDatabase openDatabase() throws SQLException, IOException {
    if (mDatabase != null) {
        return mDatabase;
    }// w w  w.j  av  a2s .c o m

    if (!this.hasDatabase()) {
        this.copyDatabase();
    }
    mDatabase = SQLiteDatabase.openDatabase(getDBFile().getPath(), null, SQLiteDatabase.OPEN_READONLY);
    return mDatabase;

}

From source file:com.codebutler.farebot.transit.ovc.OVChipDBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;/*from  w ww.j a  va 2s. c o m*/

    File file = new File(DB_PATH, DB_NAME);
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating OVChip database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.codebutler.farebot.core.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;/* w  w  w.  j ava2  s  .c o  m*/

    File file = getDBFile();
    if (!file.exists()) {
        Log.d(TAG, String.format("Database for %s does not exist, will install version %s", getDBName(),
                getDesiredVersion()));
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (allowGreaterDatabaseVersions() ? currentVersion < getDesiredVersion()
                : currentVersion != getDesiredVersion()) {
            Log.d(TAG, String.format("Updating %s database. Old: %s, new: %s", getDBName(), currentVersion,
                    getDesiredVersion()));
            tempDatabase.close();
            tempDatabase = null;
        } else {
            Log.d(TAG, String.format("Not updating %s database. Current: %s, app has: %s", getDBName(),
                    currentVersion, getDesiredVersion()));
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:holidayiq.com.geofenced.geofence.GeofenceTransitionsIntentService.java

public static void SendNotificationForEntry(Context mContext, String geoId, String geoName,
        String notificationStringTitle, String notificationStringContent, String notificationStringDeeplink,
        String objType, Context context, String eventType, boolean isBanner) {
    String currentTime = String.valueOf(System.currentTimeMillis());
    if (notificationStringTitle != null) {
        notificationStringTitle = notificationStringTitle.replace("$name$", geoName);
        notificationStringContent = notificationStringContent.replace("$name$", geoName);
        notificationStringDeeplink = notificationStringDeeplink.replace("$name$", Underscored(geoName));
        notificationStringDeeplink = notificationStringDeeplink.replace("$id$", geoId);
        Calendar c = new GregorianCalendar();
        c.set(Calendar.HOUR_OF_DAY, 6);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date d1 = c.getTime();/*  w w w. ja  v  a  2s .  c  o m*/
        boolean isNotification = false;
        File dbFile = mContext.getDatabasePath("hiq_in_app.sqlite");
        SQLiteDatabase inAppDb = SQLiteDatabase.openDatabase(dbFile.getPath(), null,
                SQLiteDatabase.OPEN_READWRITE);
        if (geoName.equalsIgnoreCase("home")) {
            String lastTriggeredHome = HIQSharedPrefrence.getString("lastTriggeredHome", mContext);
            if (lastTriggeredHome == null) {
                sendNotification(notificationStringTitle, notificationStringContent, notificationStringDeeplink,
                        context, isBanner);
                HIQSharedPrefrence.putString("lastTriggeredHome", System.currentTimeMillis() + "", mContext);
                isNotification = true;
            } else {
                long previouslastTriggeredHome = Long
                        .parseLong(HIQSharedPrefrence.getString("lastTriggeredHome", mContext));
                if (previouslastTriggeredHome < d1.getTime()) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    HIQSharedPrefrence.putString("lastTriggeredHome", System.currentTimeMillis() + "",
                            mContext);
                    isNotification = true;
                }
            }

        } else if (objType.equalsIgnoreCase("Hotel")) {
            String lastTriggeredHome = HIQSharedPrefrence.getString("lastTriggeredHotel", mContext);
            if (lastTriggeredHome == null) {
                sendNotification(notificationStringTitle, notificationStringContent, notificationStringDeeplink,
                        context, isBanner);
                HIQSharedPrefrence.putString("lastTriggeredHotel", System.currentTimeMillis() + "", mContext);
                isNotification = true;
            } else {
                long previouslastTriggeredHome = Long
                        .parseLong(HIQSharedPrefrence.getString("lastTriggeredHotel", mContext));
                if (previouslastTriggeredHome < d1.getTime()) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    HIQSharedPrefrence.putString("lastTriggeredHotel", System.currentTimeMillis() + "",
                            mContext);
                    isNotification = true;
                }
            }
        } else if (objType.equalsIgnoreCase("SS")) {
            Cursor res = null;
            try {
                String lastNotificationTriggerd = null;
                res = inAppDb.rawQuery("Select * from " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME + " where "
                        + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                if (res != null) {
                    if (res.moveToFirst()) {
                        lastNotificationTriggerd = res.getString(res.getColumnIndex("lastTimeStamp"));
                    }
                }
                if (lastNotificationTriggerd == null) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    isNotification = true;
                    Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME
                            + " SET lastTimeStamp = '" + currentTime + "' where "
                            + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                    cur.moveToFirst();
                    cur.close();
                } else {
                    long time = Long.parseLong(lastNotificationTriggerd);
                    if (time < d1.getTime()) {
                        sendNotification(notificationStringTitle, notificationStringContent,
                                notificationStringDeeplink, context, isBanner);
                        isNotification = true;
                        Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.SIGHT_SEEING_TABLE_NAME
                                + " SET lastTimeStamp = '" + currentTime + "' where "
                                + GeoDbHelper.SIGHT_SEEING_COULUMN_ID + " = " + geoId, null);
                        cur.moveToFirst();
                        cur.close();
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
                ExceptionUtils.logException(e);
            } finally {
                if (res != null && !res.isClosed()) {
                    res.close();
                }
                res = null;
                if (inAppDb != null && inAppDb.isOpen()) {
                    inAppDb.close();
                }
                inAppDb = null;
            }
        } else if (objType.equalsIgnoreCase("Destination")) {
            Cursor res = null;
            try {
                String lastNotificationTriggerd = null;
                res = inAppDb.rawQuery("Select * from " + GeoDbHelper.DESTINATION_TABLE_NAME + " where "
                        + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                if (res != null) {
                    try {
                        if (res.moveToFirst()) {
                            lastNotificationTriggerd = res.getString(res.getColumnIndex("lastTimeStamp"));
                        }
                    } catch (Exception e) {

                    }
                }
                if (lastNotificationTriggerd == null) {
                    sendNotification(notificationStringTitle, notificationStringContent,
                            notificationStringDeeplink, context, isBanner);
                    isNotification = true;
                    Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.DESTINATION_TABLE_NAME
                            + " SET lastTimeStamp = '" + currentTime + "' where "
                            + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                    cur.moveToFirst();
                    cur.close();
                } else {
                    long time = Long.parseLong(lastNotificationTriggerd);
                    if (time < d1.getTime()) {
                        sendNotification(notificationStringTitle, notificationStringContent,
                                notificationStringDeeplink, context, isBanner);
                        isNotification = true;
                        Cursor cur = inAppDb.rawQuery("update " + GeoDbHelper.DESTINATION_TABLE_NAME
                                + " SET lastTimeStamp = '" + currentTime + "' where "
                                + GeoDbHelper.DESTINATION_COLUMN_ID + " = " + geoId, null);
                        cur.moveToFirst();
                        cur.close();
                    }
                }

            } catch (Exception e) {
                ExceptionUtils.logException(e);
            } finally {
                if (res != null && !res.isClosed()) {
                    res.close();
                }
                res = null;
                if (inAppDb != null && inAppDb.isOpen()) {
                    inAppDb.close();
                }
                inAppDb = null;
            }
        }
        HashMap<String, Object> oMap = new HashMap<String, Object>();
        oMap.put("Type", eventType);
        oMap.put("Name", geoName);
        oMap.put("ID", geoId);
        oMap.put("Object Type", objType);
        oMap.put("Date", new Date());

        if (isNotification) {
        }
    }

    // HIQUtil.sendEventToGAFromObject("GeoFenceEvent", "GeoFenceEvent in Android", oMap);

}

From source file:com.codebutler.farebot.card.felica.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;//w  w w  .j  av  a2s  . co m

    File file = new File(DB_PATH, DB_NAME);
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating Suica database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.example.ryutasakamoto.readfelica.util.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;//from   w w  w .j  a va 2  s  .co  m

    File file = getDBFile();
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating Suica database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:org.thomnichols.android.gmarks.WebViewCookiesDB.java

protected SQLiteDatabase openDatabase(final int mode) {
    File dbPath = ctx.getDatabasePath(DATABASE);
    if (!dbPath.exists())
        return null;
    try {//from www .  ja va 2s. c  o  m
        SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath.getPath(), null, mode);
        while (db.isDbLockedByOtherThreads()) {
            Log.w(TAG, "Waiting for other thread to flush DB");
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
            }
        }
        return db;
    } catch (SQLiteException ex) {
        Log.w(TAG, "Error opening database", ex);
        return null;
    }
}