gpt4 book ai didi

Racket SQL : how to use variable value as table name in create-table?

转载 作者:行者123 更新时间:2023-12-04 01:16:56 28 4
gpt4 key购买 nike

https://docs.racket-lang.org/sql/是一个很好的用于准备 SQL 语句的 DSL,它适用于 db图书馆。
文档显示了一个示例,例如:

(require sql)

(create-table #:temporary the_numbers
#:columns [n integer #:not-null] [d varchar])
在此代码中 the_numbers不是标识符,它从字面上被视为表的名称。
我想做的是:
(require sql)

(define (my-create-table table-name)
(create-table #:temporary table-name
#:columns [n integer #:not-null] [d varchar]))
这会产生错误,因为它处理 table-name作为实际的表名,它不喜欢其中的连字符(我认为应该可以将它用作表名,但我想我需要做更多的事情才能让 lib 正确引用它...)
我是 Racket 的新手,不知道很多技巧。我尝试使用 'table-name但这不起作用。

最佳答案

您需要的功能在 Dynamic Statement Composition and SQL Injection 中进行了描述。的文档。您可以将表名的 Ident 写为 (Ident:AST ,expr) ,其中 expr产生一个 Ident AST 值。

> (define (my-create-table table-name)
(create-table #:temporary (Ident:AST ,(make-ident-ast table-name))
#:columns [n integer #:not-null] [d varchar]))
> (my-create-table 'the_numbers)
(sql-statement
"CREATE TEMPORARY TABLE the_numbers (n integer NOT NULL, d varchar)")

关于 Racket SQL : how to use variable value as table name in create-table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63208736/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com