How to merge previous database with new version database sqlite - Android - java

I have an app on playstore which contains a database.
Now in the new release, database is changed, a new table is entered.
Currently i am using sqlite studio and put the database file in the assets which is later copied as an app database. See this example.
I have to delete the previous database to copy new, or if i copy new(overwrite), in both cases user data is lost.
Maybe i can attach a new table to current database, but what if i have to add a column in table in previous database???
How should i handle this???
Edit: If i have to copy the data from database and re-insert, how can i know the tables and columns which currently exist in my database.
Please guide me...

Get All the data from Database into ArrayList or anything else (I will suggest Arraylist) and Delete old database and then Create New database and Insert all The data into New database.
Its not a Proper way but a trick to save your time.
Cheers
-Aman

For a new Table, you just have to add the create table in the update method
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
if(oldVersion < LastVersionWithoutTheTable && newVersion >= LastVersionWithoutTheTable){
db.execSQL(CREATE_THE_TABLE);
}
}
For a new column, copy all your data in an array, recreate your table and insert your data again.
It's maybe possible to used SQL Alter Table but I never try it on a SQlite database.

Related

Android save cursor/query results to sqlite DB file

I have a method that queries a SQL table and saves the subset to a CSV file:
public void exportSQL(File outputFile, String c1) throws SQLException, IOException {
csvWrite = new CSVWriter(new FileWriter(outputFile));
curCSV = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE col1 = '" + c1 + "'", null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
String arrStr[] = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2),
curCSV.getString(3), curCSV.getString(4), curCSV.getString(5),
curCSV.getString(6), curCSV.getString(7), curCSV.getString(8)};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
}
This works, however, I'd like to also save this query/subset as a sqlite (.db) file.
I've seen a number of questions about copying the entire database to another SQLite file, but haven't found anything containing a method for saving out only a subset of a table. In other words, I want to query a table to get a cursor, and then write the contents of that query/cursor to a .db file
Whats the correct way to do this in Java/Android?
There are a number of related questions on this, but they don't seem to cover this specific case. For example:
Copying data from one SQLite database to another. That question is about copying a table from one database to another (existing) database. Whereas I'm interested in querying/subsetting a table and saving that subset to a new (non-existing) database file
How to write a database to a text file in android. There are a number of questions like this one about saving the database to a text file, but I'd like my saved file to be a sqlite .db file, and to only contain my specific query results in a new table
I don't think you can simply save a query/subset, as a DB file that would be usable as a SQLite DB file, as a DB file holds other information e.g. it's master table.
What you could perhaps do is create a new, differently named empty database and then create the respective tables and populate them with the relevant data and finally save the new DB file.
An alternative could be to copy the DB file, open the copy as a database and then remove the components (rows tables) that you don't require to leave you with a copy with only the subset you require.
A third could be to make a copy of the original, whittle down the original, save a copy and then restore from the 1st copy.

Android SQLiteDatabase update schema without loosing data

I'm using SQLiteDatabase in my app. My SQLiteOpenHelper.onUpgrade(..) method looks like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Save current data
ArrayList<Course> tempCourses = getAllCourses();
//Drop tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COURSES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASSIGNMENTS);
//create new tables
onCreate(db);
//Add temp data back to the database
for (Course course : tempCourses) {
addCourse(course);
}
}
I want to keep the old userdata when upgrading the database schema.
However I get the following error when i increase the database version and start my app:
...
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tak3r07.unihelper/com.tak3r07.CourseStatistics.MainActivity}: java.lang.IllegalStateException: getDatabase called recursively
...
This is because getAllCourses() will open the database again to get the data which calls the onUpgrade method (obvious loop).
But how should i store my userdata then?
Regards,
Tak3r07
The ALTER TABLE command can be used to perform some schema changes on existing tables without losing data. If you do find that you need to create the table from scratch, you should follow the following pattern:
Begin transaction
Create the new table
Do a insert .. select statement
Drop the old tables
Commit transaction
If any of the steps fail, you still have the data, and you can pick up at any step to finish the upgrade if it fails for any reason. You do not want to load all the courses into your app's memory like you're doing and then drop the tables because if your app fails to insert the courses back into the db, you'll lose all your data. Do it all in SQL.

How to retrieve data from mapDB database, without override it every time?

I'm developing a GWT web application very similar to Doodle.com
I need to store in a database some informations about Events and Users.
It is a project for a university exam and we have to use mapDB for the persistent datas.
The problem we have is that each time the method saveEventsToDB() is called, a new database overwrites the one had been previously created.
This is the saveEventsToDB() method:
public void saveEventsToDB(Event event) {
DB eventsDB = DBMaker.newFileDB(new File("eventsDB")).closeOnJvmShutdown().make();
Map<Integer, Event> map = eventsDB.getTreeMap("Events");
map.clear();
Set<Integer> keys = map.keySet();
int id=0;
for(int key : keys) {
id++;
}
map.put(id+1, event);
eventsDB.commit();
eventsDB.close();
}
I'm pretty sure it's caused by this line of code:
DB eventsDB = DBMaker.newFileDB(new File("eventsDB")).closeOnJvmShutdown().make();
But this was in the example code that our professor gave us for mapDB.
mapDB documentation says that newFileDB:
Creates or open database stored in file.
But a new database is created everytime, we saw this using some breakpoints and trying to exctract data from db, only one record was returned each time.
If anyone can help it would be very appreciate. Thanks
call db.commit() sometimes. Mapdb has transactions and uncommitted data are discarded.
Your problem is that you delete the data you stored in DB when you do
map.clear()
This function delete or the record in the map. You shouldn't call this function.

Import only specific rows from hsqldb backup

I'm trying to create a function in my java application, where the user could select a prior made backup but only import table-rows that aren't in the current database instance. With a MySql database I could dump my tables, rename them inside the .sql to create temporary tables when imported again, and then simply cross query all rows not in the DB. Any idea how I could acomplish something similar in hsqldb from within my java application?
You can do this:
open the backup database
create a text table that is a copy of the main table, e.g. CREATE TEXT TABLE yourtable_copy AS (SELECT * FROM yourtable)
set a file for the table SET TABLE yourtable_copy SOURCE 'filepath'
copy the data to the new table
set the source off with SET TABLE yourtable_copy SOURCE OFF
shutdown the backup database
open the main database
now do the same text table creation and source setting with the main database but do not copy the data, as the backup data is already there and will be opend
do your updates then turn the text source off in the main database
reference http://www.hsqldb.org/doc/2.0/guide/texttables-chapt.html

Cannot delete all rows from my SQLite DB

I am populating an alertDialog from my database allowing a user to select a preset email message template. The populating seems to work but I cannot delete or edit it, only add to it. I wanted to delete all the rows and basically repopulate it from the beginning but I can seem to delete a single entry or all entries.
This is my deleting method in my DB Handler class:
public void deleteAppointment() {
ourDatabase.delete(DATABASE_TABLETEMP, null, null);
}
This code I have used to try and delete all the rows but it dosent. Can anyone see where I am going wrong?
You can do:
Drop table tablename
--run rebuild scripts

Categories