android.database.sqlite.SQLiteDatabase#openOrCreateDatabase ( )源码实例Demo

下面列出了android.database.sqlite.SQLiteDatabase#openOrCreateDatabase ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: osmdroid   文件: SqliteArchiveTileWriter.java
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());
    }
}
 
源代码2 项目: SQLiteToExcel   文件: MainActivity.java
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();
    }
}
 
源代码3 项目: KJFrameForAndroid   文件: KJDB.java
/**
 * 在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;
}
 
源代码4 项目: MyHearts   文件: RegionFunction.java
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;
}
 
源代码5 项目: osmdroid   文件: BookmarkDatastore.java
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);
        }
    }
 
源代码6 项目: AndroidComponentPlugin   文件: ContextImpl.java
@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;
}
 
源代码7 项目: android_maplib   文件: DatabaseContext.java
@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;
}
 
源代码8 项目: shillelagh   文件: DropTableTest.java
@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);
}
 
源代码9 项目: MyOwnNotes   文件: LegacyImporter.java
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;
    }
 
源代码10 项目: cwac-saferoom   文件: EncryptTest.java
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();
}
 
源代码11 项目: android_dbinspector   文件: CursorOperation.java
public T execute() {
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
    Cursor cursor = provideCursor(database);
    T result = provideResult(database, cursor);
    cursor.close();
    database.close();
    return result;
}
 
源代码12 项目: revolution-irc   文件: DCCHistory.java
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" +
            ")");
}
 
源代码13 项目: SQLiteToExcel   文件: SQLiteToExcel.java
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);
    }
}
 
源代码14 项目: android-orm   文件: DBFile.java
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;
}
 
源代码15 项目: letv   文件: FabricContext.java
@TargetApi(11)
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) {
    return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name).getPath(), factory, errorHandler);
}
 
源代码16 项目: android-lite-orm   文件: LiteOrm.java
@Override
public SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) {
    path = mConfig.context.getDatabasePath(mConfig.dbName).getPath();
    return SQLiteDatabase.openOrCreateDatabase(path, factory);
}
 
源代码17 项目: Zom-Android-XMPP   文件: DictionaryAdapter.java
/**
 * Open the connection to the database.
 * @return Returns a DBAdapter.
 * @throws SQLException
 */
private DictionaryAdapter open() throws SQLException {
    db = SQLiteDatabase.openOrCreateDatabase(mDbFile, null);

    return this;
}
 
源代码18 项目: SqliteLookup   文件: DbSqlite.java
/**
 * open database
 */
public void openDB() {
	if (mSQLiteDatabase.isOpen() == false)
		mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
}
 
源代码19 项目: MyHearts   文件: DBManager.java
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;
}
 
源代码20 项目: AvI   文件: SQLiteAndroidDatabase.java
/**
 * 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);
}