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

Reply via email to