public class MyHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "schedule.db";
public static final int VERSION = 1;
public static final String DATABASE_CREATE = "这里是创建表的语句";
public MyHelper(Context context) {
super(context, DB_NAME, null, VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
}
比如说像上面这样定义一个MyHelper 类继承SQLiteOpenHelper ,那么我在其他的地方用到MyHelper这个类的时候,
public class DbAdapter {
private final Context mCtx;
private MyHelper helper;
private SQLiteDatabase db;
public DbAdapter(Context ctx) {
this.mCtx = ctx;
}
public DbAdapter open() throws SQLException {
helper = new MyHelper(mCtx);
db = helper.getWritableDatabase();
return this;
}
public void close() {
helper.close();
}
}
MyHelper这个类中的onCreate方法什么时候被执行???
是在创建MyHelper对象的时候执行?
还是调用它的getWritableDatabase()方法执行?或者是其它什么时候执行?我现在程序中onCreate方法不执行(我在onCreate方法里面添加Log.d()方法试过了,在LogCat里面没有输出我的调试信息,说明没执行到啊)。。坐等大牛解释!!!(泪崩中)
public static final String DB_NAME = "schedule.db";
public static final int VERSION = 1;
public static final String DATABASE_CREATE = "这里是创建表的语句";
public MyHelper(Context context) {
super(context, DB_NAME, null, VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
}
比如说像上面这样定义一个MyHelper 类继承SQLiteOpenHelper ,那么我在其他的地方用到MyHelper这个类的时候,
public class DbAdapter {
private final Context mCtx;
private MyHelper helper;
private SQLiteDatabase db;
public DbAdapter(Context ctx) {
this.mCtx = ctx;
}
public DbAdapter open() throws SQLException {
helper = new MyHelper(mCtx);
db = helper.getWritableDatabase();
return this;
}
public void close() {
helper.close();
}
}
MyHelper这个类中的onCreate方法什么时候被执行???
是在创建MyHelper对象的时候执行?
还是调用它的getWritableDatabase()方法执行?或者是其它什么时候执行?我现在程序中onCreate方法不执行(我在onCreate方法里面添加Log.d()方法试过了,在LogCat里面没有输出我的调试信息,说明没执行到啊)。。坐等大牛解释!!!(泪崩中)
数据库第一次创建执行,只执行一次,以后不再执行
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();
}
}
}然后单行调试,应该就很清楚了,