Ok, find it by myself:)
in build.gradle
of your app:
...
configurations {
driver
}
dependencies {
...
driver 'org.xerial:sqlite-jdbc:3.20.1'
...
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each {File file -> loader.addURL(file.toURI().toURL())}
task createDB << {
ext.loadScript = { sql, file ->
println("Applying " + file)
String[] sqlcmds = file.text.split(";")
sqlcmds.collect{it.trim()}.findAll{!it.isEmpty() && !it.startsWith("--")}.each{
try {
sql.execute(it)
} catch(java.sql.SQLException e) {
System.err << "Invalid SQL statement: " + it
e.printStackTrace(System.err)
throw.e
}
}
}
def sql = groovy.sql.Sql.newInstance('jdbc:sqlite:path/to/asset/folder/dbfile','','','org.sqlite.JDBC')
new File("path/to/sql/init/files").eachFileMatch(~/.*\.sql/) {loadScript(sql, it)}
sql.close()
}
preBuild.dependsOn createDB
don't forget to add android specific creation sql script:
CREATE TABLE IF NOT EXISTS "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT OR REPLACE INTO "android_metadata" (ROWID, "locale") VALUES ((SELECT ROWID FROM "android_metadata" WHERE "locale" = 'en_US'), 'en_US');
Also you must handle recreation (e.g. either delete the db file before it's created or put safe sql statements like CREATE TABLE IF NOT EXISTS
or INSERT OR IGNORE INTO
intu init scripts)
How to load it in application is answered e.g. here:
Copy SQLite database from assets folder