I'm creating an Android app with a Sqlite DB that has a couple of tables with pre-populated data (500+ rows) and also has tables which will be user populated.
The way I plan on distributing the database is via the following method for adding a pre-populated database to an app Using your own SQLite database in Android applications.
I have an issue with this however as when it comes to upgrades I will likely either be adding a number of rows to the pre-populated tables and possibly even modifying a lot of the data in the existing rows, I want to completely replace those existing tables with the tables from the new pre-populated tables, yet leave the user populated tables intact.
I found another post How do i upgrade my pre-populated sqlite database on my device without re-creating tables? where a user suggests this may be the wrong approach entirely ("The "right" way to do things is quite different from how you've set out") as I want upgrades to be as efficient as possible. As I haven't actually published my first version yet should I be changing my approach?
What is the most effective approach for this scenario?
The way I plan on distributing the database is via the following method for adding a pre-populated database to an app Using your own SQLite database in Android applications.
That code has been out of date for some time. Please consider using something more modern and supported, like SQLiteAssetHelper.
I want to completely replace those existing tables with the tables from the new pre-populated tables, yet leave the user populated tables intact
SQLiteAssetHelper supports two patterns out of the box:
Replace the entire database with the new packaged copy
Run your own SQL scripts to update the existing installed database, ignoring the copy packaged with your app
Since SQLiteAssetHelper is open source, you can see how Jeff Gilfelt wrote it and try to leverage that for somehow supporting your hybrid scenario.
Personally, unless you need to do joins between "pre-populated tables" and "user populated tables", I would encourage you to use two separate databases. One would be all of the pre-populated data, which you could replace completely as needed based on app updates. The other would hold the user tables, which you would handle with a more conventional SQLiteOpenHelper. And, you might be able to do joins even with two separate databases, using ATTACH DATABASE, though I have not personally tried that scenario.
Related
I have an Android app I wrote that uses a sqllite database,(the app is used for the reservation) now I want to write the same app for iPhone and I want to use the same database I used in Android app. My question is can I use the same database in IOS.
If yes, then how can be used and where I place the sqllite database (I mean what folder)? and the users must be online to see the update in the database ?
Can I use an sqlite database created for android in IOS?
Yes, an SQLite database is a a file (or potentially 3 files if using Write-Ahead logging). It's simply a matter of copying the file(s).
Typically (i.e. if not specifying otherwise) an SQLite database is stored in data/data/your_package/database/the_database_name
where your_package is as per the App and the_database_name is the file name (may or may not have an extension).
Note if the database uses Write-ahead logging then 2 other files may exist, these being the_database_name-wal and the_database_name-shm. If they exist and are not empty, they need to also be copied.
However,
and the users must be online to see the update in the database ?
Is a completely different matter though. SQLite is not suited to a client/server situation, as you would have to write all the code to provide this functionality. See Appropriate Uses For SQLite.
Firebase may be an option if you don't mind Google's policies reqgarding information privacy. Otherwise you are likely then looking at something like MySQL/MariaDB.
I'm using ActiveAndroid in my application as the Database system. At current stage I don't want to write the migration files for migrating current tables to altered ones for when I update DB version.
Instead what I want to do is to drop all tables and recreate a new set of tables.
Unfortunately from digging in the ActiveAndroid the Database version test happens at initialization stage in which I pass a ActiveAndroid Configuration object to configure the Database.
At this point their implementation only checks for migration files in the assets folder. I don't mind to create migrations file to do what I need but the problem is that I don't know how to get the current tables initialized in the DB to recreate them.
Does some one knows how to do that with ActiveAndroid?
Not sure if I understood correctly but if you are in development then the easiest is to just uninstall the app and let Active Android recreate the DB schema when you install it again.
When in production you will need to use the migration scripts as per their documentation.
I faced a similar issue and I needed to force a few tables to be recreated. Unfortunately you will need to do it manually. However you can use the ActiveAndroid helper classes to help you out.
First you need to process the old table data somehow.
Drop the tables:
SQLiteUtils.execSql("DROP TABLE IF EXISTS Your_Table_Name");
Recreate the table using the ActiveAndroid classes.
SQLiteUtils.execSql(SQLiteUtils.createTableDefinition(Cache.getTableInfo(YOUR_MODEL_CLASS.class)));
Insert the old data into your newwly create table.
I am building one android application where I want to maintain certain details in the sqlite database. I am using SQLiteOpenHelper interface in order to maintain single instance of the database for all the threads accessing the database.
For security reasons I want to encrypt my database because if the database file is not encrypted, it is possible to view the database file on rooted android devices.
To avoid this I want to encrypt the database file.
I have gone through SEE, wxSQLite, SQLiteCrypt and botansqlite3 but they all require cutomized distribution of SQLite to be shipped onto android device.
I want to secure my database file with the normal distribution of SQLite that is shipped with Android.
Also I tried to use SQLCipher but it increases my application's total size by 8-10MB. One more reason for not using SQLCipher is that it doesn't support mips architecure (but this could be built later on with some modifications in the source code).
Also my database will be accessed and updated by many threads and will be loaded all the time by a service so decrypting database at the time of loading/opening and encrypting at the time of unloading/closing will not work for me. It should be possible to fire queries on encrypted database only or there should be some alternative solution like creating temp database, decrypting it and performing operations on it.
Can anyone suggest me a light weight solution for encrypting SQLite database on android devices?
Thanks in advance.
I wonder when to use several tables in the same database file, and when to use multiple files? For example a separate database file for each table. What are the advantages and disadvantages?
I have "places", "persons" and "bookings". A person is booked to be in one place (at a certain time). Since there are some many-to- relations between them, they need to be in separate tables. Btw, this is a common task, so I wonder if anyone has links to examples or suggestions on how to solve it in the best way.
Your data model is simple, and can be perfectly implemented using 3 tables in the same database. If you use a SQLite database, this database would be one file.
There is no benefit here in creating several files/databases. It would only bring more complexity (and even slightly impact performance/startup time because you would need to open 3 db files)
You can find a tutorial on how to use SQLite (which is the best option if you want to store the data locally on the user device) with Android here.
I'm creating an app which is going to have some data that is stored in an SQLite database. I want the user to be able to create "folders" which can be assigned to each data item.
I was going to do this using a one to many relationship e.g. one "folder" can have many data objects under it but having looked at relationships with SQLite and Android it seems that this would only work in 2.2+ so I'm just wondering what the alternative is?
Any information is much appreciated.
You can still have relationships; older versions of SQLite just won't enforce them. As a stopgap, you can build triggers to do it; in fact there's a handy generator that will generate the SQL for you to use as a starting point.