[FIXED] How to unlock SQLite database file?

Issue

When I execute the below code, I get :

"[SQLITE_BUSY] The database file is locked (database is locked)"

try {
    String query = "WITH updates(ID, newvalue) AS ( SELECT TotalStock.Sl_No, TotalStock.Price * TotalStock.Preffered_Stock  FROM TotalStock ) update TotalStock SET Total_Price=  ( SELECT newvalue FROM updates WHERE TotalStock.Sl_No = ID )";
    String query_2="select Sl_No, Item_Name, Perishable, Stock, Preffered_Stock, Total_Price from TotalStock where Stock=?";
    PreparedStatement pst=connection.prepareStatement(query);
    PreparedStatement pst_2=connection.prepareStatement(query_2);
    pst_2.setString(1, "0");
    pst.executeUpdate() ;
    ResultSet rs_2 = pst_2. executeQuery() ;
    
    table.setModel(DbUtils.resultSetToTableModel(rs_2));
} 
catch (Exception e1) {
    e1.printStackTrace();
}

SQLite connection :

Connection conn = null;

public static Connection dbConnector()
{
    try
    {
        Class.forName("org.sqlite.JDBC");
    
        Connection conn = DriverManager.getConnection("jdbc:sqlite:/Users/aditya/Downloads/EISJ/CS/IA/DATABASE/login_cred.db");
            
        return conn;
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
        return null;
    }
}

Solution

There is an instance that is using the SQlite file, Check if you are not running a similar project instance that is making use of the Sqlite File.

Also ensure that you are not using external tools to inspect the sqlite file. That could block I/O operations too.

Answered By – Chilu Mukuka

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published