java - 从 MySQL 转换到 SQLite 时出错

标签 java mysql sqlite

这并没有太大麻烦,但我不断从用户单击按钮的更新中收到错误。

最初在 MySQL 中这是一个 enum('Y','N') 值,但由于 SQLite 不使用枚举或 boolean 值,我决定分别使用带有 1 和 0 的 int。

如何解决这个问题?

if(e.getSource()==boughtB){
    String cigName;
    cigName = Habanos2.selection;
    String humiString;
    int humino;

    Connection con = null;
    System.out.println(cigName);

    try{
        ResultSet humiRS1 = HabMenu.getGenericRS("select * from cigar where name = '"+cigName+"'"); 
        if(humiRS1.next()){
            humiString = humiRS1.getString("humi_no");

            humino = Integer.parseInt(humiString);
            System.out.println("humino"+humino);

            Class.forName("org.sqlite.JDBC").newInstance();

            con = DriverManager.getConnection("jdbc:sqlite:cigardb.db");
            Statement s = con.createStatement();
            humino++;
            String humiNoS = Integer.toString(humino);
            s.executeUpdate("update cigar set humi = 1 where name ='"+cigName+"'" );
            s.executeUpdate("update cigar set humi_no = "+humiNoS+"where name ='"+cigName+"'");

            Habanos2Frame.myHumi.setText("");
            ResultSet humiii = HabMenu.getGenericRS("select * from cigar where humi = 1");

它会产生以下错误:

java.sql.SQLException: database locked at org.sqlite.DB.execute(DB.java:270) at org.sqlite.DB.executeUpdate(DB.java:281) mysql humi error humiNoS = at org.sqlite.Stmt.executeUpdate(Stmt.java:103) at cigarDiary.buttonBar$ButtonHandler.actionPerformed(buttonBar.java:207) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

最佳答案

好吧,我解决这个问题的方法是在使用完每个结果集(rs.close)和连接(con.close)后关闭它。 MySql 似乎不介意这一点,但 SQLite 却介意。当结果集及其连接仍然打开时,它不允许您执行更新。

关于java - 从 MySQL 转换到 SQLite 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7633330/

相关文章:

android更改游标适配器中的游标位置以增加或减少行

java - 在 Java 中从 C++ 复制延迟/异步启动策略

javascript - 来自 mysql、php/JQuery 的 Google 折线图

mysql - 子查询错误

php - 并发写入内存缓存

SQLite:从分组和排序结果中选择

android - 房间更新多个字段

java - 使用 guice 注入(inject) Class<T>

java - future 的任务抛出由 IndexOutOfBoundsException 引起的 ExecutionException,而据我所知,我没有使用数组

java - 仅获取 'Date' 之前的秒数