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?

2
  • 1
    Check this: stackoverflow.com/questions/9109438/… Commented Oct 18, 2012 at 4:42
  • @YaqubAhmad Thanks for the link. I'll try those suggestions tonight or later this weekend. Commented Oct 18, 2012 at 20:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.