Im doing an app which uses sqlite db file in assets folder, There is a screen in my app having a button 'check for update'.
Client has given me an URL link to update db file(which gives a sqlite file).
When user clicks on the button i need to upgrade the old db file with new db file from URL.
Please suggest me how i can do this, or give me a reference to follow
Thank you
The assets folder is read-only, so you won't be able to change or replace that copy.
Basically, you need to copy your DB file out of the assets folder into a writable application directory (probably from getFilesDir() or getExternalFilesDir()). You'll open this copy when you are actually operating, and you'll replace it with the new DB from the web when your user hits the UPDATE button. Because a SQLite database is just a file, there's no problem with deleting the old one and replacing it with a new one. (Close the old one before deleting, of course, for cleanliness.)
Related
I have a Cordova mobile app and I let the user import a SQLite dababase from an external .db file. I'm using the cordova-sqlite-storage
https://github.com/litehelpers/Cordova-sqlite-storage
A naive solution is already implemented but it's risky. Basically the user selects an external .db file using a file picker which replaces the old .db file
window.plugins.sqlDB.copyDbFromStorage("mydb.db", 0, file, true, onSuccess);
where file is the uri of the new .db file.
It works but I'd like to check the .db file first, at least those two checks:
check if the .db file is actually a SQLite database
check if the new SQLite database has some table that must be there
before importing
What I've tried
I tried to find a way to open the .db file from its original directory but it seems that the openDatabase() only opens databases stored in the special default location (which is, I guess, the internal private storage for the app)
openDatabase({name: 'test.db', location: 'default'});
other values for the location parameter are simply discouraged, they suggest to stick with default
If you cannot (or do not want to) open the database at the original location, then you have to copy it to the internal private storage location with a different file name. (You can then later copy over the old file, or delete and rename the files.)
i have a database file "myDB.db" (it have only one Table), and i put that file to assets folder, my app copy the data from it, i can read data there is no problem, but when i try to add a new ROW in my Table the new row only existing in data folder that mean if i clear data or reinstall my app i lose my new ROW. how to add that ROW to my file in assets folder "myDB.db"? or how to replace the new database from my data/data folder to my old file "myDB.db" in assets folder?
i use this code
sorry for my bad ENGLISH.
You cannot do that in the app. Assets are read-only.
You can modify the database on your computer and replace the source version of the asset that gets bundled in the APK.
For versioning preinstalled databases and without the problems with the code you're using, consider sqlite-asset-helper.
I have created a database android app for my college that stores students records and all the record are store in sqlitedatabse and the file name is STUDENT.sqlite.But that file is hidden and can be seen only if phone is rooted.so i need a solution so that the aap gets install it should show separate folder for it and the sqlite file mustbe there in it.so that when i put all records in that and save i can give that file so another person and he can view the same record.no need of entering all records again he can just change the sqlite file.
Maybe you can copy all records from your database into a public or shared directory on sd-card?
Rename the database file to yourdatabase.db and put it in your projects assets folder and on first run of application just copy that database to data directory of android system.
Say I have data about customers and I initially have around 1000 pre-determined customers details I want to insert into my SQLiteDatabase.
Would it be wise to store that data into a text file in my assets folder with my own formatting (tabs to indicate columns and new lines to indicate rows), then just read the text file and insert the data into the database with insert statements? I feel this is not the best way and very in-efficient.
Is there a better way of doing this?
You can use the utility to create your SQL Lite database offline on PC, such as this:
http://portableapps.com/apps/development/sqlite_database_browser_portable
You can package this database along with your app and then put it on phone storage and continue from there.
You can do do in two ways..
Way 1:
You can make one .sqlite file with already 1000 customer records inserted in database and you can use it that .sqlite file from assets folder. Then you can copy those records in your internal database or use that only database.
Way 2:
You can put this 1000 records on server and call it by web service at first time app is launched..
Hope it will help you.
User a .sqlite or .db file in your assets folder with prelaoded entries and import this file to you app from asset folder.
You can use this add-on to access and create you db file
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
and this is tutorial to access database file from assets
Android: Accessing assets folder sqlite database file with .sqlite extension
I would bring along the already pre-populated database .db file.
I've done this before with "sample" data included as part of an application.
Newbie Q coming from iPhone/MonoTouch C# background
I have an existing SQLite database file from another project.
Where do I include the database file into my Eclipse project to have it deploy with the app.
Do I need to indicate that the database file is writable? (In the iPhone world you need to copy the database file from the app's bundle to a writable folder on the iPhone proper before first use.)
Once I have the database file on the phone, how do I tell SQLiteOpenHelper to use it? (I extend SQLiteOpenHelper in a custom class.
Where do I include the database file into my Eclipse project to have it deploy with the app.
You can place the db file in either res/raw or assets/.
Do I need to indicate that the database file is writable? (In the iPhone world you need to
copy the database file from the app's bundle to a writable folder on the iPhone proper before first use.)
With Android, you'll have to copy the file from your bundle into the app's database directory. Then, your app should be able to open the database using SQLiteOpenHelper as you would any other database created by your app.
To open the file (assuming you placed it in res/raw), call Resource.openRawResource(id) and to get the destination directory, call Context.getDatabasePath(filename). The database path might not exist, so you might need to create the parent directories first.