Davor Josipovic
Davor Josipovic

Reputation: 5504

Open SQLite console and execute SQL without exiting

For convenience, I want to make a batch file that opens a terminal, runs SQLite and connects to a database, attaches some other database, and leaves the SQLite terminal open.

For example:

#!/bin/bash

sqlite3 -interactive "/db.sqlite" <<EOS
  ATTACH DATABASE "/sb2.sqlite" AS db2;
EOS

The problem is that sqlite3 quits right after executing the command.

Any ideas on how to leave it open?

Upvotes: 0

Views: 240

Answers (1)

Shawn
Shawn

Reputation: 52374

The issue is that sqlite3 automatically exits when it gets an EOF from standard input, which it does at the end of the heredoc. Luckily, one of the command line options it takes is -cmd SQL, which is evaluated before it starts reading from standard input. So, your script might look something like:

#!/bin/sh
sqlite3 -interactive -cmd "ATTACH '/sb2.sqlite' AS db2" /db.sqlite

Alternatively, if you always want this run every time you start the sqlite3 client, you can put it in your ~/.sqliterc file, or use -init /path/to/other/file.

Upvotes: 1

Related Questions