簡體   English   中英

Java - SQLITE_BUSY; 數據庫文件被鎖定

[英]Java - SQLITE_BUSY; database file is locked

我的應用程序讀取 html 表,然后腳本 (TableToCSV) 將其轉換為 .csv 格式。 之后,我將該 .csv 轉換為 sqlite 數據庫。 之后,我對數據庫運行查詢。 問題是在執行時,它顯示 SQLITE_BUSY; 數據庫文件被鎖定。

這是什么原因,我該如何解決?

這是我的代碼 -

        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);
               }

            }

        }
    });

更新 - 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;
    }
}
}

這可能是由於您對 sqlite 數據庫有多個打開的引用。 我首先在你的 while 循環中的 finally 塊中關閉你的 PreparedStatement 。

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

您還應該在一切結束時關閉數據庫連接。 如果這不能解決它,那么請解釋更多有關如何“將該 .csv 轉換為 sqlite 數據庫”的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM