0
votes

I'm running Sqlite on windows 7 and using a java program from within Eclipse, to attempt to load data from an input file, into a sqlite database table. The sqlite exe file, the java sqlite JDBC jar file and the database file are all in the same directory.

I've read most of the threads which sound like they were close to answering this problem but not solved so far.

I've changed the folder permissions so anybody/everybody has update/change access to the folder(where the db file is), but still no joy. I have established a database connection (see display below) but am now stuck and I've run out of ideas - can anybody help? The code fails on Line 30

public class Database {

public void loadPersonData() {
    Connection connection = null;
    PreparedStatement preparedStatement = null;

    try {
    connection = ConnectionConfig.getConnection();
    preparedStatement = connection
        .prepareStatement("INSERT INTO PERSON(PERSON_ID,LAST_NAME,FIRST_NAME,STREET,CITY) "
            + "VALUES(?,?,?,?,?)");

        for (PersonData x : ReadPersonData.personList) {
            preparedStatement.setInt(1, x.getPersonID());
            preparedStatement.setString(2, x.getLastName());
            preparedStatement.setString(3, x.getFirstName());
            preparedStatement.setString(4, x.getStreet());
            preparedStatement.setString(5, x.getCity());
            preparedStatement.addBatch();
        }

THIS IS LINE 30 ->>>>> preparedStatement.executeBatch();

    } catch (Exception e) {
        e.printStackTrace();

Currently the error messages are as below :-

database connection established

java.sql.SQLException: unable to open database file

at org.sqlite.core.DB.throwex(DB.java:859)
at org.sqlite.core.DB.executeBatch(DB.java:760)
at org.sqlite.core.CorePreparedStatement.executeBatch(CorePreparedStatement.java:77)
at Database.loadPersonData(Database.java:30)
at MainApp.main(MainApp.java:16)

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)

at org.sqlite.core.DB.newSQLException(DB.java:890)
at org.sqlite.core.CoreStatement.internalClose(CoreStatement.java:109)
at org.sqlite.jdbc3.JDBC3Statement.close(JDBC3Statement.java:35)
at Database.loadPersonData(Database.java:44)
at MainApp.main(MainApp.java:16)
1
WHats in ConnectionConfig Class ? - Sanjeev
hi Sanjeev, public class ConnectionConfig { public static Connection getConnection(){ Connection connection = null; try{ Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:C:\\Program Files (x86)\\SQLite\\SS200.db"); System.out.println("database connection established"); } catch (Exception e) {e.printStackTrace();} return connection; } } - Martin Clifford
Hi Sanjeev, I can't quite get this commenting to work, (newbie) the above code (sorry about the formatting!) is in the connectionConfig class to create the connection to the SQLite database, Cheers, Martin - Martin Clifford
Is the inserting data first operation as soon as you open database connection? also check in the database directory, if there is any lock file, it might be that database got locked by some operation accidentally. - Sanjeev
Hi Sanjeev, yes the insert is the first operation after opening the database. As to a lock file, can't see any likely file in the directory, does it remain after the db is closed or only there when the db is open? - Martin Clifford

1 Answers

0
votes

I think there are lock files locking your database. you must delete them go to your database directory and search for a file with "YourDbName-journal" delete it This should works if it did not work make sure that you have one connection running at a time and make sure you close your connection each time.

after catch clause you add this

    finally{
    try{
    preparedStatement.close();
    connection.close();}
    catch(Exception e){e.printStackTrace();}}

make sure to add this code after each method calling connection because sqlite does not support more than one connection.