Re: [sqlite] [OT] suggestion for shell script and variable interpolation

2016-11-09 Thread Richard Hipp
On 11/9/16, Luca Ferrari  wrote:
> Hi all,
> this could be trivial, but assuming I need some shell script to query
> SQLite3 databases with variable-interpolated queries, what can I do?

I typically using "tclsh" for this. https://www.tcl-lang.org/

SQLite is really a TCL extension that escaped into the wild, so it
works remarkably well with TCL.  And TCL was originally conceived as a
kind of shell language.

-- 
D. Richard Hipp
d...@sqlite.org
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] [OT] suggestion for shell script and variable interpolation

2016-11-09 Thread ravi.shan...@cellworksgroup.com

On 11/09/2016 02:39 PM, Luca Ferrari wrote:

Hi all,
this could be trivial, but assuming I need some shell script to query
SQLite3 databases with variable-interpolated queries, what can I do?
Of course the following does not work because ticks prevent variable
interpolation:

COUNT=`sqlite3 $db 'SELECT COUNT(*) FROM foo WHERE baz=$BAZ'`

and the only ugly solution I thought is to use a temp file to write
down the query:

echo "SELECT COUNT(*) FROM foo WHERE baz=$BAZ">  $$.sql
COUNT=`sqlite3 $db<  $$.sql`

but I'm sure there's a better and much more elegant approach.

Thanks,
Luca
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

This will work and i have tried in my system.

query="SELECT COUNT(*) FROM foo WHERE baz='$BAZ'"
result=$( sqlite3 dbpath "$query" )

source : 
http://stackoverflow.com/questions/15314441/what-is-the-proper-quoting-to-assign-an-interpolated-sqlite3-query-to-a-variable

___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] [OT] suggestion for shell script and variable interpolation

2016-11-09 Thread Wout Mertens
COUNT=$(sqlite3 "$db" "SELECT COUNT(*) FROM foo WHERE baz='$BAZ'") should
totally work (I quoted $BAZ as a string, don't do that if it is a number,
and you should escape any ' in $BAZ).

On Wed, Nov 9, 2016 at 11:40 AM Clemens Ladisch  wrote:

> Luca Ferrari wrote:
> > this could be trivial, but assuming I need some shell script to query
> > SQLite3 databases with variable-interpolated queries, what can I do?
> > Of course the following does not work because ticks prevent variable
> > interpolation:
> >
> > COUNT=`sqlite3 $db 'SELECT COUNT(*) FROM foo WHERE baz=$BAZ'`
>
> You did not mention the shell, but double quotes should work.
>
>
> Regards,
> Clemens
> ___
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] [OT] suggestion for shell script and variable interpolation

2016-11-09 Thread Clemens Ladisch
Luca Ferrari wrote:
> this could be trivial, but assuming I need some shell script to query
> SQLite3 databases with variable-interpolated queries, what can I do?
> Of course the following does not work because ticks prevent variable
> interpolation:
>
> COUNT=`sqlite3 $db 'SELECT COUNT(*) FROM foo WHERE baz=$BAZ'`

You did not mention the shell, but double quotes should work.


Regards,
Clemens
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] [OT] suggestion for shell script and variable interpolation

2016-11-09 Thread Luca Ferrari
Hi all,
this could be trivial, but assuming I need some shell script to query
SQLite3 databases with variable-interpolated queries, what can I do?
Of course the following does not work because ticks prevent variable
interpolation:

COUNT=`sqlite3 $db 'SELECT COUNT(*) FROM foo WHERE baz=$BAZ'`

and the only ugly solution I thought is to use a temp file to write
down the query:

echo "SELECT COUNT(*) FROM foo WHERE baz=$BAZ" > $$.sql
COUNT=`sqlite3 $db < $$.sql`

but I'm sure there's a better and much more elegant approach.

Thanks,
Luca
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users