You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Common Lisp函数中返回指定值?(CLSQL场景)

解决方案

直接用Common Lisp的unwind-protect结构就能满足需求——它会保证不管主表达式正常执行还是抛出异常,清理代码都会被执行,同时函数会返回主表达式的结果。

修改后的函数代码:

(defun select ()
  (clsql:connect "new.db" :database-type :sqlite3)
  (unwind-protect
       (clsql:print-query
        "select * from contacts"
        :titles '("id" "firstname" "email" "company" "firstline" "status"))
    (clsql:disconnect :database "new.db")))

说明

  • unwind-protect的第一个参数是你想要执行并返回结果的核心代码(这里就是clsql:print-query)
  • 第二个参数是必须执行的清理操作(这里的clsql:disconnect),无论核心代码是正常结束还是因异常中断,这段清理代码都会运行
  • 函数最终会返回clsql:print-query的执行结果,同时确保数据库连接被关闭

内容的提问来源于stack exchange,提问作者Vinn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:55:09