1
votes

I am trying print the values from the DB on the HTML page generated in APPLICATION.rkt

but this is what i see when i execute the code below

&createstring;&db-conn;SELECT * from students

Here is what I am trying to execute:

#lang racket
(require db)
(require web-server/servlet)
(provide/contract (start (request? . -> . response?)))

(define db-conn
      (virtual-connection
         (lambda () (mysql-connect   #:server "localhost"
                 #:port 8889
                 #:database "SOB"
                 #:user "root"
                 #:password "root"))))




(define (start request)
  (define (createstring id name sid)
    (string-append "id is " id "and name is " name "and sid is "  sid))
  (response/xexpr
   '(html
     (head (title "SOB"))
     (body 
      ,@(map (h1) (map createstring (in-query db-conn "SELECT * from students"))))
     )))


(require web-server/servlet-env)
(serve/servlet start
               #:launch-browser? #f
               #:quit? #f
               #:listen-ip #f
               #:port 8080
               #:extra-files-paths
               (list (build-path "/Users/lalith/Documents/LALITH FILES/MDX/SOB/" "htmlfiles"))
               #:servlet-path
               "/servlets/APPLICATION.rkt") 

Any suggestions as to what I'm doing wrnog?

1

1 Answers

3
votes

There are a couple problems.

First, use quasiquote (or "backquote") instead of quote; otherwise you can't escape from it with ,@ (ie, unquote-splicing). In other words, change

'(html ___)

to

`(html ___)

Then, inside the ,@ escape, your maps are wrong, and map doesn't work with in-query anyway. You probably want something like this instead:

,@(for/list ([(id name sid)
              (in-query db-conn "SELECT id, name, sid from students")])
    `(h1 ,(createstring id name sid)))

Or something like that. (The code above would make a level-1 header for each row in the table, if that's what you want.)


Edited in response to comment: It looks like id is a numeric column in the database. If you have a recent version of Racket, I recommend using ~a, which is like string-append but automatically converts non-string values to strings first. Change the definition of createstring to this:

(define (createstring id name sid)
  (~a "id is " id "and name is " name "and sid is "  sid))

In older versions of Racket (before ~a), use format instead (see the docs for how).