使用bash合并多个SQLite数据库?

Mug*_*ara 1 linux sqlite bash

我有多个具有相同模式的sqlite数据库,我想将它们全部合并在一起,因为我有一个独特的列,可能存在重复的风险,我insert or ignore在sqlite中使用它很容易:

sqlite3 database.db3
sqlite> attach './db1.db3' as s1;
sqlite> attach './db2.db3' as s2;
sqlite> attach './db3.db3' as s3;
sqlite> insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s1.table;
sqlite> insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s2.table;
sqlite> insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s3.table;
sqlite> .exit
Run Code Online (Sandbox Code Playgroud)

我读了关于转储,但我不想合并整个架构,只是一个表,所以我想到了这个解决方案,现在直到这里一切都很好,但我需要通过bash运行所有这一切,我尝试了以下但它没有:

sqlite3 database.db3 "attach './db1.db3' as s1;"
sqlite3 database.db3 "attach './db2.db3' as s2;"
sqlite3 database.db3 "attach './db3.db3' as s3;"
sqlite3 database.db3 "select count(*) from s1.table;"
sqlite3 database.db3 "select count(*) from s2.table;"
sqlite3 database.db3 "select count(*) from s3.table;"
Run Code Online (Sandbox Code Playgroud)

它说 Error: no such table: s1.table

我究竟做错了什么

dog*_*ane 5

使用heredoc,如下所示:

sqlite3 database.db3 << "EOF"
attach './db1.db3' as s1;
attach './db2.db3' as s2;
attach './db3.db3' as s3;
insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s1.table;
insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s2.table;
insert or ignore into table (c1, c2, c3) select c1, c2, c3 from s3.table;
.exit
EOF
Run Code Online (Sandbox Code Playgroud)

或者,将所有命令放入文件中并按如下方式运行:

sqlite3 database.db3 < commandsFile
Run Code Online (Sandbox Code Playgroud)