Example usage for android.database.sqlite SQLiteDatabase openOrCreateDatabase

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

Introduction

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

Prototype

public static SQLiteDatabase openOrCreateDatabase(@NonNull String path, @Nullable CursorFactory factory) 

Source Link

Document

Equivalent to openDatabase(path, factory, CREATE_IF_NECESSARY).

Usage

From source file:org.apache.cordova.legacywebsql.LegacyWebSql.java

/**
 * Open database.//  w  w  w .j  a v  a 2 s.c  om
 *
 * @param db
 *            The name of the database
 * @param version
 *            The version
 * @param display_name
 *            The display name
 * @param size
 *            The size in bytes
 */
public void openDatabase(String db, String version, String display_name, long size) {

    // If database is open, then close it
    if (this.myDb != null) {
        this.myDb.close();
    }

    // If no database path, generate from application package
    if (this.path == null) {
        this.path = this.cordova.getActivity().getApplicationContext().getDir("database", Context.MODE_PRIVATE)
                .getPath();
    }

    this.dbName = this.path + File.separator + db + ".db";

    /*
     * What is all this nonsense? Well the separator was incorrect so the db was showing up in the wrong 
     * directory. This bit of code fixes that issue and moves the db to the correct directory.
     */
    File oldDbFile = new File(this.path + File.pathSeparator + db + ".db");
    if (oldDbFile.exists()) {
        File dbPath = new File(this.path);
        File dbFile = new File(dbName);
        dbPath.mkdirs();
        oldDbFile.renameTo(dbFile);
    }

    this.myDb = SQLiteDatabase.openOrCreateDatabase(this.dbName, null);
}

From source file:org.skt.runtime.original.Storage.java

/**
 * Open database.//w w w  .j  a  v a2  s.c om
 * 
 * @param db
 *            The name of the database
 * @param version
 *            The version
 * @param display_name
 *            The display name
 * @param size
 *            The size in bytes
 */
public void openDatabase(String db, String version, String display_name, long size) {

    // If database is open, then close it
    if (this.myDb != null) {
        this.myDb.close();
    }

    // If no database path, generate from application package
    if (this.path == null) {
        this.path = this.ctx.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    }

    this.dbName = this.path + File.pathSeparator + db + ".db";
    this.myDb = SQLiteDatabase.openOrCreateDatabase(this.dbName, null);
}

From source file:com.android.email.mail.store.zx.LocalStore.java

/**
 * @param uri local://localhost/path/to/database/uuid.db
 *//*from   www.  ja v  a  2 s.  co  m*/
public LocalStore(String _uri, Context context) throws MessagingException {
    mContext = context;
    URI uri = null;
    try {
        uri = new URI(_uri);
    } catch (Exception e) {
        throw new MessagingException("Invalid uri for LocalStore");
    }
    if (!uri.getScheme().equals("local")) {
        throw new MessagingException("Invalid scheme");
    }
    mPath = uri.getPath();

    File parentDir = new File(mPath).getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdirs();
    }
    mDb = SQLiteDatabase.openOrCreateDatabase(mPath, null);
    int oldVersion = mDb.getVersion();

    /*
     *  TODO we should have more sophisticated way to upgrade database.
     */
    if (oldVersion != DB_VERSION) {
        if (Config.LOGV) {
            Log.v(Email.LOG_TAG, String.format("Upgrading database from %d to %d", oldVersion, DB_VERSION));
        }
        if (oldVersion < 18) {
            /**
             * Missing or old:  Create up-to-date tables
             */
            mDb.execSQL("DROP TABLE IF EXISTS folders");
            mDb.execSQL("CREATE TABLE folders (id INTEGER PRIMARY KEY, name TEXT, "
                    + "last_updated INTEGER, unread_count INTEGER, visible_limit INTEGER)");

            mDb.execSQL("DROP TABLE IF EXISTS messages");
            mDb.execSQL("CREATE TABLE messages (id INTEGER PRIMARY KEY, folder_id INTEGER, "
                    + "uid TEXT, subject TEXT, date INTEGER, flags TEXT, sender_list TEXT, "
                    + "to_list TEXT, cc_list TEXT, bcc_list TEXT, reply_to_list TEXT, "
                    + "html_content TEXT, text_content TEXT, attachment_count INTEGER, "
                    + "internal_date INTEGER, message_id TEXT)");

            mDb.execSQL("DROP TABLE IF EXISTS attachments");
            mDb.execSQL("CREATE TABLE attachments (id INTEGER PRIMARY KEY, message_id INTEGER,"
                    + "store_data TEXT, content_uri TEXT, size INTEGER, name TEXT,"
                    + "mime_type TEXT, content_id TEXT)");

            mDb.execSQL("DROP TABLE IF EXISTS pending_commands");
            mDb.execSQL("CREATE TABLE pending_commands "
                    + "(id INTEGER PRIMARY KEY, command TEXT, arguments TEXT)");

            mDb.execSQL("DROP TRIGGER IF EXISTS delete_folder");
            mDb.execSQL(
                    "CREATE TRIGGER delete_folder BEFORE DELETE ON folders BEGIN DELETE FROM messages WHERE old.id = folder_id; END;");

            mDb.execSQL("DROP TRIGGER IF EXISTS delete_message");
            mDb.execSQL(
                    "CREATE TRIGGER delete_message BEFORE DELETE ON messages BEGIN DELETE FROM attachments WHERE old.id = message_id; END;");
            mDb.setVersion(DB_VERSION);
        } else {
            if (oldVersion < 19) {
                /**
                 * Upgrade 18 to 19:  add message_id to messages table
                 */
                mDb.execSQL("ALTER TABLE messages ADD COLUMN message_id TEXT;");
                mDb.setVersion(19);
            }
            if (oldVersion < 20) {
                /**
                 * Upgrade 19 to 20:  add content_id to attachments table
                 */
                mDb.execSQL("ALTER TABLE attachments ADD COLUMN content_id TEXT;");
                mDb.setVersion(20);
            }
        }

        if (mDb.getVersion() != DB_VERSION) {
            throw new Error("Database upgrade failed!");
        }
    }
    mAttachmentsDir = new File(mPath + "_att");
    if (!mAttachmentsDir.exists()) {
        mAttachmentsDir.mkdirs();
    }
}

From source file:com.digicorp.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Open a database./* w ww . java  2  s . c om*/
 *
 * @param dbname
 *            The name of the database-NOT including its extension.
 *
 * @param password
 *            The database password or null.
 *
 */
private void openDatabase(String dbname, String password) {
    if (this.getDatabase(dbname) != null)
        this.closeDatabase(dbname);

    File dbfile = this.cordova.getActivity().getDatabasePath(dbname + ".db");

    Log.v("info", "Open sqlite db: " + dbfile.getAbsolutePath());

    SQLiteDatabase mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    dbmap.put(dbname, mydb);
}

From source file:com.phonegap.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Open database.//from  w ww. j  a va 2s .c o  m
 *
 * @param db
 *            The name of the database
 * @param version
 *            The version
 * @param display_name
 *            The display name
 * @param size
 *            The size in bytes
 */
public void openDatabase(String db, String version, String display_name, long size) {

    // If database is open, then close it
    if (this.myDb != null) {
        this.myDb.close();
    }

    // If no database path, generate from application package
    if (this.path == null) {
        Package pack = this.ctx.getClass().getPackage();
        String appPackage = pack.getName();
        this.setStorage(appPackage);
    }

    this.dbName = this.path + db + ".db";
    this.myDb = SQLiteDatabase.openOrCreateDatabase(this.dbName, null);
}

From source file:com.android.email.mail.store.LocalStore.java

/**
 * @param uri local://localhost/path/to/database/uuid.db
 *//*from w w w.  ja  v  a 2s.  c om*/
private LocalStore(String _uri, Context context) throws MessagingException {
    mContext = context;
    URI uri = null;
    try {
        uri = new URI(_uri);
    } catch (Exception e) {
        throw new MessagingException("Invalid uri for LocalStore");
    }
    if (!uri.getScheme().equals("local")) {
        throw new MessagingException("Invalid scheme");
    }
    mPath = uri.getPath();

    File parentDir = new File(mPath).getParentFile();
    if (!parentDir.exists()) {
        parentDir.mkdirs();
    }
    mDb = SQLiteDatabase.openOrCreateDatabase(mPath, null);
    int oldVersion = mDb.getVersion();

    /*
     *  TODO we should have more sophisticated way to upgrade database.
     */
    if (oldVersion != DB_VERSION) {
        if (Email.LOGD) {
            Log.v(Email.LOG_TAG, String.format("Upgrading database from %d to %d", oldVersion, DB_VERSION));
        }
        if (oldVersion < 18) {
            /**
             * Missing or old:  Create up-to-date tables
             */
            mDb.execSQL("DROP TABLE IF EXISTS folders");
            mDb.execSQL("CREATE TABLE folders (id INTEGER PRIMARY KEY, name TEXT, "
                    + "last_updated INTEGER, unread_count INTEGER, visible_limit INTEGER)");

            mDb.execSQL("DROP TABLE IF EXISTS messages");
            mDb.execSQL("CREATE TABLE messages (id INTEGER PRIMARY KEY, folder_id INTEGER, "
                    + "uid TEXT, subject TEXT, date INTEGER, flags TEXT, sender_list TEXT, "
                    + "to_list TEXT, cc_list TEXT, bcc_list TEXT, reply_to_list TEXT, "
                    + "html_content TEXT, text_content TEXT, attachment_count INTEGER, "
                    + "internal_date INTEGER, message_id TEXT, store_flag_1 INTEGER, "
                    + "store_flag_2 INTEGER, flag_downloaded_full INTEGER,"
                    + "flag_downloaded_partial INTEGER, flag_deleted INTEGER, x_headers TEXT)");

            mDb.execSQL("DROP TABLE IF EXISTS attachments");
            mDb.execSQL("CREATE TABLE attachments (id INTEGER PRIMARY KEY, message_id INTEGER,"
                    + "store_data TEXT, content_uri TEXT, size INTEGER, name TEXT,"
                    + "mime_type TEXT, content_id TEXT)");

            mDb.execSQL("DROP TABLE IF EXISTS pending_commands");
            mDb.execSQL("CREATE TABLE pending_commands "
                    + "(id INTEGER PRIMARY KEY, command TEXT, arguments TEXT)");

            addRemoteStoreDataTable();

            addFolderDeleteTrigger();

            mDb.execSQL("DROP TRIGGER IF EXISTS delete_message");
            mDb.execSQL(
                    "CREATE TRIGGER delete_message BEFORE DELETE ON messages BEGIN DELETE FROM attachments WHERE old.id = message_id; END;");
            mDb.setVersion(DB_VERSION);
        } else {
            if (oldVersion < 19) {
                /**
                 * Upgrade 18 to 19:  add message_id to messages table
                 */
                mDb.execSQL("ALTER TABLE messages ADD COLUMN message_id TEXT;");
                mDb.setVersion(19);
            }
            if (oldVersion < 20) {
                /**
                 * Upgrade 19 to 20:  add content_id to attachments table
                 */
                mDb.execSQL("ALTER TABLE attachments ADD COLUMN content_id TEXT;");
                mDb.setVersion(20);
            }
            if (oldVersion < 21) {
                /**
                 * Upgrade 20 to 21:  add remote_store_data and update triggers to match
                 */
                addRemoteStoreDataTable();
                addFolderDeleteTrigger();
                mDb.setVersion(21);
            }
            if (oldVersion < 22) {
                /**
                 * Upgrade 21 to 22:  add store_flag_1 and store_flag_2 to messages table
                 */
                mDb.execSQL("ALTER TABLE messages ADD COLUMN store_flag_1 INTEGER;");
                mDb.execSQL("ALTER TABLE messages ADD COLUMN store_flag_2 INTEGER;");
                mDb.setVersion(22);
            }
            if (oldVersion < 23) {
                /**
                 * Upgrade 22 to 23:  add flag_downloaded_full & flag_downloaded_partial
                 * and flag_deleted columns to message table *and upgrade existing messages*.
                 */
                mDb.beginTransaction();
                try {
                    mDb.execSQL("ALTER TABLE messages ADD COLUMN flag_downloaded_full INTEGER;");
                    mDb.execSQL("ALTER TABLE messages ADD COLUMN flag_downloaded_partial INTEGER;");
                    mDb.execSQL("ALTER TABLE messages ADD COLUMN flag_deleted INTEGER;");
                    migrateMessageFlags();
                    mDb.setVersion(23);
                    mDb.setTransactionSuccessful();
                } finally {
                    mDb.endTransaction();
                }
            }
            if (oldVersion < 24) {
                /**
                 * Upgrade 23 to 24:  add x_headers to messages table
                 */
                mDb.execSQL("ALTER TABLE messages ADD COLUMN x_headers TEXT;");
                mDb.setVersion(24);
            }
        }

        if (mDb.getVersion() != DB_VERSION) {
            throw new Error("Database upgrade failed!");
        }
    }
    mAttachmentsDir = new File(mPath + "_att");
    if (!mAttachmentsDir.exists()) {
        mAttachmentsDir.mkdirs();
    }
}

From source file:com.zetaDevelopment.phonegap.plugin.sqlitePlugin.SQLitePlugin.java

/**
 * Open a database.//from  ww w . j a v a  2s  .co  m
 *
 * @param dbname
 *            The name of the database-NOT including its extension.
 *
 * @param extension
 *            The extension of the database.
 *
 * @param password
 *            The database password or null.
 *
 */
private void openDatabase(String dbname, String extension, String password) {
    if (this.getDatabase(dbname) != null)
        this.closeDatabase(dbname);

    //File dbfile = this.cordova.getActivity().getDatabasePath(dbname + "." + extension);

    File dbfile = new File("/mnt/sdcard/" + dbname + "." + extension);

    Log.v("info", "Open sqlite db: " + dbfile.getAbsolutePath());

    SQLiteDatabase mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

    dbmap.put(dbname, mydb);
}

From source file:com.nolanlawson.cordova.sqlite.SQLitePlugin.java

private SQLiteDatabase getDatabase(String name) {
    SQLiteDatabase database = DATABASES.get(name);
    if (database == null) {
        if (":memory:".equals(name)) {
            database = SQLiteDatabase.openOrCreateDatabase(name, null);
        } else {//from w ww  .j  a  v a 2s .  c  o m
            File file = new File(cordova.getActivity().getFilesDir(), name);
            database = SQLiteDatabase.openOrCreateDatabase(file, null);
        }
        DATABASES.put(name, database);
    }
    return database;
}

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public Database openOrCreateDB(String databaseName) throws IOException {
    SQLiteDatabase db;//from  w ww.j  av  a2 s  . com
    if (databaseName.startsWith("file://")) {
        db = SQLiteDatabase.openOrCreateDatabase(FileSystemStorage.getInstance().toNativePath(databaseName),
                null);
    } else {
        db = getContext().openOrCreateDatabase(databaseName, getContext().MODE_PRIVATE, null);
    }
    return new AndroidDB(db);
}