I have a sqlite database that I have in the access folder. I read to it and update fields fine in my app. But when I close the app down and start it again, the changes made to the fields are the original values.
It seems the database is overwritten when when the app starts up. Is there a way to save the data, and open the new database? What am I doing wrong here?
// My understanding is:
// if no-Database created (first use)
// CreateDatabase()
// openDatabase() (2nd use and more) and this is not updated
public class DataBaseHelper extends SQLiteOpenHelper {
public static DataBaseHelper getInstance(Context context)
{
if(mDBHelper == null)
{
mDBHelper = new DataBaseHelper(context);
}
return mDBHelper;
}
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
//String packageName = context.getPackageName();
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}else{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
}
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) {
Log.e("CreateDatabase", "Error copying database " + e.toString());
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
Log.v("CheckDatabase", "Database does not exist yet");
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = null;
try {
myInput = myContext.getAssets().open(DB_NAME);
} catch (IOException e) {
e.printStackTrace();
}
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = null;
try {
myOutput = new FileOutputStream(outFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//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;
try {
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);//OPEN_READONLY);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public int updateWeight(int id)
{
W w = getW(id);
int newWeight = w.getWeight()+1;
ContentValues args = new ContentValues();
args.put(col_one, newWeight);
args.put(col_two, word.getTwo());
args.put(col_three, word.getThree());
SQLiteDatabase db = this.getWritableDatabase();
Log.v("update Weight", "Weight = " + newWeight);
return db.update(TABLE_NAME, args, col_id + "=?",
new String[]{String.valueOf(id)});
}
}
usage: in onCreate
private DataBaseHelper myDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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;
}
_gotit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDbHelper.updateWeight(whereAmI);
}
});
}
0 comments:
Post a Comment