Re: [sqlite] How accept sqlite3 commands from stdin
On Thu, Jun 24, 2010 at 9:19 AM, Eric Smithwrote: > Peng Yu wrote: > >> On Thu, Jun 24, 2010 at 5:05 AM, Simon Slavin wrote: >> > >> > On 24 Jun 2010, at 4:50am, Peng Yu wrote: >> > >> >> Is there a way to use Shebang for sqlite3 script? >> >> >> >> http://en.wikipedia.org/wiki/Shebang_%28Unix%29 >> > >> > SQLite comes with a command-line tool. You can feed it with individual >> > commands on a Unix command-line or tell it to read commands from a file. >> >> I was asking whether it is possible to use Shebang with sqlite script. >> If it is possible, would you please show me how to modify the >> following script to do so? >> >> $ ./main.sql >> Error: unknown command or invalid arguments: "/main.sql". Enter >> ".help" for help >> $ cat main.sql >> #!/usr/bin/sqlite3 main.db >> >> create table tbl1(one varchar(10), two smallint); >> .quit > > You want to pretend a .sql file is an executable and send commands to > sqlite3? Try something like this: > > [hudson:~] $ cat foo.sql > #!/bin/sh > > sqlite3 main.db < > create table tbl1(col1 text); > insert into tbl1 values('foobar'); > > EOF > [hudson:~] $ chmod +x foo.sql > [hudson:~] $ ./foo.sql > [hudson:~] $ lh main.db > -rw-r--r--. 1 eas eas 2.0K Jun 24 10:18 main.db > [hudson:~] $ sqlite3 main.db "select * from tbl1" > foobar > [hudson:~] $ This is helpful. Thank you! -- Regards, Peng ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
On Thu, Jun 24, 2010 at 10:36:20AM -0400, Pavel Ivanov scratched on the wall: > > I was asking whether it is possible to use Shebang with sqlite script. > > If it is possible, would you please show me how to modify the > > following script to do so? > > If you don't mind one error message then you can do it like this: > > > cat test.sql > #!/usr/bin/sqlite3 -init This works, but you must give the database file on the command line (or use an anonymous database). You might also want to add -batch. As best I can figure, the style the OP asked about (where the database file is part of the script) can't be done without a wrapper. This is still pretty useful, however. > Otherwise it's impossible to it because '#' is not a comment-starting > symbol in sqlite command line utility. I wonder if it would be worth creating a -initcli or something that simply ignored the first line of input. It could also imply -batch. -j -- Jay A. Kreibich < J A Y @ K R E I B I.C H > "Intelligence is like underwear: it is important that you have it, but showing it to the wrong people has the tendency to make them feel uncomfortable." -- Angela Johnson ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
On 24 Jun 2010, at 3:13pm, Peng Yu wrote: > I was asking whether it is possible to use Shebang with sqlite script. > If it is possible, would you please show me how to modify the > following script to do so? > > $ ./main.sql > Error: unknown command or invalid arguments: "/main.sql". Enter > ".help" for help > $ cat main.sql > #!/usr/bin/sqlite3 main.db > > create table tbl1(one varchar(10), two smallint); > .quit This is how to send a single command to sqlite3: $ sqlite3 mydatabase.db 'CREATE TABLE myTable (name TEXT, value INTEGER);' Put as many commands as you like in the quotes: $ sqlite3 mydatabase.db "CREATE TABLE myTable (name TEXT, value INTEGER);INSERT INTO myTable VALUES ('fred', 3);SELECT * FROM myTable" fred|3 and put as many commands like that as you like in your shell script. But it's not a neat way of scripting the command-line tool because it requires you to create a shellscript with SQL commands in: two languages in the same file. It's neater to make up a proper .sql file with just the SQL commands in, then tell the command-line tool to execute the commands from the file. Here is one way to send multiple commands to sqlite3: $ cat makemydb.sql CREATE TABLE myTable (name TEXT, value INTEGER); INSERT INTO myTable VALUES ('fred', 3); SELECT * FROM myTable; $ sqlite3 mydatabase.db '.read makemydb.sql' Here's how you would do it if you didn't know about the '.read' command: $ cat makemydb.sql | sqlite3 mydatabase.db Save the output in an output file: $ cat makemydb.sql | sqlite3 mydatabase.db > saved.txt Simon. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
> I was asking whether it is possible to use Shebang with sqlite script. > If it is possible, would you please show me how to modify the > following script to do so? If you don't mind one error message then you can do it like this: > cat test.sql #!/usr/bin/sqlite3 -init ; create table tbl1(one varchar(10), two smallint); .h on pragma table_info('tbl1'); .quit > ./test.sql -- Loading resources from ./test.sql Error: near line 1: unrecognized token: "#" cid|name|type|notnull|dflt_value|pk 0|one|varchar(10)|0||0 1|two|smallint|0||0 > Otherwise it's impossible to it because '#' is not a comment-starting symbol in sqlite command line utility. Pavel On Thu, Jun 24, 2010 at 10:13 AM, Peng Yuwrote: > On Thu, Jun 24, 2010 at 5:05 AM, Simon Slavin wrote: >> >> On 24 Jun 2010, at 4:50am, Peng Yu wrote: >> >>> Is there a way to use Shebang for sqlite3 script? >>> >>> http://en.wikipedia.org/wiki/Shebang_%28Unix%29 >> >> SQLite comes with a command-line tool. You can feed it with individual >> commands on a Unix command-line or tell it to read commands from a file. > > I was asking whether it is possible to use Shebang with sqlite script. > If it is possible, would you please show me how to modify the > following script to do so? > > $ ./main.sql > Error: unknown command or invalid arguments: "/main.sql". Enter > ".help" for help > $ cat main.sql > #!/usr/bin/sqlite3 main.db > > create table tbl1(one varchar(10), two smallint); > .quit > > > -- > Regards, > Peng > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
On Thu, Jun 24, 2010 at 09:13:01AM -0500, Peng Yu scratched on the wall: > I was asking whether it is possible to use Shebang with sqlite script. > If it is possible, would you please show me how to modify the > following script to do so? Not possible. Shebang assumes the input file can be given as the last argument. sqlite3's options structure is not setup that way. Even if it was, shebang generally only works on systems that understand "#" as a comment. You can get around the first issue with a small wrapper shell script, but I don't think you can fix the second problem without modifications to sqlite3. -j -- Jay A. Kreibich < J A Y @ K R E I B I.C H > "Intelligence is like underwear: it is important that you have it, but showing it to the wrong people has the tendency to make them feel uncomfortable." -- Angela Johnson ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
Peng Yu wrote: > On Thu, Jun 24, 2010 at 5:05 AM, Simon Slavinwrote: > > > > On 24 Jun 2010, at 4:50am, Peng Yu wrote: > > > >> Is there a way to use Shebang for sqlite3 script? > >> > >> http://en.wikipedia.org/wiki/Shebang_%28Unix%29 > > > > SQLite comes with a command-line tool. You can feed it with individual > > commands on a Unix command-line or tell it to read commands from a file. > > I was asking whether it is possible to use Shebang with sqlite script. > If it is possible, would you please show me how to modify the > following script to do so? > > $ ./main.sql > Error: unknown command or invalid arguments: "/main.sql". Enter > ".help" for help > $ cat main.sql > #!/usr/bin/sqlite3 main.db > > create table tbl1(one varchar(10), two smallint); > .quit You want to pretend a .sql file is an executable and send commands to sqlite3? Try something like this: [hudson:~] $ cat foo.sql #!/bin/sh sqlite3 main.db
Re: [sqlite] How accept sqlite3 commands from stdin
On Thu, Jun 24, 2010 at 5:05 AM, Simon Slavinwrote: > > On 24 Jun 2010, at 4:50am, Peng Yu wrote: > >> Is there a way to use Shebang for sqlite3 script? >> >> http://en.wikipedia.org/wiki/Shebang_%28Unix%29 > > SQLite comes with a command-line tool. You can feed it with individual > commands on a Unix command-line or tell it to read commands from a file. I was asking whether it is possible to use Shebang with sqlite script. If it is possible, would you please show me how to modify the following script to do so? $ ./main.sql Error: unknown command or invalid arguments: "/main.sql". Enter ".help" for help $ cat main.sql #!/usr/bin/sqlite3 main.db create table tbl1(one varchar(10), two smallint); .quit -- Regards, Peng ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
On 24 Jun 2010, at 4:50am, Peng Yu wrote: > Is there a way to use Shebang for sqlite3 script? > > http://en.wikipedia.org/wiki/Shebang_%28Unix%29 SQLite comes with a command-line tool. You can feed it with individual commands on a Unix command-line or tell it to read commands from a file. Simon. ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 06/23/2010 08:50 PM, Peng Yu wrote: > Is there a way to use Shebang for sqlite3 script? What happened when you tried it? Roger -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkwi8mYACgkQmOOfHg372QRlLgCeP9F1x0lyrZEJkOfjsqgVgaky EEYAoKiTb3XWpgXJx7Ss+bBVMYeW3aJQ =5lgT -END PGP SIGNATURE- ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
On Tue, May 25, 2010 at 3:25 PM, Black, Michael (IS)wrote: > sqlite3 main.db < main.txt > > or > cat main.txt | sqlite3 main.db > > or > > echo "create table tbl1(one varchar(10), two smallint);" | sqlite3 main.db Is there a way to use Shebang for sqlite3 script? http://en.wikipedia.org/wiki/Shebang_%28Unix%29 -- Regards, Peng ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] How accept sqlite3 commands from stdin
sqlite3 main.db < main.txt or cat main.txt | sqlite3 main.db or echo "create table tbl1(one varchar(10), two smallint);" | sqlite3 main.db Michael D. Black Senior Scientist Northrop Grumman Mission Systems From: sqlite-users-boun...@sqlite.org on behalf of Peng Yu Sent: Tue 5/25/2010 9:20 AM To: sqlite-users@sqlite.org Subject: [sqlite] How accept sqlite3 commands from stdin I got the following error when I try to read the commands from the command line. Would you please let me know how to let sqlite3 read from stdin? $ cat main.txt create table tbl1(one varchar(10), two smallint); $ echo main.txt |sqlite3 main.db Incomplete SQL: main.txt -- Regards, Peng ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] How accept sqlite3 commands from stdin
I got the following error when I try to read the commands from the command line. Would you please let me know how to let sqlite3 read from stdin? $ cat main.txt create table tbl1(one varchar(10), two smallint); $ echo main.txt |sqlite3 main.db Incomplete SQL: main.txt -- Regards, Peng ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users