下面列出了android.database.sqlite.SQLiteDatabase#openOrCreateDatabase ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
}
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();
}
}
/**
* 在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;
}
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;
}
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);
}
}
@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;
}
@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;
}
@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);
}
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;
}
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();
}
public T execute() {
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
Cursor cursor = provideCursor(database);
T result = provideResult(database, cursor);
cursor.close();
database.close();
return result;
}
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" +
")");
}
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);
}
}
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;
}
@TargetApi(11)
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, DatabaseErrorHandler errorHandler) {
return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name).getPath(), factory, errorHandler);
}
@Override
public SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) {
path = mConfig.context.getDatabasePath(mConfig.dbName).getPath();
return SQLiteDatabase.openOrCreateDatabase(path, factory);
}
/**
* Open the connection to the database.
* @return Returns a DBAdapter.
* @throws SQLException
*/
private DictionaryAdapter open() throws SQLException {
db = SQLiteDatabase.openOrCreateDatabase(mDbFile, null);
return this;
}
/**
* open database
*/
public void openDB() {
if (mSQLiteDatabase.isOpen() == false)
mSQLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
}
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;
}
/**
* 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);
}