我有一个名为“考勤-事件”的列表,由结构化类型的考勤事件组成。结构被定义为
(define-struct attendance-event (date flag))“日期”是事件的日期,“标志”是代表事件类型的2个字符值。
在我的数据库中,我选择"select date,attendance_table代码“,并希望将这两列添加到每一行的考勤事件列表中。
那么,如何才能做到这一点?
发布于 2015-06-24 14:18:54
这将执行查询并返回attendance-event结构的列表:
(for/list ([(date code)
(in-query conn "select date, code from attendance_table")])
(make-attendance-event date code))发布于 2015-06-23 23:42:09
在将数据库行转换成哈希表之后,我发现使用它们要容易得多。以下函数将rows-result函数返回的query类型转换为不可变散列:
(require (planet jphelps/loop)) ;; Use the loop macro from Common Lisp.
(define (rows-result->hash results)
(let ((header (rows-result-headers results)))
(loop for row in (rows-result-rows results)
collect (loop for ((_ . name) (_ . decltype)) in header
for column across row
with-collection-type hash/immutable
collect (cons name column)))))然后,您可以像这样包装query函数:
(define (simple-query query-text conn . params)
(rows-result->hash (apply query (list* conn query-text params))))最后,要创建您的结构:
(loop for row in (simple-query "select date,code from attendance_table;")
collect (make-attendance-event (hash-ref row 'date) (hash-ref row 'code)))https://stackoverflow.com/questions/31012789
复制相似问题