How do I import tables from another database in sqlite?

前端 未结 3 551
北海茫月
北海茫月 2021-02-02 14:47

I have SQLite databases named database1 with a table t1 and database2 with a table t2. I want to import table t2

3条回答
  •  盖世英雄少女心
    2021-02-02 15:19

    You could use the sqlite3 .dump command to pipe the dump output into the other db. It takes an optional argument with the table name.

    db1=~/mydb1.sqlite 
    db2=~/mydb2.sqlite
    t=t2
    
    sqlite3 "$db2" ".dump $t" | sqlite3 "$db1"
    

    If you have no common tables in both databases, you can leave out the table name and copy all tables.

    If the tables are big, this may be slow because it will do INSERTs. If they are huge, and it is really too slow, maybe .import would be faster. You could try something like

    sqlite3 "$db2" ".schema $t" | sqlite3 "$db1"
    sqlite3 "$db2" "SELECT * FROM $t" | sqlite3 "$db1" ".import /dev/stdin $t"
    

提交回复
热议问题