There's got be an easy way to copy a table from sqlLite DB1 to DB2

I just want to copy a table from one database file to another. The table doesn’t have a primary key nor default values, it’s just fields and data.

This has got to be easy but I can’t find it.

Ideas?

1 Like

SQL SELECT INTO Statement (w3schools.com)

just missing the to another database file part.

Thanks,

Use the ATTACH statement. https://www.sqlite.org/lang_attach.html

Then you can use SELECT INTO to copy the table between the two.

You can attach a 2nd database file using http://docs.xojo.com/SQLiteDatabase.AddDatabase, then copy data to or from the 2nd database file.

Well, I learned something new. I didn’t notice the AddDatabase feature in 2019r2. Time to revamp some of my code…

1 Like

So I tried this: wp.cc.teamsDB.addDatabase(userArchiveDB.databaseFile, “archive”) and got an error that says "not enough arguments: missing FolderItem value for parameter “file”

Any ideas here?

I also tried it with attach and detach database. The tables were not saved in the new database file.

Something like:


Var dbh as sqlitedatabase

// Here, connect dbh to your source database

dbh.ExecuteSQL ("attach database '/path/to/second/db' as db2")
dbh.ExecuteSQL ("create table db2.tablename as select * from main.tablename")
dbh.ExecuteSQL ("insert into db2.tablename select * from main.tablename")

.
is what you want. I do this all the time, although in my case I’m moving a single row from one database to another.

tablename is whatever the name of your table to be copied is.

So, you want to copy a TABLE you do not created ?

And this is a SQLite file ?

The password parameter must be specified. Try:

wp.cc.teamsDB.addDatabase(userArchiveDB.databaseFile, "archive", "")

Yes sqlite. It just seems like copying the whole table from one database to another should be easy.

Thanks. fyi, the language reference has an example without the password:

Var addDBFile As New FolderItem(“AddDB.sqlite”)

Try
currentDB.AddDatabase(AddDBFile, “locations”)
MessageBox(“Database attached.”)

Catch error As IOException
MessageBox(“The database could not be added.”)

End Try

It is, see my post.

As easy as it gets . . .

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.