Testing a SQLite database on Android - android

I am working on JUnit tests for my Android app which uses a SQLite database. I need to create a test database which can be manipulated by test cases. The database needs to start in a known configuration and reset for each test case. I figured out that I can use an in-memory database by passing the SQLiteOpenHelper a null value as the file name for the database (see this article for details). Now I also want to populate the database table for some of my tests. I have a CSV file on my local file system with some data that I can use for testing. How do I get this data into my SQLite database on an emulator or other device for testing?

There is no built-in CSV import function in the SQLite library code.
However, the sqlite3 command-line tool can import CSV files. Once imported, you have a database file that you can just copy to your device with the adb tool.
Alternatively, you can use sqlite3's .dump command to generate the SQL commands to recreate the database; you then put them into a string array, or put them into a text file read by your test program.

Related

SQLite database file upload at server in SQL format

I want to upload local sqlite database file at server but in sql format. Is it possible to directly save it in mysql supported format? The file is uploaded successfully on server but its just a flat file of sqlite db. When i open it in editplus or import it in phpmyadmin, it shows error. But when I manually export the database from sqlite manager in .sql extension, it successfully gets open in readable format in editplus. Please help me with this problem. Thanks in advance !
AFAIK you are talking about two completely different things.
Its like comparing a DB dump file to a properly exported file like an xml file with schema and data information from Oracle or MySql
When you export you get a file with or without .sql extension and it means a file with set of queries like DDL or DML create,insert etc. That may be sequentially run to execute all the commands in it and provide you with the right schema and data present in that file.
Whereas a DB file that is created via app is specific to the device and is an .sqlite file which is specific to sqlite browser and that may read it and not a set of queries only that you may open in edit-plus or a text editor.
Sqlite DB file that you find on your device is dynamically generated pages to maintain the integrity of the db.
For more info you must read,
Format of sqlite database
Sorry I don't have code but the idea is,
--Edit with algo--
Assuming you have the create commands for all tables and constraints with you , as they are not going to change most of the time.
You may use this function as a Utility to create and maintain the sql
command list for you parallel to the db you have.
void createExportCommands()
{
...
- Read all tables one by one using a `Cursor`
- Based on specific tables/columns create queries
i.e. String query="insert into "+your_table+" values("+cursor.getString(0),....+");";
- Write this data into a file called export_data.sql and keep updating it in background
}
Perhaps its not the best approach but it will solve your issue.

Where is SQLite database located on android

I'm new to programing for android and i'm still learning. So I have a question about the location of the SQLite database. Is it stored in the same file system as the application ?
And also i'm not sure can the database be created before the app is installed(can it come with the app) or can the database only be created from inside the app ?
And what if i wanted my app to come with a database that already has tables and records which is local and not on a server. Would that be possible ?
SQLite is available on every Android device. Using an SQLite database in Android does not require any database setup or administration.
You only have to define the SQL statements for creating and updating the database. Afterwards the database is automatically managed for you by the Android platform.
Access to an SQLite database involves accessing the filesystem. This can be slow. Therefore it is recommended to perform database operations asynchronously, for example inside the AsyncTask class.
If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.
The parts of the above directory are constructed based on the following rules. DATA is the path which the Environment.getDataDirectory() method returns. APP_NAME is your application name. FILENAME is the name you specify in your application code for the database.
You can also follow this tutorial for further understanding.
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Database is created after the app installation or after the db changes. The db is stored in /data/data/your_package/databases/

how to export database in android application

I have created a database android application it works correctly on emulator , but when Iam transferring it to my device it says that the table is not exist ,I have used 'openOrCreateDatabase() ' method , the database doesnt come with apk file ,when I run the ap, my database is read only, means I already have a lot of data in it and only need to see them
Things in your apk file such as assets are read only so you can't use it as a live database. If you have data already in sqlite database, you will have to copy the data into the live database. If on the other hand you have it in some other format such as csv, you will have to create a reader which will add it into your live database.

Android - Download SQL Database and Search

I've been working on testing trying to get an SQL database off of a website then take all the records and store them into arrays. Problem is that I've tried a ton of downloading methods and I keep getting hit with an error with opening the SQL database. I can't tell if this is actually downloading, if the database is corrupt (I checked it and it looked correct) or if my queries are messed up. Bear with me because I am just learning SQL and Android development.
The file at the link you provide is not a SQLite database. It's an ASCII file that contains SQL statements for creating a database and inserting content. There's no straightforward way to execute the content of that file in an Android app.
To make this work you need to create an actual SQLite database from your SQL file. To do that, install SQLite for whatever your desktop OS is and use this command:
sqlite> .read <filename of your SQL file>
Replace the file on your website with the SQLite file that creates. That should get you past the open error.
What are you doing here is download your database from url and stored it in sdcard.
Now you should open a empty database in your app and then copy your database in your app database and then try to open it.(you are not copying the database) the default location of database is
private String DB_PATH = "/data/data/your package name/databases/";

How to convert MDB to SQLite in Android

I have one Microsoft Access .MDB file and want to use that database in an Android application.
How can I convert the .mdb database to SQLite?
You need to use some tools to convert database, refer to supported list softwares that do the job you need here: http://www.sqlite.org/cvstrac/wiki?p=ConverterTools
You can write your own - it's not very difficult
Install SQlite on your desktop - just go to sqlite.org
Get JDBC access to SQLite (there're a lot of JDBC drivers for SQLite)
Get JDBC access to your MDB (MS-Access) using JDBC (common JDBC-ODBC bridge driver is ok)
VoilĂ !
Steps to read Access files in android :
1-Creat Acces Database then,Export the access database into text files, semicolon or comma delimited.
2-Open the SQLite database browser version 1.1 ( http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/1.1/sqlitebrowser-1.1-win.zip/download?use_mirror=garr&download= ) and chose creat new DATABASE then enter it's name ,then file menu ->import->table from csv file. Browse for your text file and choose the proper delimiter. Click create.
3-Done.
Then you would need to make some modifications to that database and those tables to make it usable by Android to populate listviews and other widgets.
1) The database must contain a table called "android_metadata"
2) This table must have the column "locale"
3) There should be a single record in the table with a value of "en_US"
4) The primary key for every table needs to be called "_id" (this is so Android will know where to
bind the id field of your tables)
Then you put the DB in your assets folder and when your app starts copy it to your apps data directory.
Now :
Using your own SQLite database in Android applications example here:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Another excellent tool to convert the .mdb database to SQLite:
https://github.com/arturasn/mdb2sqlite
Executable direct download: https://github.com/arturasn/mdb2sqlite/blob/master/bin/mdb2sqlite.exe

Categories

Resources