Okay so my android app makes use of a pre-existing database, which I access using this
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Now I have no problem reading data from the database, but I cannot seem to add to it. The problem is the database file is never updated, only the instance of the database I have running at the time. I need the changes I make to the database to be permanent, so I tried to serialize the instance of the database, but found that it cannot be done. I think that after I add to the instance of my database, I need to export this to a file, an with it overwrite the database file. I'm not sure how I would go about and have read every comment from the site above and have gone through countless post here as well.
I could be wrong but that's the only way I can imagine it being done.
public void addTenantWithoutProperty(String name, String email, String phone){
//This method reads from my database
ArrayList<Tenants> tenants = (ArrayList<Tenants>) getAllTenants();
//Checks data in the database before I try add to it
for(int i = 0;i < tenants.size();i++){
String name;
name = tenants.get(i).getName();
System.out.println(i + " before " + names);
}
//I have also tried adding to it using ContentValues, but it makes no difference
String tenantsql = "INSERT INTO tenants (name, phone, email) VALUES ('" + name + "','"
+ phone + "','" + email + "')";
myDataBase.execSQL(tenantsql);
tenants = (ArrayList<Tenants>) getAllTenants();
//checks data in database after insertion
for(int i = 0;i < tenants.size();i++){
String names;
names = tenants.get(i).getName();
System.out.println(i + " after " + names);
}
}
This is called on a button click, and the output shows that the data is being added, but it does not cause a permanent change in the database. How can I get around this? Any help at all would be greatly appreciated
EDIT: Resolved! The if statment in the createDataBase() read if(!dbExist), changed to if(dbExist) and works fine. Problem was that even if the database existed, it was being overwritten by the database file in my assets folder. Thanks for your replies regardless