I need to open an SQLite database if it exists, or create the database
if it doesn't already exist.  The android API makes this easy to do,
but my problem is that I would like to execute additional code if the
database is being created for the first time (i.e. this is the first
time the user is running the app). I am not sure of the easiest way to
do this.

Per the android SDK docs, the onCreate method of the SQLiteOpenHelper
is "Called when the database is created for the first time".  I
thought that this meant that when I call the getWritableDatabase
method of SQLiteOpenHelper, it executes the onCreate method *only* if
the database is being created for the first time, and not if the
database is being opened but already exists.  This doesn't seem to be
true based on the following code where dBCreated is the public boolean
variable which is initially set to false, but assigned to be true only
in the onCreate method
of MySQLHelper (which extends SQLiteOpenHelper).   When the code runs
with the database already existing, the log reveals that the
MySQLHelper.onCreate method was executed anyway.

So does onCreate behave differently than I thought? Is there a better
way to do what I want? Or have I missed something else?

Thanks for any guidance.



CODE:
public class LaunchB extends ListActivity {
        public static final String DB_NAME="testDB";
        public static final String TABLE_NAME="table1";


        public static SQLiteDatabase db;
        public static SQLHelper MySQLHelper;
        public static Boolean dbCreated;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        /** Called when the activity is first created. */
        super.onCreate(savedInstanceState);
        LaunchB.dbCreated = false;

        MySQLHelper = new SQLHelper(this);
        db=MySQLHelper.getWritableDatabase();
        if (dbCreated) {
                // install and setup
                Log.v("Launch", "db created, do install and setup");
        } else {
                Log.v("Launch", "db not created");
        }
     }
}


public class SQLHelper extends SQLiteOpenHelper{

        public SQLHelper(Context context){
                // constructor
                super(context, LaunchB.DB_NAME, null, 1);
        }
        public void onCreate(SQLiteDatabase db) {
                LaunchB.dbCreated = true;

        }

        public void onUpgrade(SQLiteDatabase db,int oldVersion, int

newVersion) {

        }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to