I want to save a table file in database, which have many tables in it, but, I don't want to save the database, because I will restore the table file if needed and other tables files keep original state. Is there a way?
Asked
Active
Viewed 107 times
1 Answers
0
Some ideas:
- Copy the original database and only use the copy. This approach is simple and doesn't require the code to do anything "special" to not leave changes. However, it makes it hard to persist any data back to the original -- but see below for a nifty trick!
- Use an attached database. Only update tables in the attached database. The code has to be modified to only use tables from the attached database. Modifications to tables in the primary database will still be saved. The attached database can be deleted (or left alone) as needed. Attached databases are a good way to move data between different SQLite data files!
- Use a temporary table (and see this SO post). Then delete the table when done (or let SQLite delete it). Again, this requires the code to play nice and not modify other tables.
- Just don't put the data in the DB :-)
An SQLite database can also be opened "read only", but this has other implications as well.
Happy coding!

Community
- 1
- 1
-
first, thank you for giving some ideas. I want to use the second idea. could you offer some demos for using attached database? – Big Bang Jun 24 '12 at 07:42
-
@BigBang Just like in the documentation link: `ATTACH DATABASE [path] AS [name]` SQL-command (just "execute it") :-) The `getAttachedDatabases()` method in Android can be used to see what is attached. see: http://stackoverflow.com/questions/5503414/android-select-from-attached-database for an example – Jun 24 '12 at 17:52