I finally figured out how to load multiple "dot commands" or settings 
from the command line tool. (Some users only have the default binary to 
rely on, ya know.) Since I have never found this information on the web 
before, I thought I would post it here to share the information.

To load options before doing the query, they have to be separated from 
the query by a hard return, in the quoted string itself.

So:

 > sqlite3 -header -column tmp.db ".width 5 30; select * from data;"

doesn't really change the column widths.

But, doing this does:

 > echo ".width 5 30\n select * from data;" | sqlite3 -header -column tmp.db

To load multiple options, you have to put them on different lines, with 
the query on the last line:

 > echo ".width 5 30\n.timeout 15000\n select * from data;" | sqlite3 
-header -column tmp.db

Some systems I have tried this on /required /only a single hard return 
after the last option, with a semicolon between the multiple settings. 
Others required the hard returns without any semicolons. Experiment on 
your own platform/binary version.

I don't know which versions/flavors this works on, but it works for me 
finally.

Note: Your version of echo has to support turning '\n' into a hard 
return. Not all do.  If not, you can do something like a perl -e to do 
it as well.

All the examples on websites show applying the dot commands from a 
sqlite> prompt.  Doing the hard returns within the string makes it look 
like a user typing the commands in, with the hard returns.

If anyone could fix the parser in the sqlite3 source code, I'm sure many 
novices would greatly appreciate it.  The above trick has eluded me for 
several months of working with sqlite3 from the command line, and is 
still cumbersome, although usable now.

One other trick is to put the options in a .sqliterc file in your home 
directory, one dot command per line.  This works, but is not really 
feasible in an environment where it will run on multiple hosts and you 
don't control other accounts that will run it. Also, you can't pick and 
choose which ones are loaded. They are global settings at that point.

I hope this helps someone out there looking for this, as well as 
possibly getting the parser fixed to make it easier to use in the future.

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to