Shlomi Fish | 22 Dec 2006 10:33
Picon
Gravatar

Placeholders and/or Quoting in CLSQL

Hi all!

I'm trying to translate the following Amarok script I wrote in Perl into Lisp:

http://www.kde-apps.org/content/show.php?content=49151

Now in the script I used SQL with placeholders:

<<<<<<<<
$statement = $dbh->prepare("SELECT * FROM people WHERE first_name = ?");

$results = $statement->execute("Natan");
>>>>>>>>

However, I'm unable to find anything similar in CLSQL. Is there anything like 
that?

An alternative would be to quote the values into SQL strings using the 
database-safe quoting mechanism. From what I understood from the 
documentation, that's what the (clsql:sql) function does. However, the 
following code:

<<<<<<<<<<<<
(require 'clsql)

(let*
  (
   (db 
(clsql:connect '("/home/shlomi/.kde/share/apps/amarok/scripts-data/per-song-volume.sqlite")
                :database-type :sqlite3)
       ))
  (print db)
  (print (clsql:query "SELECT path, volume FROM songs_volumes" :database 
db :field-names nil))
  (print (clsql:sql :database db "Hello 'Hi Ho ' Please ' Got"))
  )
>>>>>>>>>>>>

generates the following error:

<<<<<<<<<<<
debugger invoked on a SIMPLE-ERROR:
  Error during processing of --eval option (LOAD #P"db2.lisp"):

  A CLSQL lisp code error occurred: No type conversion to SQL for 
SQLITE3-DATABASE is defined for DB NULL.
>>>>>>>>>>>

Which means I cannot quote according to a database handle. On the other hand 
the following code:

<<<<<<<<<<<<
(require 'clsql)

(let*
  (
   (db 
(clsql:connect '("/home/shlomi/.kde/share/apps/amarok/scripts-data/per-song-volume.sqlite")
                :database-type :sqlite3)
       ))
  (print db)
  (print (clsql:query "SELECT path, volume FROM songs_volumes" :database 
db :field-names nil))
  (print (clsql:sql "Hello \\'Hi Ho \\' Please ' Got"))
  )
>>>>>>>>>>>>

Generates the following error at (clsql:sql):

<<<<<<<<<
debugger invoked on a SIMPLE-ERROR:
  Error during processing of --eval option (LOAD #P"db2.lisp"):

  There is no applicable method for the generic function
    #<STANDARD-GENERIC-FUNCTION CLSQL-SYS:DATABASE-TYPE (2)>
  when called with arguments
    (NIL).
>>>>>>>>>

So it seems I cannot use the cl:sql function with backslashes (which I may 
need).

My system is:

* Mandriva Linux 2007 running on a Pentium 4 machine
* SBCL 0.9.17
* CLSQL-3.7.7
* SQLite 3.3.6-3mdv2007.0

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      shlomif@...
Homepage:        http://www.shlomifish.org/

Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.

Gmane