Home > database >  How can I quit sqlite from a command batch file?
How can I quit sqlite from a command batch file?

Time:09-22

I am trying to create a sealed command for my build pipeline which inserts data and quits.

So far I have created my data files

things-to-import-001.sql and 002 etc, which contains all the INSERT statements I'd like to run, with a file per table.

I have created a command file to run them

-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.quit

However when I run my command

sqlite3 -init ./import-all.sql ./database.sqlite

..the data is inserted, but the program remains running and shows the sqlite> prompt, despite the .quit command. I have also tried using .exit 0.

From the sqlite3 --help

 -init FILENAME       read/process named file

Docs: https://www.sqlite.org/cli.html#reading_sql_from_a_file

How can I tell sqlite to exit once my inserts have finished?

CodePudding user response:

I have managed to find a dirty workaround for this issue.

I have updated my import file to include a bad command, and executed using -bail to quit on first error.

-- import-all.sql
.read ./things-to-import-001.sql
.read ./things-to-import-002.sql
.fakeErrorToQuitWithBail

Then you can execute with

sqlite3 -init import-all.sql -bail

and it should quit with

Error: unknown command or invalid arguments: "fakeErrorToQuitWithBail". Enter ".help" for help

CodePudding user response:

Try using ".exit" at the place of ".quit". For some reason SQLite dont doccumented this commands.

https://www.tutorialspoint.com/sqlite/sqlite_commands.htm

  • Related