Java 有人能解释一下这条线是干什么的吗?
在本博客中: 这是一行Java 有人能解释一下这条线是干什么的吗?,java,android,Java,Android,在本博客中: 这是一行this.getReadableDatabase(),我不明白它是做什么的,但如果我从代码中删除它,它就会停止工作 /** * Creates a empty database on the system and rewrites it with your own database. * */ public void createDataBase() throws IOException{ boolean dbExist = checkDataBase();
this.getReadableDatabase()代码>,我不明白它是做什么的,但如果我从代码中删除它,它就会停止工作
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
this.getReadableDatabase()
调用getReadableDatabase()方法;从课堂上。这是可能的,因为DataBaseHelper是SQLiteOpenHelper的子类。这是由这条线决定的
public class DataBaseHelper extends SQLiteOpenHelper
此类是SQLiteOpenHelper
的子类
从方法的引用中获取:[1]
创建和/或打开将用于读取和
写作。第一次调用它时,将打开数据库
和onCreate(SQLiteDatabase)、onUpgrade(SQLiteDatabase、int、int)
和/或将调用onOpen(SQLiteDatabase)
因此,它基本上只是为您准备SQLiteDatabase
,并通过更新等方式处理安装生命周期
[1] 从函数实现中,您可以看到此api通过调用getWritableDatabase()打开数据库。如果由于某种原因失败,它将以只读模式打开数据库
public synchronized SQLiteDatabase getReadableDatabase() {
if (mDatabase != null && mDatabase.isOpen()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException("getReadableDatabase called recursively");
}
try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (mName == null) throw e; // Can't open a temp database read-only!
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
}
SQLiteDatabase db = null;
try {
mIsInitializing = true;
String path = mContext.getDatabasePath(mName).getPath();
db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
if (db.getVersion() != mNewVersion) {
throw new SQLiteException("Can't upgrade read-only database from version " +
db.getVersion() + " to " + mNewVersion + ": " + path);
}
onOpen(db);
Log.w(TAG, "Opened " + mName + " in read-only mode");
mDatabase = db;
return mDatabase;
} finally {
mIsInitializing = false;
if (db != null && db != mDatabase) db.close();
}
}
下面是getWritableDatabase()的实现
在这上面有一个有用的评论。另外,您是否尝试搜索任何文档?你看到了吗?看看文档!这与问题有什么关系?问题是“getReadableDatabase()
做什么?”。
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;
if (mDatabase != null) mDatabase.lock();
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
db = mContext.openOrCreateDatabase(mName, 0, mFactory);
}
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.unlock();
}
mDatabase = db;
} else {
if (mDatabase != null) mDatabase.unlock();
if (db != null) db.close();
}
}
}