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

The following examples show how to use android.database.sqlite.SQLiteDatabase#openOrCreateDatabase() . 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 File: SqliteArchiveTileWriter.java    From osmdroid with Apache License 2.0 6 votes vote down vote up
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 File: MainActivity.java    From SQLiteToExcel with 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 File: BookmarkDatastore.java    From osmdroid with 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 4
Source File: KJDB.java    From KJFrameForAndroid with 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 5
Source File: RegionFunction.java    From MyHearts with 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 6
Source File: ContextImpl.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
@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
Source File: DatabaseContext.java    From android_maplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
@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 File: DropTableTest.java    From shillelagh with 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
Source File: LegacyImporter.java    From MyOwnNotes with GNU General Public License v3.0 5 votes vote down vote up
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 File: EncryptTest.java    From cwac-saferoom with 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
Source File: CursorOperation.java    From android_dbinspector with Apache License 2.0 5 votes vote down vote up
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
Source File: DCCHistory.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
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 File: DBFile.java    From android-orm with 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 14
Source File: SQLiteToExcel.java    From SQLiteToExcel with 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 15
Source File: DbSqlite.java    From SqliteLookup with 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 16
Source File: DictionaryAdapter.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 17
Source File: LiteOrm.java    From android-lite-orm with 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 18
Source File: FabricContext.java    From letv with 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 19
Source File: DBManager.java    From MyHearts with 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 File: SQLiteAndroidDatabase.java    From AvI with 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);
}