0

I want to detach the current sqlite database(OLD) before i attach the NEW sqlite database. But when I execute below code:

Statement statement = connection.createStatement();
statement.execute("DETACH database '" + alias+"'");
statement.close();**

I am getting following error:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such database: OLD)

I have attached the database using the below code :

SQLiteDataSource dataSource = new SQLiteDataSource();
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
statement.execute("ATTACH DATABASE '" + path + "' AS " + alias);
statement.close();**

Can anybody please help me where i am doing wrong?

Anantha
  • 31
  • 6
  • if i try to execute any query for NEW and OLD databases independently they work fine and get all the required resultSet for NEW and OLD. But when i put them in same code and query them(attaching NEW database file first, querying it, then attaching the OLD and then querying OLD), i am getting duplicate data in resultSet of OLD. So i want to detach the NEW before i query OLD – Anantha Jul 18 '17 at 06:03
  • 1
    Did you try it without the single quotes around the alias (as specified in the documentation)? – Dawood ibn Kareem Jul 18 '17 at 06:05
  • @DawoodibnKareem yes, i tried 1. `statement.execute("DETACH database '" + alias+"'");` 2. `statement.execute("DETACH database " + alias);` 3. `statement.execute("DETACH '" + alias+"'");` 4. `statement.execute("DETACH " + alias);` All 4 of them gives same error :( – Anantha Jul 18 '17 at 06:06
  • @user7294900 can you pls tell how to detach with PATH – Anantha Jul 18 '17 at 06:07
  • Are you using the same connection object? – CL. Jul 18 '17 at 08:22
  • @CL. yes `SQLiteDataSource dataSource = new SQLiteDataSource(); connection = dataSource.getConnection(); ` but i am calling the method which has above code, each time i want to connect to db. – Anantha Jul 19 '17 at 04:07

1 Answers1

0

Resolved the issue by creating new reference each time i am calling the the method for initializing database connection :)

Anantha
  • 31
  • 6