0

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?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

1 Answers1

2

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.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • 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. – Code-Apprentice Oct 17 '12 at 21:14