Converting a CSV file into an SQLite .db file - android

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.

Related

Modify data in excel using jxl without creating copy

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 ?

decrypt db from asset folder

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

Accessing SQLite Database Through Assets

Im trying to use ionic with the SQLite plugin (https://github.com/brodysoft/Cordova-SQLitePlugin) and I was able to create and use a database, but in my app i need a prepopulated database.
I see methods of achieving this by placing the database in the platforms/android/assets folder in the ionic project, and then copying it, on the first android run, to the "correct location".
My question is, why is it needed to copy it to another location? why cant I just access it from the assets folder which the application creates? If it was an image, i wouldnt need to change its place either, i would use it from the assets, so why not the db too?
Your database file in the assets folder is stored in an exported file format (minimum storage space required, but not for interactive use). The Android SQLite Database needs to import this file. This means reading and storing it in a new format for better searching/reading and writing.
So it is not a simple copying process it's an interaction and after the import your database does not read from your assets folder any longer. So replacing your database in your assets folder does not update your imported database.
You can use this project to import your database in android: https://github.com/jgilfelt/android-sqlite-asset-helper

Best way to populate SQLiteDatabase with initial data?

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.

Get a reference to a file saved at compile time

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.

Categories

Resources