I am creating database tables to my android project and the following is the helper class. I am able to create the database, when I try to get the data from the method getSettings() it is showing an error saying that no such table 'Settings'.
But I created the table in onCreate method. After that I debug and found that OnCreate method is not calling. Here is my code:
public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="testDatabase";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
private final String SETTINGS_TABLE = "settings";
private final String SETTINGS_COLUMN_ID = "_id";
private final String SETTINGS_COLUMN_PROFILE_ID = "profileId";
private final String SETTINGS_TABLE_CREATE = "create table " + SETTINGS_TABLE
+ "(" + SETTINGS_COLUMN_ID + " integer primary key autoincrement, "
+ SETTINGS_COLUMN_PROFILE_ID + " varchar(100), );";
private final int SETTINGS_ID = 1;
public DataBaseHelper(Context context)
{
super(context, DB_NAME, null, 1);// 1? its Database Version
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getWritableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
mDataBase = getWritableDatabase();
return mDataBase != null;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
mDataBase = db;
System.out.println("Oncreate table");
try {
db.execSQL(SETTINGS_TABLE_CREATE);
InsertSettingsData();
Log.d(TAG, "onCreate Success create SETTINGS_TABLE_CREATE");
} catch (Exception e) {
e.printStackTrace();
}
}
private int InsertSettingsData() {
// TODO Auto-generated method stub
Log.d(TAG, "InsertConfigInitialData");
try {
ContentValues resourceTable = new ContentValues();
resourceTable.put(SETTINGS_COLUMN_ID, SETTINGS_ID);
resourceTable.put(SETTINGS_COLUMN_PROFILE_ID,
"123456");
int resourceId = (int) mDataBase.insert(SETTINGS_TABLE, null,
resourceTable);
if (resourceId >= 0) {
return resourceId;
}
} catch (Exception e) {
Log.d(TAG, "InsertConfigInitialData failed : " + e.toString());
e.printStackTrace();
}
return -1;
}
public boolean UpdateConfigData() {
Log.d(TAG, "UpdateConfigData");
try {
String condition = SETTINGS_COLUMN_ID + "=" + SETTINGS_ID;
ContentValues resourceTable = new ContentValues();
resourceTable.put(SETTINGS_COLUMN_PROFILE_ID,
"34567");
int totalUpdated = mDataBase.update(SETTINGS_TABLE,
resourceTable, condition, null);
if (totalUpdated > 0) {
return true;
} else {
Log.d(TAG, "UpdateConfigData failed, no records updated");
}
} catch (Exception e) {
Log.d(TAG, "UpdateConfigData failed : " + e.toString());
}
return false;
}
public SettingsObject getSettings() {
Log.d(TAG, "LoadConfigs");
SettingsObject settings = new SettingsObject();
String condition = SETTINGS_COLUMN_ID + "=" + SETTINGS_ID;
try {
Cursor cursor = mDataBase.query(SETTINGS_TABLE, null,
condition, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
SettingsObject.setProfile_id(cursor.getString(cursor
.getColumnIndex(SETTINGS_COLUMN_PROFILE_ID)));
cursor.close();
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "LoadConfigs failed : " + e.getMessage());
}
return settings;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
from my activity class I am calling like this,
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
}
catch (IOException ioe)
{
throw new Error("Unable to create database");
}
try
{
myDbHelper.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
SettingsObject object = myDbHelper.getSettings();
System.out.println("settings profile Id" + object.getProfile_id());
Can I call creating database tables in the following method?
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
mDataBase = getWritableDatabase();
mDataBase.execSQL(SETTINGS_TABLE_CREATE);
InsertSettingsData();
return mDataBase != null;
}
onCreate()
will not be called because the database already exists -- your code shows that you are copying the database from assets if it doesn't exist, so by the time you invoke SQLiteOpenHelper, it does exist and onCreate()
is skipped. In your special case, you can not rely on SQLiteOpenHelper to dispatch the correct events, but rather, you need to copy from assets, then open and amend the schema all at once.
You'd want something like this:
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
try
{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
SQLiteDatabase db = getWriteableDatabase();
db.execSQL( ...create ddl... );
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}