Skip to content

Importing SQLite schema

casidiablo edited this page Mar 6, 2012 · 1 revision

It is common to have a file with an already created SQLite schema which populates the database at start. If that's your case you can do it by using any of these methods: afterCreateImportFromAssets(Context, String), afterCreateImportFromStream(InputStream), afterCreateImportFromString(String), beforeCreateImportFromAssets(Context, String), beforeCreateImportFromStream(InputStream) or beforeCreateImportFromString(String) from the class SqlPersistence.

For instance:

SqlPersistence database = PersistenceConfig.getDatabase("database_name.db", 1);
database.afterCreateImportFromAssets(this, "dump.sql");

There are some rules to keep in mind when building an importing file:

  • You should not use BEGIN TRANSACTION; nor COMMIT;
  • Statements can be broken in lines for readability, but the end of the statement should always finish with ; and nothing else. For instance:
# this is OK
insert into foo values(1, 'bar');
# this is OK
insert into foo
values(1, 'bar');
# this is wrong
insert into foo (1, 'bar');insert into foo (1, 'baz');

However, it is highly recommended to compress things in just one line. For instance, this import file takes up to 6 times more to be processed than this one which insert the same records to the database. In general, it is highly recommended to use compounded INSERT statements like the previous example.

Clone this wiki locally