I have a text file which I'll be using to populate my database. The easiest way I found to use this file is using .import statement of SQLite. The statement will be something like this.
.import <myFileName> <myTableName>
However, I don't know where to save this text file. The most basic choice is res/raw folder. But then how to get a reference to this File ?
I must emphasize on the fact that I want a reference of the file and not read it.
Thanks.
You can in general cannot get real file handles of files that do not exist. Ressources and Assets are compiled into your apk, and thus no regular files.
If you wish to ship your application with a database, you can use the asset folder. There is a related question about that. The basic method is that you create the whole database at compile-time (using some tool for sqlite databases, for example SQLite database browser) and ship that database file as asset. Then you can extract the database file from assets and use the newly created database file.
Related
I'm trying to use an excel file as a database and the app constantly needs to read and modify the file. As explained in this Q&A, it is necessary to create a copy of original excel file and all the modification has to be done there. Is there anyway to modify the excel file without creating a copy because I'll be using the file name at another instance to read it.
Also, I'd like your opinion on which one would be efficient as a database for offline access? An android sqlite or excel file ?
I have a CSV file and am trying to convert it into an SQLite .db file so that I can use it in my Android app.
I am aware of methods I can use within the app to convert a CSV placed in the assets folder to an SQLite database within the Android app (e.g. by reading all lines from the CSV file and adding them to the SQLite database), however I want to generate a .db file outside of the app.
The reason I would prefer to do this is because I have several CSVs at around 5 MB each (over 350,000 rows) and so it would take too long to read them all and put them into a new SQLite database in the Android app.
I'm hoping that being able to put my .db files into the assets folder and using the android-sqlite-asset-helper library to access the data from these will be faster.
I have tried tools like a CSV to SQL converter which uses the data from the CSV file you upload to generate an SQL script (i.e. DROP then CREATE then INSERT), but I'm not sure what I need to do with the .sql file to make a .db out of it.
There are also some ways of doing this (I think) I found on Stack Overflow with commands, but I am unsure of how these work and how I use the commands.
So I am asking how I can convert a CSV file into an SQLite database. What is the best method?
Out of curiosity, have you tried creating the database from within the SQLite CLI? Facilities for CSV import exist: https://www.sqlite.org/cli.html#section_8 followed up with a quick ".save output.db" may accomplish what you need.
I searched a lot. but they not worked for me.
I have an encrypted database in asset folder. How can I decrypt in for copy to data folder?
I know how can copy db from asset to data in normal situation. But now I have encrypted database in asset folder.
I'm not familiar with dbconvert.com but as far as I can tell the site you used has nothing to do encryption/decryption. It appears to convert from one format to another... so assuming you converted any format to SQLite (which is what Android uses) then you should just be able to copy the database from your assets folder when the app is first run and from then on use it just like any other SQLite database. There are plenty of examples/code for this on the Internet and also Stack Overflow, for example: How to use an existing database with an Android application
I need to deal with sqlite db placed in asset folder in Android project.
The issue is that usually we create a copy of asset db at runtime and all the queries are done on this copy, but the problem is that when I clear the cache it will remove all updates; so I need to update the asset file not the copy that has been created on runtime.
Can I insert update delete the asset sqlite file?
Actually I want to preserve modifications on DB, and I don't want to loose these changes when clearing application data from settings.
You CANNOT modify resources (assets, raw, drawable, etc.) of an .apk once its built. They are read-only, so what ever goes-in will always remain as it is - until you update the .apk with the newer stuff.
You can not get the old record once you cleared the cache, and cant even update the asset file, once the apk file is generated as it is READ-ONLY. Yeah if you want assistance on how to use already ready database then this link will help you for sure.
Using your own SQlite Database in android applications
And i think if you can copy database from assets folder, then, you can get it from sdcard too. dont know much about it.
I have created an android app that calculates the numerical values of word, and gives you a list of other words with the same numerical value. The way I have been doing it, is storring the words and value in a .properties file. Ie. A line from a .proprties file called "myWords" will have something like: 61=you, then I just use a get() method to call it,
ie. String myString = ResourseBundle.get("myWords").get("61"); would return the string "you". Is there a better way to do this? My guess is that this is not the proper use of a .properties file, and I was wondering if there was another way to do this correctly. I want to include the file in assets folder of the app, and from my limited understanding of sqlite, you can create a file within android, but you can't just include a file in the assets folder, and then read it. So that said, is there some other type of file that I should use, or was I wrong about sqlite, or is the .properties file being used correctly?
SQLite is your best bet and is the best way to handle your data on an Android phone, that is why Google bundled it on Android in the first place, to avoid people the pain of dealing with files.
If you follow this Tutorial they will show you how to create your database in your computer and then load it up on your "assets" folder and access it from your Android application.
Hope that helps!
You can use a csv file, read it from the assets folder each time the app starts or only once after installation and then store the values in a database.
Take a look at my answer here on how to read the files included in your app (you would use a csv file instead of a libray, but it's still reading files): Hosting an executable within Android application
Edit: here's another example to read from the assets folder: Image uploaded from the android app space seems corrupted
You can try out database option. Here is an interesting tutorial on how to pre-populate a database and then ship it out in the APK.