首页 > 解决方案 > CLSQL:本地启用 sql reader 语法

问题描述

我正在尝试在 CLSQL 中使用微不足道的 SELECT。我尝试了很多排列,如下图:

(clsql:select 'dept_name
              :from 'dept
              :where (= 'dept_id 1))
;; Error 
;; DEPT_ID is not of type NUMBER 
;; though dept_id is of type INTEGER

(clsql:select 'dept_name
              :from 'dept
              :where [= id 'dept_id])
;; Error
;; The variable [= is unbound.
;;    [Condition of type UNBOUND-VARIABLE]

(clsql:select [dept.dept_name]
             :from [dept]
             :where [= [dept.dept_id] 2])
;; Error
;; The variable [DEPT.DEPT_NAME] is unbound.

(clsql:select 'dept
              :where [= [slot-value 'dept 'dept-id] 1])

;; Error
;; The variable [= is unbound.
;;    [Condition of type UNBOUND-VARIABLE]

但是,查询

(clsql:select 'dept_name
   :from 'dept)

返回一个元素(dept_name)列表中的所有部门名称。

我已经声明了一个视图类。

(clsql:def-view-class dept ()
  ((dept-id
    :db-kind :key
    :db-constraints :not-null
    :type integer
    :initarg :dept-id)
   (dept-name
    :accessor dept-name
    :type (string 10)
    :initarg :dept-name))
  (:base-table dept))

对应表dept有两个属性。

我也启用了

(clsql:locally-enable-sql-reader-syntax)

我不知道我错过了什么以及如何让它发挥作用。

标签: common-lispclsql

解决方案


我认为我所有的实验都是错误的。首先,(clsql:locally-enable-sql-reader-syntax)我们需要使用(clsql:enable-sql-reader-syntax). 本地仅适用于文件。请参阅 Reddit 论坛中的此评论


推荐阅读