SQLite User Forum

DOT commands - Is there a way to concatenate multiple commands?
Login

DOT commands - Is there a way to concatenate multiple commands?

(1) By anonymous on 2022-04-06 19:12:22 [link] [source]

If I need to run multiple dot commands before running an SQL statement e.g.

.open './db/chinook.db'
,mode column
select * from employees;

1 - Is there a way to concatenate the dot commands?

Without success, I tried:

sqlite> .mode column .open './db/chinook.db'
extra argument: "./db/chinook.db"
2 - If concatenation of dot commands is possible, is it possible to concatenate sql statements also?

(2) By Larry Brasfield (larrybr) on 2022-04-06 19:34:10 in reply to 1 [link] [source]

Is there a way to concatenate the dot commands?

Not now. Maybe later.

If concatenation of dot commands is possible, is it possible to concatenate sql statements also?

SQL statements can be concatenated today. (Not sure what to make of "also".)
For example: select 1; select 2; .

Just be aware that if any line of your SQL ends with semicolon, the shell will try to run it just after seeing that terminator. So the trick would be to begin any subsequent SQL statements on a line after the semicolon terminating the previous SQL statement.

But why do you want to concatenate? Does that accomplish something that sequential execution by the shell REPL does not?

(5) By anonymous on 2022-04-06 19:55:35 in reply to 2 [source]

To clarify further:

  1. I am using Windows.

  2. Nothing from this hint works for me.

  3. SQLITE3.EXE is wrapped around Command Prompt and the windows command prompt permits concatenation; the command prompt ways for concatenation do NOT work in SQLite3.exe.

As regards the questions you raise:

(Not sure what to make of "also".)

Concatenate SQL statements with dot commands; I'm aware that I can concatenate SQL statements.

But why do you want to concatenate? Does that accomplish something that sequential execution by the shell REPL does not?

I am trying to use SQLITE3.EXE as an external process with stdIn, stdOut, stdErr redirection; that is, I need t write the commands (including SQL statements) to stdIn. I can write the commands sequentially, sure , but that complicates the handling of stdOut as some (eg dot) commands DO NOT write to stdOut. Concatenation will allow me to ensure that every time I write to stdIn, there is somethin in stdOut to handle.

(7) By Kees Nuyt (knu) on 2022-04-06 21:10:03 in reply to 5 [link] [source]

Can't you just change the program that constructs the stdin for SQLite in a way that it inserts carriage-return and/or linefeed between the commands?

Something like (in your example):

write(stdout,".open './db/chinook.db'<cr><lf>.mode column<cr<lf>select * from employees;")

where 
   <cr>=chr(13)
   <lf>=chr(10)
-- 
Regards,
Kees Nuyt

(9) By anonymous on 2022-04-06 21:48:35 in reply to 7 [link] [source]

Perfect!

In my rush, I read your suggestion literally only to meet the error

extra argument: "<cr><lf>.mode"

on stdErr.

(On learning my lesson), then, I substituted <cr><lf> for the actual characters and it worked!

So, instead of multiple writes to stdIn (with only some of them writing to stdOut) I now have ONE write to stdIn and KNOW that stdOut requires handling.

Thank you very much. Much appreciated.

(3.2) By midijohnny on 2022-04-06 19:50:00 edited from 3.1 in reply to 1 [link] [source]

Nope - the docs are explicit on this. See the section that begins with

"A dot-command has a more restrictive structure:"

Depending on what you are doing though - maybe the 'init' flag might of use here. Create a text file of your dot commands (and anything else you need to do).

e.g.

# init.sql
pragma foreign_keys=on;
.headers on
.mode column

And then call as follows:

sqlite3 -init init.sql
-- Loading resources from init.sql
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> values('hello','world');
column1     column2   
----------  ----------
hello       world 

Or call within sqlite3 itself:

.read init.sql

You can also just create a file in $HOME/.sqliterc which will be called automatically when the '-init' flag isn't specified. Also note - some common options are available directly on the commandline ; for instance:

sqlite3 -column

(6) By David Raymond (dvdraymond) on 2022-04-06 20:09:13 in reply to 3.2 [link] [source]

Similarly, you can use .read to read commands from a text file. So if you've got a whole bunch of dot commands and/or SQL statements to run just put them in a file and .read file_name_here

If you do that I would also recommend considering adding ".bail on" to the top. Which will set it so that if one of the lines in your file errors out, then it will stop processing there and not just blindly continue with all the things in the rest of the file.

(8) By anonymous on 2022-04-06 21:10:17 in reply to 6 [link] [source]

.bail on

Thanks for the tip; I was unaware of it.

(4) By Keith Medcalf (kmedcalf) on 2022-04-06 19:42:33 in reply to 1 [link] [source]

1 - Is there a way to concatenate the dot commands?

No. a "dot command" is a command to the application accepting that input, such as the SQLite3 Command Line Application. It is an application command. The application command requires that the first character be a dot (.) and the application will treat that as a command to be processed by the application.

is it possible to concatenate sql statements also?

No. SQL commands are procesed one after each upon presentement to the SQLite3 Library. While an application may make it appear that the SQL commands can be concatenated (whatever that means), this is merely an illusion. Each SQL statement is processed one at a time, side after each.

You are entirely free to write an application that behaves in whatever manner you desire, including permitting the illusion of "concatenation" (whatever that is).