Java Code Examples for android.database.sqlite.SQLiteDatabase#create()

The following examples show how to use android.database.sqlite.SQLiteDatabase#create() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: VCL-Android   File: MediaDatabase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SQLiteDatabase getWritableDatabase() {
    SQLiteDatabase db;
    try {
        return super.getWritableDatabase();
    } catch(SQLiteException e) {
        try {
            db = SQLiteDatabase.openOrCreateDatabase(VLCApplication.getAppContext().getDatabasePath(DB_NAME), null);
        } catch(SQLiteException e2) {
            Log.w(TAG, "SQLite database could not be created! Media library cannot be saved.");
            db = SQLiteDatabase.create(null);
        }
    }
    int version = db.getVersion();
    if (version != DB_VERSION) {
        db.beginTransaction();
        try {
            if (version == 0) {
                onCreate(db);
            } else {
                onUpgrade(db, version, DB_VERSION);
            }
            db.setVersion(DB_VERSION);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    return db;
}
 
Example 2
Source Project: MiBandDecompiled   File: DbTest.java    License: Apache License 2.0 5 votes vote down vote up
protected SQLiteDatabase createDatabase()
{
    if (inMemory)
    {
        return SQLiteDatabase.create(null);
    } else
    {
        getContext().deleteDatabase("greendao-unittest-db.temp");
        return getContext().openOrCreateDatabase("greendao-unittest-db.temp", 0, null);
    }
}
 
Example 3
Source Project: sync-android   File: AndroidSQLite.java    License: Apache License 2.0 5 votes vote down vote up
public static AndroidSQLite open(File path) {
    SQLiteDatabase db;
    if (path != null) {
        db = SQLiteDatabase.openOrCreateDatabase(path, null);
    } else {
        db = SQLiteDatabase.create(null);
    }
    return new AndroidSQLite(db);
}
 
Example 4
Source Project: NexusData   File: SQLiteDatabaseHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create and/or open a database that will be used for reading and writing.
 * The first time this is called, the database will be opened and
 * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be
 * called.
 *
 * <p>Once opened successfully, the database is cached, so you can
 * call this method every time you need to write to the database.
 * (Make sure to call {@link #close} when you no longer need the database.)
 * Errors such as bad permissions or a full disk may cause this method
 * to fail, but future attempts may succeed if the problem is fixed.</p>
 *
 * <p class="caution">Database upgrade may take a long time, you
 * should not call this method from the application main thread, including
 * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
 *
 * @throws SQLiteException if the database cannot be opened for writing
 * @return a read/write database object valid until {@link #close} is called
 */
public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getWritableDatabase called recursively");
    }

    boolean success = false;
    SQLiteDatabase db = null;

    try {
        mIsInitializing = true;
        if (mPath == null) {
            db = SQLiteDatabase.create(null);
        } else {
            db = mContext.openOrCreateDatabase(mPath.getName(), 0, mFactory);
        }

        int version = db.getVersion();
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    onCreate(db);
                } else {
                    if (version > mNewVersion) {
                        LOG.warn("Can't downgrade read-only database from version " +
                                version + " to " + mNewVersion + ": " + db.getPath());
                    }
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        onOpen(db);
        success = true;
        return db;
    } finally {
        mIsInitializing = false;
        if (success) {
            if (mDatabase != null) {
                try { mDatabase.close(); } catch (Exception e) { }
            }
            mDatabase = db;
        } else {
            if (db != null) db.close();
        }
    }
}
 
Example 5
Source Project: YiBo   File: SQLiteOpenHelper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Create and/or open a database that will be used for reading and writing.
 * Once opened successfully, the database is cached, so you can call this
 * method every time you need to write to the database.  Make sure to call
 * {@link #close} when you no longer need it.
 *
 * <p>Errors such as bad permissions or a full disk may cause this operation
 * to fail, but future attempts may succeed if the problem is fixed.</p>
 *
 * @throws SQLiteException if the database cannot be opened for writing
 * @return a read/write database object valid until {@link #close} is called
 */
public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getWritableDatabase called recursively");
    }

    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.

    boolean success = false;
    SQLiteDatabase db = null;
    try {
        mIsInitializing = true;
        if (mName == null) {
            db = SQLiteDatabase.create(null);
        } else {
        	String path = databaseDirectory(mContext) + File.separator + mName;
        	db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        }

        int version = db.getVersion();
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    onCreate(db);
                } else {
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        onOpen(db);
        success = true;
        return db;
    } finally {
        mIsInitializing = false;
        if (success) {
            if (mDatabase != null) {
                try { mDatabase.close(); } catch (Exception e) { }
            }
            mDatabase = db;
        } else {
            if (db != null) db.close();
        }
    }
}