I am trying print the values from the DB on the HTML page generated in APPLICATION.rkt
我正在尝试从APPLICATION.rkt中生成的HTML页面上的数据库中打印值
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?
关于我正在做什么wrnog的任何建议?
1 个解决方案
#1
3
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
首先,使用quasiquote(或“backquote”)而不是引用;否则你无法逃避它,@(即,非引号拼接)。换句话说,改变
'(html ___)
to
至
`(html ___)
Then, inside the ,@
escape, your map
s are wrong, and map
doesn't work with in-query
anyway. You probably want something like this instead:
然后,在@ escape中,你的地图是错误的,无论如何地图都不适用于查询。你可能想要这样的东西:
,@(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.)
或类似的东西。 (如果这是您想要的,上面的代码将为表中的每一行创建一个1级标题。)
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:
编辑以回应评论:看起来id是数据库中的数字列。如果您有最新版本的Racket,我建议使用~a,类似于字符串追加,但会自动将非字符串值首先转换为字符串。将createstring的定义更改为:
(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).
在旧版本的Racket中(在~a之前),请使用格式(请参阅文档了解如何)。
#1
3
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
首先,使用quasiquote(或“backquote”)而不是引用;否则你无法逃避它,@(即,非引号拼接)。换句话说,改变
'(html ___)
to
至
`(html ___)
Then, inside the ,@
escape, your map
s are wrong, and map
doesn't work with in-query
anyway. You probably want something like this instead:
然后,在@ escape中,你的地图是错误的,无论如何地图都不适用于查询。你可能想要这样的东西:
,@(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.)
或类似的东西。 (如果这是您想要的,上面的代码将为表中的每一行创建一个1级标题。)
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:
编辑以回应评论:看起来id是数据库中的数字列。如果您有最新版本的Racket,我建议使用~a,类似于字符串追加,但会自动将非字符串值首先转换为字符串。将createstring的定义更改为:
(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).
在旧版本的Racket中(在~a之前),请使用格式(请参阅文档了解如何)。