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

Linux 使用bash合并多个SQLite数据库?,linux,bash,sqlite,Linux,Bash,Sqlite,我有多个具有相同模式的sqlite数据库,我想将它们合并在一起,因为我有一个唯一的列,并且可能存在重复的风险,我使用的是插入或忽略,在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

我有多个具有相同模式的sqlite数据库,我想将它们合并在一起,因为我有一个唯一的列,并且可能存在重复的风险,我使用的是
插入或忽略
,在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
我读过关于dump的文章,但我不想合并整个模式,只合并一个表,所以我考虑了这个解决方案,现在一切正常,但我需要通过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;"
它说
错误:没有这样的表:s1.表

我做错了什么

使用如下所示的

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
sqlite3 database.db3使用如下所示的

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
sqlite3 database.db31)创建一个文本文件,其中包含要输入sqlite命令行程序的行,如下所示:

sqlite3 database.db3 < commandsFile
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
然后只需调用
sqlite3 database.db

2) 使用外壳

#!/bin/bash 

sqlite3 database.db <<"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
#/bin/bash
sqlite3 database.db1)创建一个文本文件,其中包含要输入sqlite命令行程序的行,如下所示:

sqlite3 database.db3 < commandsFile
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
然后只需调用
sqlite3 database.db

2) 使用外壳

#!/bin/bash 

sqlite3 database.db <<"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
#/bin/bash
sqlite3数据库.db