Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SqLite&;格拉德尔_Sqlite_Groovy_Gradle - Fatal编程技术网

SqLite&;格拉德尔

SqLite&;格拉德尔,sqlite,groovy,gradle,Sqlite,Groovy,Gradle,我在build.gradle中添加了SQLite: dependencies { compile 'org.xerial:sqlite-jdbc:3.8.9.1' } buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'org.xerial:sqlite-jdbc:3.8.9.

我在build.gradle中添加了SQLite:

dependencies {
        compile 'org.xerial:sqlite-jdbc:3.8.9.1'
    }

buildscript {
    repositories {
        jcenter()
        mavenCentral()      
    }
    dependencies {
        classpath 'org.xerial:sqlite-jdbc:3.8.9.1'
    }
}
一旦我想在代码中连接到SQLite DB:

 groovy.sql.Sql.newInstance(dbLocation, "org.sqlite.JDBC")
它抱怨说:

java.lang.ClassNotFoundException: org.sqlite.JDBC

原因是什么?如何修复它?

问题是您需要将JDBC驱动程序放入根类加载器,而不仅仅是放在通用类路径上

你有几个选择。其中之一是使用您自己的
配置
,然后通过
GroovyObject
操作根类加载器:

import groovy.sql.Sql

configurations {
    sqllite
}

repositories {
    mavenCentral()
}

dependencies {
    sqllite 'org.xerial:sqlite-jdbc:3.8.9.1'
}

URLClassLoader loader = GroovyObject.class.classLoader
configurations.sqllite.each { File file ->
    loader.addURL(file.toURL())
}

Sql sql = Sql.newInstance('jdbc:sqlite:test.db', "org.sqlite.JDBC")

task checkSql << {
    sql.execute 'CREATE TABLE TIM(name CHAR(50))'
    sql.eachRow('SELECT * FROM sqlite_master') { row ->
        logger.lifecycle row.toString()
    }
}

至少在Gradle2.9中是这样的,问题是您需要将JDBC驱动程序放入根类加载器,而不仅仅是放在通用类路径上

你有几个选择。其中之一是使用您自己的
配置
,然后通过
GroovyObject
操作根类加载器:

import groovy.sql.Sql

configurations {
    sqllite
}

repositories {
    mavenCentral()
}

dependencies {
    sqllite 'org.xerial:sqlite-jdbc:3.8.9.1'
}

URLClassLoader loader = GroovyObject.class.classLoader
configurations.sqllite.each { File file ->
    loader.addURL(file.toURL())
}

Sql sql = Sql.newInstance('jdbc:sqlite:test.db', "org.sqlite.JDBC")

task checkSql << {
    sql.execute 'CREATE TABLE TIM(name CHAR(50))'
    sql.eachRow('SELECT * FROM sqlite_master') { row ->
        logger.lifecycle row.toString()
    }
}

至少在Gradle 2.9中是这样的

使用
--info
--stacktrace
运行Gradle并查找导致错误的原因。使用
--info
--stacktrace
运行Gradle并查找导致错误的原因。谢谢!我根据你的答案创建了一个Gradle插件:@gmazzo Nice!玩得高兴谢谢我根据你的答案创建了一个Gradle插件:@gmazzo Nice!玩得高兴