I have 2 SQLite databases with common data but with different purposes and I wanted to avoid reinserting data, so I was wondering if it was possible to copy a whole table from one database to another?
相关问题
- NOT DISTINCT query in mySQL
- SQL/SQL-LITE - Counting records after filtering
- Flush single app django 1.9
- keeping one connection to DB or opening closing pe
- What SQLite NuGet package do I need
You'll have to attach Database X with Database Y using the ATTACH command, then run the appropriate Insert Into commands for the tables you want to transfer.
Or, if the columns are not matched up in order:
Objective-C code for copy Table from a Database to another Database
Consider a example where I have two databases namely allmsa.db and atlanta.db. Say the database allmsa.db has tables for all msas in US and database atlanta.db is empty.
Our target is to copy the table atlanta from allmsa.db to atlanta.db.
Steps
ATTACH '/mnt/fastaccessDS/core/csv/allmsa.db' AS AM;
note that we give the entire path of the database to be attached.sqlite> .databases
you can see the output asINSERT INTO atlanta SELECT * FROM AM.atlanta;
This should serve your purpose.
Easiest and correct way on a single line:
The primary key and the columns types will be kept.
For one time action, you can use .dump and .read.
Dump the table my_table from old_db.sqlite
Read the dump into the new_db.sqlite assuming the table there does not exist
Now you have cloned your table. To do this for whole database, simply leave out the table name in the .dump command.
Bonus: The databases can have different encodings.
I needed to move data from a sql server compact database to sqlite, so using sql server 2008 you can right click on the table and select 'Script Table To' and then 'Data to Inserts'. Copy the insert statements remove the 'GO' statements and it executed successfully when applied to the sqlite database using the 'DB Browser for Sqlite' app.