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?
Sujay
6,7812 gold badges32 silver badges49 bronze badges
1 Answer 1
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.
answered Oct 30, 2012 at 8:13
CL.
182k18 gold badges241 silver badges282 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
user1761808
thanks a lot. it's so easy and is works fine. Here is my new code: connNewDb.prepareStatement( "ATTACH DATABASE '" + dbName + "' AS fromDB").execute();
Tacitus86
Do you even need to open a connection to old DB at all?
CL.
@Tacitus86 To quote the answer, "you don't need
connOldDb".default
SendeDSis 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 eitherSendDSorSenderDSinstead. Such typos are very easy to overlook when you know what it should be. I'm just suggesting that you double check this.