问题
Thanks to previous SO posts I can now merge sqlite databases.
But I want to run this merging in an automatic way using a bash script,
I have tried various ways and here is my latest and most complete fail:
## DB to merge
I1=d1.db
I2=d2.db
## Command to run
CMD='attach \'d2.db\' as toMerge; BEGIN; insert into sae select * from toMerge.sae; COMMIT; detach toMerge;'
echo $CMD
## Pass to sqlite
sqlite3 $I1 $CMD
Two issues meet here:
1- passing a bash variable (here d2.db). I had tried with the quote function as well:
CMD='attach quote('$I2') as toMerge; BEGIN; insert into sae select * from toMerge.sae; COMMIT; detach toMerge;'
2- the * in the select command that is interpreted in the bash way, hence the single quotes.
回答1:
This works
d1=d1.db
d2=d2.db
sqlite3 "$d1" <<!
attach database "$d2" as m;
insert into sae select * from m.sae;
detach m;
!
using a heredoc
回答2:
find . -name "*.db" -exec sqlite3 {} .dump \; | sqlite3 new_db
来源:https://stackoverflow.com/questions/49637071/merging-sqlite-databases-through-a-bash-script