BUMP EDIT.
I'm relatively new to Android, so please excuse if this question seems easy, but I'm Trying to import an existing database into my program, and I used the method mentioned in the links http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ How to use an existing database with an Android application
Now if I want to add additional tables to my databases, If i place them in the Oncreate part of the code, will they still work or would I have to place them in the openDatabase/createDatabase portion of the code? The code that Im using is :
public class DatabaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.example.az101.conquer_the_word/databases/";
private static String DB_NAME = "words.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**static final String dbName="words";
static final String employeeTable="Employees";
static final String colID="EmployeeID";
static final String colName="EmployeeName";
static final String colAge="Age";
static final String colDept="Dept";
static final String deptTable="Dept";
static final String colDeptID="DeptID";
static final String colDeptName="DeptName";
static final String viewEmps="ViewEmps";**/
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
private static final String TAG = DatabaseHelper.class.getSimpleName().toString();
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
db.execSQL(ScoreTableRepo.createTable());
db.execSQL(Users_in_groupsRepo.createTable());
db.execSQL(Groups_infoRepo.createTable());
db.execSQL(ClustersRepo.createTable());
db.execSQL(Words_StatsRepo.createTable());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, String.format("SQLiteDatabase.onUpgrade(%d -> %d)", oldVersion, newVersion));
// Drop table if existed, all data will be gone!!!
db.execSQL("DROP TABLE IF EXISTS " + ScoreTable.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Users_In_Groups.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Groups_info.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Clusters.TABLE);
db.execSQL("DROP TABLE IF EXISTS " + Words_Stats.TABLE);
onCreate(db);
}
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
Also to prevent the issue of multiple queries creating problems in the code, I use the method provided below to keep a counter and I'm not sure If I'm doing it right, how and when would I call the databaseManager Function? Code for my Database manager is also given below. https://github.com/dmytrodanylyk/dmytrodanylyk/blob/gh-pages/articles/Concurrent%20Database%20Access.md
public class DatabaseManager {
private Integer mOpenCounter = 0;
private static DatabaseManager instance;
private static SQLiteOpenHelper mDatabaseHelper;
private SQLiteDatabase mDatabase;
public static synchronized void initializeInstance(SQLiteOpenHelper helper) {
if (instance == null) {
instance = new DatabaseManager();
mDatabaseHelper = helper;
}
}
public static synchronized DatabaseManager getInstance() {
if (instance == null) {
throw new IllegalStateException(DatabaseManager.class.getSimpleName() +
" is not initialized, call initializeInstance(..) method first.");
}
return instance;
}
public synchronized SQLiteDatabase openDatabase() {
mOpenCounter+=1;
if(mOpenCounter == 1) {
// Opening new database
mDatabase = mDatabaseHelper.getWritableDatabase();
}
return mDatabase;
}
public synchronized void closeDatabase() {
mOpenCounter-=1;
if(mOpenCounter == 0) {
// Closing database
mDatabase.close();
}
}
}
Any input and help is be appreciated.
The repofunctions in the Oncreate method have all the functions related to that particular table. I was not able to find any links online for the right method.