Re: sqlite help needed with my proc....



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

.



Relevant Pages

  • Re: cant get join on two large tables to use_nl or indexes
    ... In the mean time hear is the plan for the query. ... ccp is the ... and sid column is not a bitmap index. ... TABLE ACCESS OF STR (Cost=60153 Card=1293923 ...
    (comp.databases.oracle.misc)
  • Re: Server.URLEncode :-(
    ... "Peter Foti" wrote in message ... >> that doesnt work tho, it completely ignores it and still generates a ' ... >> Function SQL_QUOTE (str) ... > fieldnames "COL1" and "COL2" in the query above that. ...
    (microsoft.public.inetserver.asp.general)
  • RE: Build A User Defined STR Statement Using Access 2000
    ... few codes to check, I will use the text field method. ... I have taken an extract from a query which I currently use to find specific ... I would like a variable to buld up a resulting STR ... I then want the database to automatically build a query with the STR ...
    (microsoft.public.access.modulesdaovba)
  • Re: Internation Currency & Number formats
    ... Use Str() instead of CStror Format() to generate the decimal value in the ... SQL query. ...
    (microsoft.public.vb.general.discussion)
  • Re: how to link to a csv file with a date field in it?
    ... Regarding the scheduler it's in XP and above - if you aren't familiar with using the Windows Task Scheduler, here is a link to a fairly clean, simple, short tutorial. ... I think you have the right idea - link to the csv, maybe run an append query to process the contents into your main table, in that query you can fix things such as the date. ... s is the complete row of data as a string, flag is a boolean that is initialized to be False. ... The full code I snipped this from contains so much checking for quotes and # hashes and internal commas and internal single quotes replacing certain chars, that it's difficult for me to follow and I wrote it. ...
    (microsoft.public.access.externaldata)