LISP 简单的数据库 3.7 更新记录
【摘要】
增加update函数以更新记录
;使用全局变量记录数据
(defvar *db* nil)
;数据记录格式
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped...
增加update函数以更新记录
;使用全局变量记录数据
(defvar *db* nil)
;数据记录格式
(defun make-cd (title artist rating ripped)
(list :title title :artist artist :rating rating :ripped ripped)
)
;添加记录
(defun add-record (cd)
( push cd *db*)
)
;查看数据库内容
(defun dump-db()
(dolist (cd *db*)
(format t "~{~a: ~10t~a~%~}~%" cd))
)
;提示用户输入CD信息
(defun prompt-read (prompt)
(format *query-io* "~a: " prompt)
(force-output *query-io*)
(read-line *query-io*)
)
;依次提示用户输入信息
(defun prompt-for-cd()
(make-cd
(prompt-read "Title")
(prompt-read "Artist")
;(prompt-read "Rating")
;(prompt-read "Ripped [y/n]")
;快餐式验证数据有效性
(or (parse-integer (prompt-read "Rating") :junk-allowed t) 0)
(y-or-n-p "Ripped [y/n]: ")
)
)
;批量添加数据,使用回车退出
(defun add-cds()
(loop (add-record (prompt-for-cd))
(if (not (y-or-n-p "Another? [y/n]:"))
(return)
)
)
)
;保存数据
(defun save-db (filename)
(with-open-file(out filename :direction :output :if-exists :supersede)
(with-standard-io-syntax(print *db* out))
)
)
;载入数据
(defun load-db(filename)
(with-open-file(in filename)
(with-standard-io-syntax(setf *db* (read in)))
)
)
;通过艺术家查找记录
(defun select-by-artist(artist)
(remove-if-not #'(lambda (cd) (equal (getf cd :artist) artist)) *db*)
)
;优化查询
(defun select(selector-fn)
(remove-if-not selector-fn *db*)
)
;where条件判断
(defun where (&key title artist rating (ripped nil ripped-p))
#'( lambda (cd)
(and
(if title (equal (getf cd :title) title) t)
(if artist (equal (getf cd :artist) artist) t)
(if rating (equal (getf cd :rating) rating) t)
(if ripped-p (equal (getf cd :ripped) ripped) t)
)
)
)
;更新记录
(defun update(selector-fn &key title artist rating (ripped nil ripped-p))
(setf *db*
(mapcar #'(lambda (row) (when (funcall selector-fn row)
(if title (setf (getf row :title) title))
(if artist (setf (getf row :artist) artist))
(if rating (setf (getf row :rating) rating))
(if ripped-p (setf (getf row :ripped) ripped))
) row) *db*)
)
)
当前*DB*数据
(
(:TITLE "Title3" :ARTIST "fengda" :RATING 11 :RIPPED NIL)
(:TITLE "Title2" :ARTIST "Artist2" :RATING 7 :RIPPED T)
(:TITLE "Title1" :ARTIST "Artist1" :RATING 6 :RIPPED T)
(:TITLE "Kly" :ARTIST "Gy" :RATING 9 :RIPPED T)
(:TITLE "Lily" :ARTIST "Pgy" :RATING 8 :RIPPED T)
(:TITLE "Roses" :ARTIST "Kathy" :RATING 7 :RIPPED T))
执行函数:
CL-USER> (update (where :artist "fengda") :artist "hahaha" :rating 22)
((:TITLE "Title3" :ARTIST "hahaha" :RATING 22 :RIPPED NIL) (:TITLE "Title2" :ARTIST "Artist2" :RATING 7 :RIPPED T) (:TITLE "Title1" :ARTIST "Artist1" :RATING 6 :RIPPED T) (:TITLE "Kly" :ARTIST "Gy" :RATING 9 :RIPPED T) (:TITLE "Lily" :ARTIST "Pgy" :RATING 8 :RIPPED T) (:TITLE "Roses" :ARTIST "Kathy" :RATING 7 :RIPPED T))
文章来源: blog.csdn.net,作者:fengda2870,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fengda2870/article/details/9081129
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)