Sqlite Database Not Created By Sqlite Open Helper
Solution 1:
You should be using DatabaseHandler
to open your database, not using openOrCreateDatabase
. Change the constructor like so (because you only need one of the arguments):
publicDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
Then get a database like so:
DatabaseHandlerdbHandler=newDatabaseHandler(this/*context*/);
SQLiteDatabasedb= dbHandler.getReadableDatabase();
Cursorc= db.rawQuery(...);
Solution 2:
For db operations, I use a helper class like this:
publicclassDBAdapter {
privatestaticfinalStringDATABASE_NAME="DBNAME";
privatestaticfinalintDATABASE_VERSION=1;
privatestatic DBAdapter sInstance;
privatefinal Context mContext;
public DatabaseHelper DBHelper;
public SQLiteDatabase mDb;
privateDBAdapter(Context ctx) {
this.mContext = ctx.getApplicationContext();
DBHelper = newDatabaseHelper(mContext);
}
publicstatic DBAdapter getInstance(Context context) {
if (sInstance == null) {
sInstance = newDBAdapter(context.getApplicationContext());
}
return sInstance;
}
publicstaticclassDatabaseHelperextendsSQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {
...
}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
...
}
}
//---opens the database---public DBAdapter open()throws SQLException {
mDb = DBHelper.getWritableDatabase();
returnthis;
}
//add any db functions you want herepublic Cursor getCursor(intvar) {
...do stuff here with the mDb database
}
This uses a singleton pattern so only one connection to the db is used at all times.
From an activity, I access the db like this:
DBAdaptermDb= DBAdapter.getInstance(getApplicationContext());
mDb.open();
Cursor cur= mDb.getCursor(2);
You dont need to close the db as only one connection will be used, it will be closed when the app closes but you could also add this after the open() function
publicvoidclose() {
DBHelper.close();
}
Solution 3:
You can do something like this to work with SQLite
1)Create a databaseHandler class that will extend SQLieOpenHelper
publicclassEventRegisterextendsSQLiteOpenHelper{
privatestatic Context context;
//Database Versionprivatestaticfinalint DATABASE_VERSION=6;
//Database Nameprivatestaticfinal String DATABASE_NAME="event_db";
//Table Namesprivatestaticfinal String TABLE_NAME="event_table";
//Database Constants I am taking some constants as demo you can take your ownprivatestaticfinal String COLUMN_ID="id";
privatestaticfinal String COLUMN_TITLE="event_title";
privatestaticfinal String COLUMN_CATEGORY="event_category";
privatestaticfinal String COLUMN_DATE="event_date";
privatestaticfinal String COLUMN_TIME="event_time";
privatestaticfinal String COLUMN_DESCRIPTION="event_description";
//Command to create a Tableprivatestaticfinal String CREATE_TABLE="create table "+TABLE_NAME+"("
+COLUMN_ID+" integer primary key autoincrement,"
+COLUMN_TITLE+" text,"
+COLUMN_CATEGORY+" text,"
+COLUMN_DATE+" text,"
+COLUMN_TIME+" text,"
+COLUMN_DESCRIPTION+" text )";
publicEventRegister(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
/*
*This method will be called on Start
*/@OverridepublicvoidonCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
/*
* This method will be called onUpgradate of Database
*/@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
//Method for adding data in database EventData is my Model Object You can insert value as Single or Using Model class alsopublicvoidaddEvent(EventData taskdata)
{
int flag=0;
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=newContentValues();
contentValues.put(COLUMN_TITLE, taskdata.getEvent_title());
contentValues.put(COLUMN_CATEGORY, taskdata.getEvent_category());
contentValues.put(COLUMN_DATE, taskdata.getEvent_date());
contentValues.put(COLUMN_TIME, taskdata.getEvent_time());
contentValues.put(COLUMN_DESCRIPTION, taskdata.getEvent_description());
flag=db.insert(TABLE_NAME, null, contentValues);
db.close();
}
//Add Other methods here
And from MainActivity or else you want to get data can use someThing like
EventRegister eventRegister=newEventRegister(context);
eventRegister.addData(EventData);
I hope you got some direction how to work with SQLite
Post a Comment for "Sqlite Database Not Created By Sqlite Open Helper"