代码1是来自网页的示例项目,我不知道代码1中override fun onCreate(db: SQLiteDatabase)
中的var db是如何传递值的。
我检查了源代码ManagedSQLiteOpenHelper和SQLiteOpenHelper,我还不明白!
代码1
class ForecastDbHelper(ctx: Context = App.instance) : ManagedSQLiteOpenHelper(ctx,
ForecastDbHelper.DB_NAME, null, ForecastDbHelper.DB_VERSION) {
companion object {
val DB_NAME = "forecast.db"
val DB_VERSION = 1
val instance by lazy { ForecastDbHelper() }
}
override fun onCreate(db: SQLiteDatabase) {
db.createTable(CityForecastTable.NAME, true,
CityForecastTable.ID to INTEGER + PRIMARY_KEY,
CityForecastTable.CITY to TEXT,
CityForecastTable.COUNTRY to TEXT)
db.createTable(DayForecastTable.NAME, true,
DayForecastTable.ID to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
DayForecastTable.DATE to INTEGER,
DayForecastTable.DESCRIPTION to TEXT,
DayForecastTable.HIGH to INTEGER,
DayForecastTable.LOW to INTEGER,
DayForecastTable.ICON_URL to TEXT,
DayForecastTable.CITY_ID to INTEGER)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.dropTable(CityForecastTable.NAME, true)
db.dropTable(DayForecastTable.NAME, true)
onCreate(db)
}
}
ManagedSQLiteOpenHelper
abstract class ManagedSQLiteOpenHelper(
ctx: Context,
name: String,
factory: SQLiteDatabase.CursorFactory? = null,
version: Int = 1
): SQLiteOpenHelper(ctx, name, factory, version) {
private val counter = AtomicInteger()
private var db: SQLiteDatabase? = null
fun <T> use(f: SQLiteDatabase.() -> T): T {
try {
return openDatabase().f()
} finally {
closeDatabase()
}
}
@Synchronized
private fun openDatabase(): SQLiteDatabase {
if (counter.incrementAndGet() == 1) {
db = writableDatabase
}
return db!!
}
@Synchronized
private fun closeDatabase() {
if (counter.decrementAndGet() == 0) {
db?.close()
}
}
}
SQLiteOpenHelper
public abstract class SQLiteOpenHelper {
public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
throw new RuntimeException("Stub!");
}
public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
throw new RuntimeException("Stub!");
}
public String getDatabaseName() {
throw new RuntimeException("Stub!");
}
public void setWriteAheadLoggingEnabled(boolean enabled) {
throw new RuntimeException("Stub!");
}
public SQLiteDatabase getWritableDatabase() {
throw new RuntimeException("Stub!");
}
public SQLiteDatabase getReadableDatabase() {
throw new RuntimeException("Stub!");
}
public synchronized void close() {
throw new RuntimeException("Stub!");
}
public void onConfigure(SQLiteDatabase db) {
throw new RuntimeException("Stub!");
}
public abstract void onCreate(SQLiteDatabase var1);
public abstract void onUpgrade(SQLiteDatabase var1, int var2, int var3);
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
throw new RuntimeException("Stub!");
}
public void onOpen(SQLiteDatabase db) {
throw new RuntimeException("Stub!");
}
}
答案 0 :(得分:1)
SQLHelper类旨在简化对SQLite数据库的访问。将值传递给super允许它存储用于创建或升级数据库的值。
第一次调用getWriteable数据库时,它将根据构造函数中提供的数据库验证数据库的版本,如果它不存在,则会调用onCreate,如果它更新,则会调用onUpgrade,否则将发生正常操作
你也在SQLiteOpenHelper上面发布了虚假来源,实际上就是这样:
public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
DatabaseErrorHandler errorHandler) {
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
mContext = context;
mName = name;
mFactory = factory;
mNewVersion = version;
mErrorHandler = errorHandler;
}
所以我不确定你在哪里获得了SQLiteOpenHelper的源代码,但它是编码的,比你上面粘贴的抽象类要大得多。