Java - SQLITE_BUSY; 数据库文件被锁定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22335196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:01:07  来源:igfitidea点击:

Java - SQLITE_BUSY; database file is locked

javadatabasesqlitecsvfile-io

提问by satnam

My application reads a html table and then a script (TableToCSV) converts it into a .csv format. After that I convert that .csv into a sqlite database. After that I run queries on the database. Problem is that upon executing, it shows that SQLITE_BUSY; database file is locked.

我的应用程序读取 html 表,然后脚本 (TableToCSV) 将其转换为 .csv 格式。之后,我将该 .csv 转换为 sqlite 数据库。之后,我对数据库运行查询。问题是在执行时,它显示 SQLITE_BUSY; 数据库文件被锁定。

Whats the reason of this and how can I fix this?

这是什么原因,我该如何解决?

Here is my code -

这是我的代码 -

        final JFileChooser  fileDialog = new JFileChooser();
    JButton btnInputFile = new JButton("Input File");
    btnInputFile.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            int returnVal = fileDialog.showOpenDialog(rootPane);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
               java.io.File file = fileDialog.getSelectedFile();

               String name = file.getName();
               name = name.substring(0, name.lastIndexOf("."));
               name += ".html";
               File newFile = new File(file.getParentFile(), name);
               if (file.renameTo(newFile)) {
                   try {
                    TableToCSV tableToCSV = new TableToCSV(newFile, ',', '\"', '#', CSV.UTF8Charset );
                    System.out.println("action");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               }

               try
               {
                BufferedReader br=new BufferedReader(new FileReader("v.csv"));
                String line;

                while((line=br.readLine())!=null)
                {
                    System.out.println(line);
                    String[]value = line.split(",");
                    System.out.println(value.length);

                    String sql = "INSERT into main ([Ticket #], Status, Priority, Department, [Account Name]) "
                     + "values ('"+value[0]+"','"+value[1]+"','"+value[2]+"','"+value[3]+"','"+value[4]+"')";

                     PreparedStatement pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
                     pst.executeUpdate();

                }
                br.close();

               }

               catch(Exception e)
               {
                JOptionPane.showMessageDialog(null, e);
               }

            }

        }
    });

UPDATE - DatabaseConnection.java

更新 - DatabaseConnection.java

public class DatabaseConnection {
Connection conn = null;
Statement stmt = null;

public static Connection ConnectDB() {

    try {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:database.db");
        conn.setAutoCommit(true);
        return conn;
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}
}

回答by bradvido

It's probably due to you having multiple open references to the sqlite database. I'd start by closing your PreparedStatement in a finally block inside your while loop.

这可能是由于您对 sqlite 数据库有多个打开的引用。我首先在你的 while 循环中的 finally 块中关闭你的 PreparedStatement 。

PreparedStatement pst = null;
try{   
  pst = DatabaseConnection.ConnectDB().prepareStatement(sql);
  pst.executeUpdate();
}finally{
  if(pst != null) {
    pst.close();
  }
}

You should also close the database connection at the end of everything. If that doesn't fix it, then please explain more about how you "convert that .csv into a sqlite database"

您还应该在一切结束时关闭数据库连接。如果这不能解决它,那么请解释更多有关如何“将该 .csv 转换为 sqlite 数据库”的信息