加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Clojure中访问SQLite数据库?

发布时间:2020-12-12 18:59:19 所属栏目:百科 来源:网络整理
导读:(ns db-example (:use [clojure.contrib.sql :only (with-connection with-query-results)] ) (:import (java.sql DriverManager)));; need this to load the sqlite3 driver (as a side effect of evaluating the expression)(Class/forName "org.sqlite.JD
(ns db-example
   (:use [clojure.contrib.sql :only (with-connection with-query-results)] )
   (:import (java.sql DriverManager)))

;; need this to load the sqlite3 driver (as a side effect of evaluating the expression)
(Class/forName "org.sqlite.JDBC")

(def +db-path+  "...")
(def +db-specs+ {:classname  "org.sqlite.JDBC",:subprotocol   "sqlite",:subname       +db-path+})

(def +transactions-query+ "select * from my_table")

(with-connection +db-specs+
  (with-query-results results [+transactions-query+]
    ;; results is an array of column_name -> value maps
    ))

解决方法

你必须从with-query-results宏中实际返回一些东西.因为seq绑定到结果是懒惰的,让我们消耗它:

(with-connection +db-specs+  
   (with-query-results results [+transactions-query+]  
     (doall results)))

这是使用clojure.contrib.sql时的常见模式,不依赖于SQLite JDBC适配器.

顺便说一句,我从来没有必须手动执行(Class / forName driver-class-str),这显然是你的Java习惯.驱动程序被加载到contrib.sql的引擎盖下.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读