免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4429 | 回复: 0
打印 上一主题 下一主题

Guile Scheme 之 dbi [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-12-03 18:50 |只看该作者 |倒序浏览
本帖最后由 Lispor 于 2010-12-03 19:01 编辑

Guile-dbi(data base interface) 为 guile 提供了一种简单、通用和易于使用的各种数据库接口, 例如, Postgres, MySQL 或者是 SQLite.

guile-dbi 被分为两部分: DBI(database independent) 和 DBD(database dependent) 插件, DBI 为 guile提供操作接口, 而 DBD 插件为 scheme 连接特定的 SQL server. 现在, 只有 Postgres, MySQL 和 SQLite3 的 DBD 后端.

1.1 历史

2004 年, Maurizio Boriani 写道:

我在寻找一个为 guile scheme 提供通用的 database 库, 虽然找到了一些, 但是他们并不是真正的 'dynamic', 只是几个不同的后端简单的编译而成, 而我寻找的是像 perl, php, tcl等语言的 dbi 系统, 其可以在运行时连接 database driver, 最后没有寻找到, 因此我就写了一个.

2008 年, Linas Vepstas 写道:

我在寻找一个为 guile scheme 提供通用的 database 库, 并且这是我找到的唯一具有文档说明, 能够保证工作的一个. 经过一些补丁后, 满足了我的需求, 现在我就在我的 OpenCog NLP 子系统工程里使用它.

1.2 获取 Guile DBI

你可以从 http://www.gna.org/projects/guile-dbi/ 网页获得 Guile dbi 最新版本.

2. 使用手册

scheme 接口非常简单, 只有 5 个函数: dbi-open, dbi-close, dbi-query, dbi-get_status 和 dbi-get_row.

只要有相应的 dbd backend, guile DBI 可以支持任何数据库, 目前只有 MySQL, Postgres 和 SQLite 后端.

下面是一个 SQLite3 的使用实例:
  1. (use-modules (dbi dbi))

  2. ;; Log into the database.
  3. (define db-obj (dbi-open "sqlite3" "my-example-db"))

  4. ;; Create a table.
  5. (dbi-query db-obj "create table hellotable(id int, name varchar(15))")

  6. ;; Look at the return status of the last SQL command
  7. (display db-obj) (newline)

  8. ;; Populate the table with values.
  9. (dbi-query db-obj
  10.            "insert into hellotable ('id', 'name') values('33', 'ola')")
  11. (dbi-query db-obj
  12.            "insert into hellotable ('id', 'name') values('34', 'dzien dobre')")
  13. (dbi-query db-obj
  14.            "insert into hellotable ('id', 'name') values('44', 'annyong haseyo')")
  15. (display db-obj) (newline)

  16. ;; Display each of the rows of the table, in turn.
  17. (dbi-query db-obj "select * from hellotable")
  18. (display db-obj) (newline)
  19. (write (dbi-get_row db-obj)) (newline)
  20. (write (dbi-get_row db-obj)) (newline)
  21. (write (dbi-get_row db-obj)) (newline)
  22. (write (dbi-get_row db-obj)) (newline)

  23. ;; Close the database.
  24. (dbi-close db-obj)
  25. (display db-obj) (newline)
复制代码
下面是使用 MySQL 数据库的一个实例. 这个实例假定 MySQL 服务已经运行, 并且一个名为 pippo 的 table 已经被创建, 并被存储了一些数据:
  1. #!/usr/bin/guile -e main -s
  2. !#

  3. (use-modules (dbi dbi))

  4. (define ciccio (dbi-open "mysql"
  5.                          "user:pass:pluto:tcp:localhost:3306"))
  6. (define ret #f)
  7. ;; (define ciccio (dbi-open "mysql"
  8.                             "user:pass:pluto:socket:/tmp/mysql.sock"))

  9. (define main
  10.   (lambda (args)
  11.     (display "HERE")(newline)
  12.     (display ciccio)(newline)
  13.     (dbi-query ciccio "select * from pippo")
  14.     (display ciccio)(newline)
  15.     (set! ret (dbi-get_row ciccio))
  16.     (while (not (equal? ret #f))
  17.            (display ret)(newline)
  18.            (set! ret (dbi-get_row ciccio))
  19.            )
  20.     (display ret)(newline)

  21. ))
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP