Re: sqlite help needed with my proc....
- From: Nick Hounsome <nick.hounsome@xxxxxxxxxxxxxx>
- Date: Sat, 23 Jun 2007 23:37:21 -0700
On Jun 24, 3:02 am, billpo...@xxxxxxxxxxxx wrote:
On Jun 23, 2:05 pm, Mel <MelHer...@xxxxxxxxx> wrote:
What is wrong with this proc and how can i fix it.
Please help
----------------------------------------------------------------------------------
file delete -force test.db
sqlite dbh test.db
dbh eval "create table departments (id int, name char(50))"
###########################################
#####
###########################################
proc query {qstr} {
sqlite dbh test.db
if {[catch {set rval [dbh eval $qstr]} e] != 0} {
puts $e
exit
}
puts >>>$rval|$qstr<<<
return $rval
}
query "delete from departments"
set str "Al's automotive"
query "insert into departments values (1000,$str)"
i get :
>>>|delete from departments<<<
near "'s automotive)": syntax error
sql wants quotes around str to tell it that it is a string.
It works if you write:
query "insert into departments values (1000,\"$str\")"
The immediate cause of your problem is that single quotes are a valid
quoting mechanism for SQL (not just sqlite) so you have an unbalanced
quote. The suggestion to double quote works because double quotes
protect single (and vise versa) but it is not a general solution (e.g.
try it with set str un"balanced'quotes\\n )
This mechanism is never going to work for general strings because you
cannot guarantee the quoting will come out right in the arg string.
One way that will work is to use sqlite's own expansion but then you
need to ensure that the variable is visible in the correct scope:
# ensure that $::str is visible to sqlite
global str
set str "Al's automotive"
# Note curly brackets - db eval expands $::str and (logically) does
the necessary quting
query {insert into departments values (1000,$::str)}
Another way if you didn't want to use globals would be to pass the
variable names in to query and upvar them to themselves.
Then you would call
query {insert into departments values (1000,$str)} str
and query would be
#args is a list of variable names used in the query
proc query {qstr args} {
foreach var $args { upvar 1 $var $var }
...
All very clever but I suspect that the fundamental design is wrong
anyway and that you should reconsider it carefully before using any
technical fixes
Nick
.
- References:
- sqlite help needed with my proc....
- From: Mel
- Re: sqlite help needed with my proc....
- From: billposer
- sqlite help needed with my proc....
- Prev by Date: Re: DB problem with strings....
- Next by Date: Re: How to compile this simple tcltest.c?
- Previous by thread: Re: sqlite help needed with my proc....
- Next by thread: Re: sqlite help needed with my proc....
- Index(es):
Relevant Pages
|