【发布时间】:2018-01-25 21:13:07
【问题描述】:
SQLite JDBC 驱动程序版本 3.21.0(最新)。
总结
为一个表上的多个插入操作打开了一个准备好的语句,但无法在违反主键的情况下存活。
如果由于违反主键而导致一次“坏”插入失败,则准备好的语句无法处理后续“好”插入。调用 pstmt.setString() 时引发异常“语句未执行”。
将错误跟踪到 org.Sqlite.core.CorePreparedStatement 调用在 checkOpen() 指针==0 中失败。
示例如下。
有人知道为什么会这样吗?我看到一个类似的bug report 被提出但据说是固定的。
Connection conn = DriverManager.getConnection("jdbc:sqlite:./test.db");
String createtable = "CREATE TABLE dummy(ID text, VAL text, PRIMARY KEY(ID) )";
String psSQL = "INSERT INTO dummy (ID, VAL) VALUES (?,?)";
String id = "123456789";
String val = "FooBar";
String id2 = "123456789";
String val2 = "FooBar2";
String id3 = "345678901";
String val3 = "FooBar3";
try {
Statement st= conn.createStatement();
st.executeUpdate(createtable);
PreparedStatement pst = conn.prepareStatement(psSQL);
// 1 insert good entry
pst.setString(1, id);
pst.setString(2, val);
pst.executeUpdate();
System.out.println("1st insert OK for " + id);
// 2. try to insert bad duplicate entry with pkey violation
try {
pst.setString(1, id);
pst.setString(2, val);
pst.executeUpdate();
System.out.println("2nd insert OK for " + id);
} catch (SQLException e) {
System.out.println("2nd insert Failed for " + id);
e.printStackTrace();
}
// 3. try to insert 3rd good value
try {
pst.setString(1, id3); // exception raised here
pst.setString(2, val3);
pst.executeUpdate();
System.out.println("3 insert OK for " + id3);
} catch (SQLException e) {
System.out.println("3 insert Failed for " + id3);
e.printStackTrace();
}
pst.close();
st.executeUpdate(droptable);
} catch (SQLException e) {
e.printStackTrace();
}
【问题讨论】:
-
为什么要问这个问题而不是提交错误报告?
-
错误报告添加到已知问题 #74 github.com/xerial/sqlite-jdbc/issues/74