Java 和 SQLite
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java and SQLite
提问by Scott Bennett-McLeish
I'm attracted to the neatness that a single file database provides. What driver/connector library is out there to connect and use SQLite with Java.
我被单个文件数据库提供的整洁所吸引。有什么驱动程序/连接器库可以将 SQLite 与 Java 连接和使用。
I've discovered a wrapper library, http://www.ch-werner.de/javasqlite, but are there other more prominent projects available?
我发现了一个包装库http://www.ch-werner.de/javasqlite,但是还有其他更突出的项目吗?
采纳答案by Peter Hoffmann
The wikilists some more wrappers:
在维基列出了更多的包装:
- Java wrapper (around a SWIG interface): http://tk-software.home.comcast.net/
- A good tutorial to use JDBC driver for SQLite. (it works at least !) http://www.ci.uchicago.edu/wiki/bin/view/VDS/VDSDevelopment/UsingSQLite
- Cross-platform JDBC driver which uses embedded native SQLite libraries on Windows, Linux, OS X, and falls back to pure Java implementation on other OSes: https://github.com/xerial/sqlite-jdbc(formerly zentus)
- Another Java - SWIG wrapper. It only works on Win32. http://rodolfo_3.tripod.com/index.html
- sqlite-java-shell: 100% pure Java port of the sqlite3 commandline shell built with NestedVM. (This is not a JDBC driver).
- SQLite JDBC Driver for Mysaifu JVM: SQLite JDBC Driver for Mysaifu JVM and SQLite JNI Library for Windows (x86) and Linux (i386/PowerPC).
- Java 包装器(围绕 SWIG 接口):http: //tk-software.home.comcast.net/
- 将 JDBC 驱动程序用于 SQLite 的好教程。(它至少有效!)http://www.ci.uchicago.edu/wiki/bin/view/VDS/VDSDevelopment/UsingSQLite
- 跨平台 JDBC 驱动程序,它在 Windows、Linux、OS X 上使用嵌入式本机 SQLite 库,并在其他操作系统上回退到纯 Java 实现:https: //github.com/xerial/sqlite-jdbc(以前称为zentus)
- 另一个 Java - SWIG 包装器。它仅适用于 Win32。http://rodolfo_3.tripod.com/index.html
- sqlite-java-shell:使用 NestedVM 构建的 sqlite3 命令行 shell 的 100% 纯 Java 端口。(这不是 JDBC 驱动程序)。
- 适用于 Mysaifu JVM 的 SQLite JDBC 驱动程序:适用于 Mysaifu JVM 的 SQLite JDBC 驱动程序和适用于 Windows (x86) 和 Linux (i386/PowerPC) 的 SQLite JNI 库。
回答by Bernie Perez
I found your question while searching for information with SQLiteand Java. Just thought I'd add my answer which I also posted on my blog.
我在使用SQLite和 Java搜索信息时发现了您的问题。只是想我会添加我也在我的博客上发布的答案。
I have been coding in Java for a while now. I have also known about SQLite but never used it… Well I have used it through other applicationsbut never in an app that I coded. So I needed it for a project this week and it's so simple use!
我已经用 Java 编码有一段时间了。我也知道 SQLite 但从未使用过它……嗯,我已经通过其他应用程序使用过它,但从未在我编码的应用程序中使用过。所以这周我在一个项目中需要它,它的使用非常简单!
I found a Java JDBC driver for SQLite. Just add the JAR fileto your classpath and import java.sql.*
我找到了一个用于 SQLite 的 Java JDBC 驱动程序。只需将JAR 文件添加到您的类路径并导入 java.sql.*
His test app will create a database file, send some SQL commands to create a table, store some data in the table, and read it back and display on console. It will create the test.dbfile in the root directory of the project. You can run this example with java -cp .:sqlitejdbc-v056.jar Test
.
他的测试应用程序将创建一个数据库文件,发送一些 SQL 命令来创建一个表,在表中存储一些数据,然后读回并显示在控制台上。它将在项目的根目录中创建test.db文件。您可以使用java -cp .:sqlitejdbc-v056.jar Test
.
package com.rungeek.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists people;");
stat.executeUpdate("create table people (name, occupation);");
PreparedStatement prep = conn.prepareStatement(
"insert into people values (?, ?);");
prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
ResultSet rs = stat.executeQuery("select * from people;");
while (rs.next()) {
System.out.println("name = " + rs.getString("name"));
System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();
}
}
回答by javashlook
I understand you asked specifically about SQLite, but maybe HSQL databasewould be a better fit with Java. It is written in Java itself, runs in the JVM, supports in-memory tables etc. and all that features make it quite usable for prototyping and unit-testing.
我知道您专门询问了 SQLite,但也许HSQL 数据库更适合 Java。它是用 Java 本身编写的,在 JVM 中运行,支持内存表等,所有这些特性使它非常适用于原型设计和单元测试。
回答by javashlook
There is a new project SQLJetthat is a pure Java implementation of SQLite. It doesn't support all of the SQLite features yet, but may be a very good option for some of the Java projects that work with SQLite databases.
有一个新项目SQLJet,它是 SQLite 的纯 Java 实现。它尚不支持所有 SQLite 功能,但对于一些使用 SQLite 数据库的 Java 项目来说可能是一个非常好的选择。
回答by Srinivas
Bernie's post is very helpful. Couldn't vote up (don't have enough reputation :( ). But it Helped a lot. Just to reiterate!
伯尼的帖子非常有帮助。无法投票(没有足够的声誉 :( )。但它有很大帮助。只是重申!
http://www.zentus.com/sqlitejdbc/
http://www.zentus.com/sqlitejdbc/
Here you can find the latest SQLite JDBC jar. Just add the jar into you classpath and you are done! :) You can run Bernie's sample code to test if everything is fine.
在这里您可以找到最新的 SQLite JDBC jar。只需将 jar 添加到您的类路径中,您就完成了!:) 您可以运行 Bernie 的示例代码来测试是否一切正常。
http://souptonuts.sourceforge.net/readme_sqlite_tutorial.htmlhttp://www.sqlite.org/lang.html
http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html http://www.sqlite.org/lang.html
Here you can find some help on SQL syntax for SQLite. Cheers to SQLite :)
在这里你可以找到一些关于 SQLite 的 SQL 语法的帮助。为 SQLite 干杯 :)
回答by snail
The example code leads to a memory leak in Tomcat (after undeploying the webapp, the classloader still remains in memory) which will cause an outofmemory
eventually. The way to solve it is to use the sqlite-jdbc-3.7.8.jar; it's a snapshot, so it doesn't appear for maven yet.
示例代码导致 Tomcat 内存泄漏(在取消部署 web 应用程序后,类加载器仍保留在内存中),outofmemory
最终将导致。解决方法是使用sqlite-jdbc-3.7.8.jar;这是一个快照,所以它还没有出现在 maven 中。
回答by Eddie
Typo: java -cp .:sqlitejdbc-v056.jar Test
错别字: java -cp .:sqlitejdbc-v056.jar Test
should be: java -cp .:sqlitejdbc-v056.jar; Test
应该: java -cp .:sqlitejdbc-v056.jar; Test
notice the semicolon after ".jar" i hope that helps people, could cause a lot of hassle
注意“.jar”后面的分号,我希望对人们有所帮助,但可能会引起很多麻烦
回答by aboutstudy
When you compile and run the code, you should set the classpath options value. Just like the following:
当您编译和运行代码时,您应该设置类路径选项值。就像下面这样:
javac -classpath .;sqlitejdbc-v056.jar Text.java
java -classpath .;sqlitejdbc-v056.jar Text
Please pay attention to "." and the sparate ";"(win, the linux is ":")
请注意“。” 和零散的“;”(赢,linux是“:”)
回答by Daniel Magnusson
David Crawshaw project(sqlitejdbc-v056.jar) seems out of date and last update was Jun 20, 2009, source here
David Crawshaw 项目(sqlitejdbc-v056.jar)似乎已经过时,上次更新是 2009 年 6 月 20 日, 来源在这里
I would recomend Xerials forkof Crawshaw sqlite wrapper. I replaced sqlitejdbc-v056.jar with Xerials sqlite-jdbc-3.7.2.jar file without any problem.
我会推荐Crawshawsqlite 包装器的Xerials 叉。我用 Xerials sqlite-jdbc-3.7.2.jar 文件替换了 sqlitejdbc-v056.jar 没有任何问题。
Uses same syntax as in Bernie's answerand is much faster and with latest sqlite library.
使用与Bernie 的答案相同的语法,并且速度更快,并且使用最新的 sqlite 库。
What is different from Zentus's SQLite JDBC?
与 Zentus 的 SQLite JDBC 有何不同?
The original Zentus's SQLite JDBC driver http://www.zentus.com/sqlitejdbc/itself is an excellent utility for using SQLite databases from Java language, and our SQLiteJDBC library also relies on its implementation. However, its pure-java version, which totally translates c/c++ codes of SQLite into Java, is significantly slower compared to its native version, which uses SQLite binaries compiled for each OS (win, mac, linux).
To use the native version of sqlite-jdbc, user had to set a path to the native codes (dll, jnilib, so files, which are JNDI C programs) by using command-line arguments, e.g., -Djava.library.path=(path to the dll, jnilib, etc.), or -Dorg.sqlite.lib.path, etc. This process was error-prone and bothersome to tell every user to set these variables. Our SQLiteJDBC library completely does away these inconveniences.
Another difference is that we are keeping this SQLiteJDBC libray up-to-date to the newest version of SQLite engine, because we are one of the hottest users of this library. For example, SQLite JDBC is a core component of UTGB (University of Tokyo Genome Browser) Toolkit, which is our utility to create personalized genome browsers.
Zentus 的原始 SQLite JDBC 驱动程序 http://www.zentus.com/sqlitejdbc/本身是一个很好的使用 Java 语言的 SQLite 数据库的实用程序,我们的 SQLiteJDBC 库也依赖于它的实现。但是,它的纯 Java 版本将 SQLite 的 c/c++ 代码完全翻译成 Java,与使用为每个操作系统(win、mac、linux)编译的 SQLite 二进制文件的原生版本相比,速度要慢得多。
要使用 sqlite-jdbc 的本机版本,用户必须使用命令行参数设置本机代码(dll、jnilib、so 文件,即 JNDI C 程序)的路径,例如 -Djava.library.path= (dll、jnilib 等的路径),或 -Dorg.sqlite.lib.path 等。这个过程很容易出错,而且告诉每个用户设置这些变量很麻烦。我们的 SQLiteJDBC 库完全消除了这些不便。
另一个不同之处在于我们将这个 SQLiteJDBC 库保持在最新版本的 SQLite 引擎上,因为我们是这个库的最热门用户之一。例如,SQLite JDBC 是 UTGB(东京大学基因组浏览器)工具包的核心组件,它是我们创建个性化基因组浏览器的实用程序。
EDIT: As usual when you update something, there will be problems in some obscure place in your code(happened to me). Test test test =)
编辑:像往常一样,当您更新某些内容时,您的代码中某些晦涩的地方会出现问题(发生在我身上)。测试测试测试 =)
回答by Morbo
sqlitejdbc code can be downloaded using git from https://github.com/crawshaw/sqlitejdbc.
sqlitejdbc 代码可以使用 git 从https://github.com/crawshaw/sqlitejdbc下载。
# git clone https://github.com/crawshaw/sqlitejdbc.git sqlitejdbc
...
# cd sqlitejdbc
# make
Note: Makefile requires curl binary to download sqlite libraries/deps.
注意:Makefile 需要 curl 二进制文件才能下载 sqlite 库/deps。