Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach Sqlite Database in Java

I have a problem to attach a Database and insert all rows from attached databases to the main.

Here is my code.

public void selectOldDb(String dbName) throws Exception {
    createNewDB();
    Class.forName("org.sqlite.JDBC");

    Connection connOldDb = DriverManager.getConnection("jdbc:sqlite:"+ dbName);
    String newDbName = getDirToNewDb();

    newDbName = newDbName + "auftraege.db";
    Connection connNewDb = DriverManager.getConnection("jdbc:sqlite:"+ newDbName);

    connNewDb.prepareStatement("ATTACH DATABASE \"" + connOldDb + "\" AS  fromDB").execute();

    connNewDb.prepareStatement("INSERT INTO main.auftraege  SELECT * FROM fromDB.SendeDS").execute();

    connNewDb.close();
    connOldDb.close();
}

I become this error when i try to insert.

[SQLITE_ERROR] SQL error or missing database (no such table: fromDB.SendeDS)

What am I doing wrong?

like image 413
user1761808 Avatar asked May 11 '26 09:05

user1761808


1 Answers

The ATTACH DATABASE command expects a file name, but you give it the representation of a Connection object.

You don't need connOldDb, just use dbName instead.

like image 172
CL. Avatar answered May 13 '26 22:05

CL.