Merging sqlite databases through a bash script

谁说我不能喝 提交于 2020-01-25 08:45:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!