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

The following examples show how to use android.database.sqlite.SQLiteDatabase#openOrCreateDatabase() . 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 want to check out the right sidebar which shows the related API usage.
Example 1
public SqliteArchiveTileWriter(String outputFile) throws Exception {
    // do this in the background because it takes a long time
    db_file = new File(outputFile);
    try {
        mDatabase = SQLiteDatabase.openOrCreateDatabase(db_file.getAbsolutePath(), null);
    } catch (Exception ex) {
        throw new Exception("Trouble creating database file at " + outputFile, ex);
    }
    try {

        mDatabase.execSQL("CREATE TABLE IF NOT EXISTS " + DatabaseFileArchive.TABLE + " (" + DatabaseFileArchive.COLUMN_KEY + " INTEGER , " + DatabaseFileArchive.COLUMN_PROVIDER + " TEXT, tile BLOB, PRIMARY KEY (key, provider));");
    } catch (Throwable t) {
        t.printStackTrace();
        Log.d(IMapView.LOGTAG, "error setting db schema, it probably exists already", t);
        // throw new IOException("Trouble creating database file"+ t.getMessage());
    }
}
 
Example 2
Source Project: SQLiteToExcel   File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void showDbMsg(String dbName) {
    SQLiteDatabase database;
    try {
        database = SQLiteDatabase.openOrCreateDatabase(dbName, null);
        Cursor cursor = database.rawQuery("select name from sqlite_master where type='table' order by name", null);
        while (cursor.moveToNext()) {
            tv.append("\nNew tables is : " + cursor.getString(0) + "  ");
            Cursor cursor2 = database.rawQuery("select * from " + cursor.getString(0), null);
            while (cursor2.moveToNext()) {
                tv.append("\n");
                for (int i = 0; i < cursor2.getColumnCount(); i++) {
                    tv.append(cursor2.getString(i) + "  ");
                }
            }
            cursor2.close();
        }
        cursor.close();
        database.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source Project: KJFrameForAndroid   File: KJDB.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 在SD卡的指定目录上创建文件
 * 
 * @param sdcardPath
 * @param dbfilename
 * @return
 */
private SQLiteDatabase createDbFileOnSDCard(String sdcardPath,
        String dbfilename) {
    File dbf = new File(sdcardPath, dbfilename);
    if (!dbf.exists()) {
        try {
            if (dbf.createNewFile()) {
                return SQLiteDatabase.openOrCreateDatabase(dbf, null);
            }
        } catch (IOException ioex) {
            throw new RuntimeException("数据库文件创建失败", ioex);
        }
    } else {
        return SQLiteDatabase.openOrCreateDatabase(dbf, null);
    }
    return null;
}
 
Example 4
Source Project: MyHearts   File: RegionFunction.java    License: Apache License 2.0 6 votes vote down vote up
public static List<RegionInfo> getProvencesOrCityOnId(int id) {
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DBManager.PATH, null);
    List<RegionInfo> regionInfos = new ArrayList<>();
    Cursor cursor = db.rawQuery("select * from regions where _id = " + id, null);
    while (cursor.moveToNext()) {
        RegionInfo regionInfo = new RegionInfo();
        int _id = cursor.getInt(cursor.getColumnIndex("_id"));
        int parent = cursor.getInt(cursor.getColumnIndex("parent"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        int type1 = cursor.getInt(cursor.getColumnIndex("type"));
        String firstName = cursor.getString(cursor.getColumnIndex("firstName"));
        regionInfo.setId(_id);
        regionInfo.setParent(parent);
        regionInfo.setName(name);
        regionInfo.setType(type1);
        regionInfo.setFirstName(firstName);
        regionInfos.add(regionInfo);
    }
    cursor.close();
    db.close();
    return regionInfos;
}
 
Example 5
Source Project: osmdroid   File: BookmarkDatastore.java    License: Apache License 2.0 6 votes vote down vote up
public BookmarkDatastore() {

        Configuration.getInstance().getOsmdroidTileCache().mkdirs();
        db_file = new File(Configuration.getInstance().getOsmdroidTileCache().getAbsolutePath() + File.separator + DATABASE_FILENAME);


        try {
            mDatabase = SQLiteDatabase.openOrCreateDatabase(db_file, null);
            mDatabase.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE + " (" +
                COLUMN_LAT+ " INTEGER , " +
                COLUMN_LON + " INTEGER, " +
                COLUMN_TITLE + " TEXT, " +
                COLUMN_ID + " TEXT, " +
                COLUMN_DESC+" TEXT, PRIMARY KEY (" + COLUMN_ID + ") );");
        } catch (Throwable ex) {
            Log.e(IMapView.LOGTAG, "Unable to start the bookmark database. Check external storage availability.", ex);
        }
    }
 
Example 6
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
        DatabaseErrorHandler errorHandler) {
    File f = validateFilePath(name, true);
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f.getPath(), factory, errorHandler);
    setFilePermissionsFromMode(f.getPath(), mode, 0);
    return db;
}
 
Example 7
@Override
public SQLiteDatabase openOrCreateDatabase(
        String name,
        int mode,
        SQLiteDatabase.CursorFactory factory)
{
    SQLiteDatabase result = null;
    try {
        result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), factory);
    } catch (SQLiteException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example 8
Source Project: shillelagh   File: DropTableTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  File dbDir = getContext().getDir("tests", Context.MODE_PRIVATE);
  databaseFile = new File(dbDir, "database_test.db");

  if (databaseFile.exists()) {
    //noinspection ResultOfMethodCallIgnored
    databaseFile.delete();
  }
  database = SQLiteDatabase.openOrCreateDatabase(databaseFile.getPath(), null);
  assertNotNull(database);
  database.setVersion(CURRENT_DATABASE_VERSION);
}
 
Example 9
public List<Note> extractNotes(){

        List<Note> extractedNotes = new ArrayList<>();

        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(), null);

        Cursor cursor = db.rawQuery("SELECT * FROM notes WHERE noteStatus IS NOT NULL", null);

        if (cursor!=null)
        {
            while(cursor.moveToNext()){
                Note note = new Note();
                note.title = "Unsynchronized Note from previous version";
                note.content = cursor.getString(cursor.getColumnIndex("content"));

                extractedNotes.add(note);
            }

            // close and delete old database
            db.close();
            getDatabasePath().delete();
        } else {
            db.close();
        }

        return extractedNotes;
    }
 
Example 10
Source Project: cwac-saferoom   File: EncryptTest.java    License: Apache License 2.0 5 votes vote down vote up
private void enkey(Callable<?> encrypter) throws Exception {
  final Context ctxt=InstrumentationRegistry.getTargetContext();

  assertEquals(SQLCipherUtils.State.DOES_NOT_EXIST, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  SQLiteDatabase plainDb=
    SQLiteDatabase.openOrCreateDatabase(ctxt.getDatabasePath(DB_NAME).getAbsolutePath(),
      null);

  plainDb.execSQL("CREATE TABLE foo (bar, goo);");
  plainDb.execSQL("INSERT INTO foo (bar, goo) VALUES (?, ?)",
    new Object[] {1, "two"});

  assertOriginalContent(plainDb);
  plainDb.close();

  assertEquals(SQLCipherUtils.State.UNENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  encrypter.call();

  assertEquals(SQLCipherUtils.State.ENCRYPTED, SQLCipherUtils.getDatabaseState(ctxt, DB_NAME));

  SafeHelperFactory factory=
    SafeHelperFactory.fromUser(new SpannableStringBuilder(PASSPHRASE));
  SupportSQLiteOpenHelper helper=
    factory.create(InstrumentationRegistry.getTargetContext(), DB_NAME,
      new Callback(1));
  SupportSQLiteDatabase db=helper.getReadableDatabase();

  assertOriginalContent(db);
  db.close();
}
 
Example 11
public T execute() {
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
    Cursor cursor = provideCursor(database);
    T result = provideResult(database, cursor);
    cursor.close();
    database.close();
    return result;
}
 
Example 12
private void open() {
    mDatabase = SQLiteDatabase.openOrCreateDatabase(mPath, null);
    mDatabase.execSQL("CREATE TABLE IF NOT EXISTS '" + TABLE_DCC_HISTORY + "' (" +
            COLUMN_ID + " INTEGER PRIMARY KEY," +
            COLUMN_ENTRY_TYPE + " INTEGER," +
            COLUMN_DATE + " INTEGER," +
            COLUMN_SERVER_NAME + " TEXT," +
            COLUMN_SERVER_UUID + " BLOB," +
            COLUMN_USER_NICK + " TEXT," +
            COLUMN_REMOTE_ADDRESS + " TEXT," +
            COLUMN_FILE_NAME + " TEXT," +
            COLUMN_FILE_SIZE + " INTEGER," +
            COLUMN_FILE_URI + " TEXT" +
            ")");
}
 
Example 13
Source Project: SQLiteToExcel   File: SQLiteToExcel.java    License: Apache License 2.0 5 votes vote down vote up
private SQLiteToExcel(List<String> tables, String protectKey, String encryptKey, String fileName,
                      String dataBaseName, String filePath, String sql, String sheetName) {
    this.protectKey = protectKey;
    this.encryptKey = encryptKey;
    this.fileName = fileName;
    this.filePath = filePath;
    this.sql = sql;
    this.sheetName = sheetName;

    try {
        database = SQLiteDatabase.openOrCreateDatabase(dataBaseName, null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source Project: android-orm   File: DBFile.java    License: Apache License 2.0 5 votes vote down vote up
public DBProxy buildDBProxy() {
    DBProxy proxy = new DBProxy() {
        private SQLiteDatabase database;

        @Override
        public SQLiteDatabase getCreateDatabase() {
            if (database == null || !database.isOpen()) {
                database = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
            }
            return database;
        }
    };
    return proxy;
}
 
Example 15
Source Project: letv   File: FabricContext.java    License: Apache License 2.0 4 votes vote down vote up
@TargetApi(11)
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) {
    return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name).getPath(), factory, errorHandler);
}
 
Example 16
Source Project: android-lite-orm   File: LiteOrm.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) {
    path = mConfig.context.getDatabasePath(mConfig.dbName).getPath();
    return SQLiteDatabase.openOrCreateDatabase(path, factory);
}
 
Example 17
/**
 * Open the connection to the database.
 * @return Returns a DBAdapter.
 * @throws SQLException
 */
private DictionaryAdapter open() throws SQLException {
    db = SQLiteDatabase.openOrCreateDatabase(mDbFile, null);

    return this;
}
 
Example 18
Source Project: SqliteLookup   File: DbSqlite.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * open database
 */
public void openDB() {
	if (mSQLiteDatabase.isOpen() == false)
		mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
}
 
Example 19
Source Project: MyHearts   File: DBManager.java    License: Apache License 2.0 3 votes vote down vote up
private SQLiteDatabase openDatabase(String dbfile) {
   // Log.d(TAG, dbfile);
   // Log.d(TAG, "new File(dbfile).exists():" + new File(dbfile).exists());


    writeDB();

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    return db;
}
 
Example 20
Source Project: AvI   File: SQLiteAndroidDatabase.java    License: MIT License 2 votes vote down vote up
/**
 * Open a database.
 *
 * @param dbfile   The database File specification
 */
void open(File dbfile) throws Exception {
    dbFile = dbfile; // for possible bug workaround
    mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
}