Android SQLite Where and when to initialise SQLiteOpenHelper
I am really struggling with using the SqlLite in my App;
I have set up a database helper class that extends from SQLiteOpenHelper
public class DatabaseHelper extends SQLiteOpenHelper {
In the OnCreate I am Creating about 6 tables:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE myTestTable (Id INTEGER PRIMARY KEY , "
+ "info TEXT," +
+ "TopLeft TEXT")); <-- example insert
}
When my App starts I am parsing some xml and entering the data into the table, and I have checked the database and all 开发者_运维技巧is in there and my tables created correctly.
When I go to call the data it is gone! I have lots of different queries within my dbhelper class and on each activity I;m re initilizing the class so I can get at my function within the class.
I assume this is not the way to do it as it runs my oncreate again and wipes all my tables (or at least this is when it appears to be doing as I check the DB after I've tried calling data from it and it's empty!
I was under the impression the oncreate only ran once, but this is obviously not the case as it appears to be recreating my DB everytime and wiping my data!
Where are you meant to initialize the the dbhelper class and how do you stop it recreating the tables? I need the data to persist even when the app closes!
I'm confused!
If this doesn't make sense please ask specific questions on how I can clarify it!
UPDATE
I have found I can add
CREATE TABLE IF NOT EXISTS
Which stops it recreating the tables, but it still doesn't seem right calling the oncreate every time..
You could put a wrapper class around it?
public class DbAdapter
{
// database details
private static final String DATABASE_NAME = "dbname";
private static final int DATABASE_VERSION = 1;
// where we're running
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// your initial database create statements go here
db.execSQL(DATABASE_CREATE);
}
// if you need to recreate the database, you just up the version number
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// drop commands for each table goes here
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DbAdapter(Context ctx)
{
this.mCtx = ctx;
}
public DbAdapter open() throws SQLException
{
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close()
{
mDbHelper.close();
}
// other database methods can go here
}
onCreate() method is called only the first time you try to access you database and it's not created. So onCreate()
will be called only once. getWritableDatabase returns a db object and will call onCreate()
if needed. NotePadProvider gives a good example how to use SQLiteOpenHelper
.
I am not sure what you mean by the statement -
"each activity I;m re initilizing the class so I can get at my function within the class."
In each activity where you need to access the database, all you have to do is open the database, access/update the data in the tables, and close the handler.
Have you seen this simple sample NotePad app -
NotePad sample app by Google
May be that will help
The DB initialization code should go in your Helper's onCreate method. From the android SQLiteOpenHelper javadocs:
/**
* Called when the database is created for the first time. This is where the
* creation of tables and the initial population of the tables should happen.
*
* @param db The database.
*/
public abstract void onCreate(SQLiteDatabase db);
精彩评论