Having spent the last couple of days trying every possibility I still seem unable to resolve my issue. Can some kind person please help. I have a working android application that uses a SQLite database and the onCreate() method does its job correctly, loading the database from the assets folder as distributed. The only way I get this to work is as detail in the code below. The user can then add their own comments to the database as and when they require. Again this works fine. My problem comes when I want to send out an updated table with new books, new book prices for existing books etc. I can get the onUpgrade method to run and successfully create a new 'temp' table with just the book records that have a comments value, thus saving the users own input to the temp table. NOW THE PROBLEM; I cannot load the new table from the assets folder without getting a RECURSIVE DATABASE CALL error yet getReadableDatabase is needed as the table will not load without it. I need to achieve this so I can work a way out of then loading the users comments back to the relevant book records in the new table. The code below does not load the data in the last method call, copyDataBase() of the onUpgrade method (as there is no getReadable/getWritable) but everything else is OK. I also know that onUpgrade is regarded as a schema change rather than a data change but is using this the problem?.
public DBHelper(Context context) {
super(context, DB_NAME, null, version);
this.cont = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
File dbFile = cont.getDatabasePath(DB_NAME);
if (dbFile.exists()){
// do nothing
} else {
this.getReadableDatabase();
InputStream input = cont.getAssets().open(DB_NAME);
int size = input.available();
input.close();
if (size > 0) {
Log.d("file", dbFile.getPath());
copyDataBase(dbFile);
} else {
db.execSQL("create table " + DB_TABLE + " ( " + _id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Book + " Text, " + Title + " Text, "
+ Label + " Text, " + Comment + " Text");
}
}
} catch (IOException e) {
e.printStackTrace();
}
this.ourdb = db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop the previous copy of the tmp Table
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE + "_tmp");
// Copy the records with comments to the temp table
db.execSQL("CREATE TABLE " + DB_TABLE" + "_tmp AS SELECT * FROM " + DB_TABLE + " WHERE Comment != ' '");
// Delete the original database table
db.execSQL("delete from "+ DB_TABLE);
// Reload the New table from Assets
copyDataBase(db);
// TODO
// Load the comments back from the DB_TABLE_tmp file to their matching records in newly created DB_TABLE
}
private void copyDataBase(File dbFile) throws IOException {
//Open db as the input stream
InputStream myInput = cont.getAssets().open(DB_NAME);
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(dbFile);
//transfer bytes
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
Log.d("buf", "" + length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Aucun commentaire:
Enregistrer un commentaire