Get a database from the assets folder

The database used by this app was created by hand using sqlite3 before the app was written. To read the database, the app uses the third-party class SQLiteAssetHelper, which is a subclass of Android’s class SQLiteOpenHelper. It opens an SQLite .db file in the databases subfolder of the app’s assets folder.

Source code in Assets.zip

  1. MainActivity.java. The SimpleCursorAdapter uses simple_list_item_2.xml.
  2. Helper.java: defines a subclass of SQLiteAssetsHelper.
  3. activity_main.xml. The RelativeLayout displays a ListView when the table is non-empty, and a TextView when the table is empty.
  4. strings.xml
  5. stooges.db (not humanly readable)
  6. AndroidManifest.xml
  7. build.gradle (Module: app). I added compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' to the dependencies.

Create the project

Create the database file stooges.db

cd ~/Desktop
pwd

sqlite3 stooges.db

sqlite> create table android_metadata (
	locale text default "en_US"
);
sqlite> .schema android_metadata

sqlite> insert into android_metadata (locale) values ("en_US");

sqlite> select * from android_metadata;
en_US

sqlite> create table people (
	_id integer primary key autoincrement,
	name text
);
sqlite> .schema people

sqlite> insert into people (name) values ("Moe");
sqlite> insert into people (name) values ("Larry");
sqlite> insert into people (name) values ("Curly");
sqlite> insert into people (name) values ("Shemp");

sqlite> select * from people;
1|Moe
2|Larry
3|Curly
4|Shemp

sqlite> .quit
ls -l stooges.db
-rw-------  1 myname  mygroup  5120 Jul 24 21:18 stooges.db

sqlite> file stooges.db
stooges.db: SQLite 3.x database

Create the app/assets folder

Select the app folder at the top of the Android Studio project view and pull down
File → New → Folder → Assets Folder
Finish

Create the app/assets/databases folder

Select the app/assets folder you jsut created in the Android Studio project view and pull down
File → New → Folder → Assets Folder
☑ Change Folder Location
New Folder Location: src/main/assets/databases
Finish

Copy stooges.db into app/assets/databases

Control-click on the stooges.db on your Desktop and select Copy “stooges.db”. Then control-click on app/assets/databases and select Paste. Press OK.