3

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?

3
  • I see two possibilities: 1) the table doesn't exist, in which case you need to create it, 2) there is a typo in your database or table names. Commented Oct 29, 2012 at 22:02
  • Thanks for your answer. The table is there. I have testet it with a result set after the connection. But what do you mean with typo? Commented Oct 30, 2012 at 6:36
  • By a typo, I mean that you should double check that SendeDS is in fact the name of the table you are trying to query. Since I am a native English speaker, I see the possibility that you might have meant either SendDS or SenderDS instead. Such typos are very easy to overlook when you know what it should be. I'm just suggesting that you double check this. Commented Oct 30, 2012 at 23:03

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot. it's so easy and is works fine. Here is my new code: connNewDb.prepareStatement( "ATTACH DATABASE '" + dbName + "' AS fromDB").execute();
Do you even need to open a connection to old DB at all?
@Tacitus86 To quote the answer, "you don't need connOldDb".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.