Java - JDBC驱动程序SQLite 3.7.2 - 无法打开数据库test.db:文件已加密或不是数据库 [英] Java - JDBC Driver SQLite 3.7.2 - Unable to open database test.db: file is encrypted or is not a database
问题描述
我正在使用java中的SQLite JDBC驱动程序3.7.2将一些数据写入数据库文件。当我尝试在命令行上打开数据库文件时,我收到一条错误消息:
I'm writing some data to a database file using the SQLite JDBC driver 3.7.2 from within java . When I try it to open the database file on the command line, I get an error message saying:
无法打开数据库test.db:文件已加密或是不是数据库
"Unable to open database test.db: file is encrypted or is not a database"
我做了一个最小的例子导致了这种行为:
I made a minimal example that results in this behavior:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class PlaygroundSQLite {
public static void insertIntoDB(String dbFilename, String tablename){
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+dbFilename);
PreparedStatement prep = conn.prepareStatement("insert into " + tablename + "( id, text) values (?, ?);");
prep.setString(1, "1FD22A38");
prep.setString(2, "This is a simple test");
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createDB(String dbFilename, String tablename){
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + dbFilename);
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists test;");
stat.executeUpdate("create table " + tablename + " (key INTEGER PRIMARY KEY, id TEXT , text TEXT);");
conn.close();
stat.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String dbFilename = "test.db";
String tablename = "test";
PlaygroundSQLite.createDB(dbFilename, tablename);
PlaygroundSQLite.insertIntoDB(dbFilename, tablename);
}
}
推荐答案
这里的问题是Java SQLite JDBC驱动程序(版本3.7.2)与我的机器上安装的SQLite版本(版本2.8.17)之间的版本不匹配。使用sqlite3二进制解决了这个问题。
The problem here was a version mismatch between the Java SQLite JDBC Driver (version 3.7.2) and the SQLite version installed on my machine (version 2.8.17). Using a sqlite3 binary solved the problem.
这篇关于Java - JDBC驱动程序SQLite 3.7.2 - 无法打开数据库test.db:文件已加密或不是数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!