Testing a SQLite database on Android

好久不见. 提交于 2019-12-24 18:43:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12943230/testing-a-sqlite-database-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!