----- Original Message ----
From: Martin Jenkins <[EMAIL PROTECTED]>
To: sqlite-users@sqlite.org
Sent: Friday, June 22, 2007 2:00:45 PM
Subject: Re: [sqlite] Capturing output from SQLlite with variables in a BASH 
script


litenoob wrote:
> Hi, I'm wondering how to write a BASH script that will capture my SQLite
> output.
> 
> I can do it for a single line with something like this:
> 
> somevar=`sqlite3 dbfilename "SELECT name FROM tablename WHERE name='smith'
> LIMIT 1;"`
> 
> However, if I want to do anything with multiple lines, I haven't figured out
> a good way.  So far, I'm using a workaround by outputting to a file, then
> reading it after I exit the SQLite commandline, but that slows down the
> script significantly.
> 
> e.g.
> 
> sqlite3 dbfilename << EOF
> 
> .output temp1
> select id from tablename where name = "bush";
> .output temp2
> select id from tablename where name = "osama";
> 
> .quit
> EOF
> 
> read id1 < temp1
> read id2 < temp2
> 
> What's the better way to do this without actually writing to a file?
> 
> Thanks!

If you're using bash you can simply do something like:
sqlite3 dbfilename 'SELECT name FROM tablename WHERE name="smith"' | ( while 
read name ; do echo "--> $name" ; done )

You can actually put whatever you want within parenthesis (even more 
parenthised goodness).

That, or use a scripting language like perl or python :)

Nicolas

Reply via email to