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?
-
1Check this: stackoverflow.com/questions/9109438/…Yaqub Ahmad– Yaqub Ahmad2012-10-18 04:42:52 +00:00Commented Oct 18, 2012 at 4:42
-
@YaqubAhmad Thanks for the link. I'll try those suggestions tonight or later this weekend.Code-Apprentice– Code-Apprentice2012-10-18 20:28:00 +00:00Commented Oct 18, 2012 at 20:28
Add a comment
|
1 Answer
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.
1 Comment
Code-Apprentice
I assume that the
sqlite3 tool you are referring to will run on my desktop command-line. So if I import the CSV file to a database, then it will be saved in my desktop file system. That amount of by-hand work is fine. Now is there a way to programmatically import that database to a device during testing? I will also need to erase the database (probably in a tearDown() method), which shouldn't be difficult with the Android API. The only part I'm not sure about is getting the database on the emulator in the first place.