TransWikia.com

how to pass multiple commands to sqlite3 in a one liner shell command

Unix & Linux Asked by kenn on February 4, 2021

I’m trying to save clipboard content into an sqlite database. Created database and tables.

I don’t want it to create journal file in every clipboard change, so I tried to pass PRAGMA journal_mode = OFF; flag. But it’s tricky to pass those commands in a one liner commands because sqlite only accepts two command like

sqlite3 clipboard_archive.db "insert into cb (cb_context) values ('clipboard');"

it works.
I looked for Q&A sites, some advises echoing the commands in the following way.

echo "PRAGMA journal_mode = OFF;" | sqlite3 clipboard_archive.db "insert into cb (cb_context) values ('clipboard');"

But PRAGMA journal_mode = OFF; doesn’t take effect in that way though it works within the sqlite3 command prompt.

What’s wrong with my one liner script?

3 Answers

It seems that you can use the -cmd switch not only once. This also works for me (SQLite 3.8.7.2 on Windows):

sqlite3.exe yourdb.db -cmd ".mode column" -cmd ".headers on" "select * from persons;"

Answered by chris on February 4, 2021

derobert's answer doesn't seem to work with dot-commands, but you can use -cmd: sqlite3 tolls.sql3 -cmd ".mode csv" ".import tolls.csv tolls"

it accepts multiple -cmd commands and executes them in order, before the final arg.

Answered by jcomeau_ictx on February 4, 2021

Not sure why you want to use SQLite if you don't want the journal (have you considered the much faster WAL mode if speed is a concern?) but you can give multiple commands separated by semicolons:

sqlite3 clipboard_archive.db "PRAGMA journal_mode = OFF; insert into cb (cb_context) values ('clipboard');"

Answered by derobert on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP