0

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.

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

it's a delimiter, <<EOF is also commonly used
0
find . -name "*.db" -exec sqlite3 {} .dump \; | sqlite3 new_db

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.