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里面没有输出我的调试信息,说明没执行到啊)。。坐等大牛解释!!!(泪崩中)

解决方案 »

  1.   

    在调getReadableDatabase或getWritableDatabase时,会判断指定的数据库是否存在,不存在则调SQLiteDatabase.create创建, onCreate只在数据库第一次创建时才执行
      

  2.   

    因为你运行一次后已经有schedule.db这个数据库文件了,所以你之后都不会调用onCreate这个方法了,你把FileExploer里的数据库文件删除就OK了。
      

  3.   

    请问怎么删除这个文件,我在Eclipse里的DDMS视图下可以找到这个文件,但是里面好像不能进行删除操作的吧。
      

  4.   

     onCreate()
    数据库第一次创建执行,只执行一次,以后不再执行
      

  5.   

    可以删,FileExploer右上角有个按钮就可以删除选中文件
      

  6.   

    启动的时候,如果数据库不存在,则会自动调用onCreate
      

  7.   

    请问在FileExploer的那个文件夹里能找到schedule.db文件啊
      

  8.   

    贴上源码:
        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();
                }
            }
        }然后单行调试,应该就很清楚了,