找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1472|回复: 3

[每日一码] XDRX-API 操作 SQLite 数据库函数介绍

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-28 22:35:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 newer 于 2021-1-28 23:06 编辑

XDRX API 对 SQLITE 数据库支持的函数有:


  1. xdrx-sqlite-open
  2. xdrx-sqlite-close
  3. xdrx-sqlite-closeall
  4. xdrx-sqlite-query
  5. xdrx-sqlite-assocquery
  6. xdrx-sqlite-dml
  7. xdrx-sqlite-scalar
  8. xdrx-sqlite-scalar
  9. xdrx-sqlite-cmpstmt
  10. xdrx-sqlite-stmtbind
  11. xdrx-sqlite-stmtfnl
  12. xdrx-sqlite-ver
  13. xdrx-sqlite-sqlitever
  14. xdrx-sqlite-printf
  15. xdrx-sqlite-getformat
  16. xdrx-sqlite-printfx
  17. xdrx-sqlite-getformatx
  18. xdrx-sqlite-loadext
  19. xdrx-sqlite-lasterr
  20. xdrx-sqlite-dumperr
  21. xdrx-sqlite-keywords

复制代码


  1. (setq db "d:\\xdsoft\\mysqlite.db")


1、xdrx-sqlite-open

打开数据库
(xdrx-sqlite-open db)

2、xdrx-sqlite-close

关闭数据库
(xdrx-sqlite-close db)

3、xdrx-sqlite-closeall

关闭所有打开的数据库
(xdrx-sqlite-closeall)


示例代码:
  1. (defun createdatabse ()
  2.   (chkerr (xdrx-sqlite-open mydb))
  3.   (chkerr (xdrx-sqlite-close mydb))
  4. )

  5. (defun c:enablekeys ( / a db)
  6.   (setq db mydb)
  7.   (chkerr (xdrx-sqlite-open db))
  8.   (xdrx-sqlite-query db "pragma foreign_keys = on;")
  9.   (setq a (xdrx-sqlite-query db "pragma foreign_keys;"))
  10.   (chkerr (xdrx-sqlite-close db))
  11.   a
  12. )



4、xdrx-sqlite-query

数据表查询函数
(xdrx-sqlite-query db query)
例如:
  1. (xdrx-sqlite-query db "pragma foreign_keys = on;")
  2. (xdrx-sqlite-query db "select * from mytable;")


5、xdrx-sqlite-assocquery

将参数替换查询字符串的 %d, %f......后,进行查询

例如:
  1. (xdrx-sqlite-assocquery db "select * from mytable where age=%d;" 101)


6、xdrx-sqlite-dml
数据库编辑函数,支持 insert,update,delete......
  1. (xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);")


上面是主要函数,上面的函数基本上能做很多工作了,数据库主要是打开,关闭,创建,查询,插入,更新,删除这些操作

  1. ;;; create a new table
  2. (defun c:createtable ( / db)
  3.   (setq db mydb)
  4.   (chkerr (xdrx-sqlite-open db))
  5.   (chkerr (xdrx-sqlite-dml db "create table mytable (no int, firstname char(64), lastname char(64), age int);"))
  6.   (chkerr (xdrx-sqlite-close db))
  7. )

  8. ;;; insert data
  9. (defun c:addstuff (/ db)
  10.   (setq db mydb)
  11.   (chkerr (xdrx-sqlite-open db))
  12.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (1, 'donald', 'luck ' , 99);"))
  13.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);"))
  14.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (3, 'minni' , 'mouse' , 29);"))
  15.   (chkerr (xdrx-sqlite-close db))
  16. )



命令行执行完createtable, addstuff后,用数据库编辑软件打开mysqlite.db文件

微信截图_20210128222927.png

  1. ;;; get data, note all queries are returned in the format
  2. ;;; ((col1 name col2 name col3 name)(data1 data2 data3)....)
  3. ;;; exeptions are return as
  4. ;;; (nil . error message)
  5. (defun c:getme ( / db)
  6.   (setq db mydb)
  7.   (chkerr (xdrx-sqlite-open db))
  8.   (setq a (xdrx-sqlite-query db "select * from mytable;"))
  9.   (chkerr (xdrx-sqlite-close db))
  10.   a
  11. )


命令行执行getme后得到:

(("no" "firstname" "lastname" "age") (1 "donald" "luck " 99) (2 "mickey" "mouse" 101) (3 "minni" "mouse" 29))

查询年龄大于30的符合条件的记录:

  1. (defun c:getage>30 ( / db)
  2.   (setq db mydb)
  3.   (chkerr (xdrx-sqlite-open db))
  4.   (setq a (xdrx-sqlite-query db "select * from mytable where age > 30;"))
  5.   (chkerr (xdrx-sqlite-close db))
  6.   a
  7. )



命令: GETAGE>30
(("no" "firstname" "lastname" "age") (1 "donald" "luck " 99) (2 "mickey" "mouse" 101))



下面是辅助函数的用法:

  1. ;(xdrx-sqlite-printf "-%x-" 16777215)
  2. ;(xdrx-sqlite-getformat (entget(car(entsel))))
  3. ;(xdrx-sqlite-printf "(%ld)(%s)(%ld)(%s)(%s)(%d)(%s)(%s)(%s)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)" (entget(car(entsel))))
  4. ;(xdrx-sqlite-getformatx (entget(car(entsel))))
  5. ;(xdrx-sqlite-printfx (strcat "(%d,%ld)(%d,%s)(%d,%ld)(%d,%s)(%d,%s)(%d,%d)(%d,%s)(%d,%s)(%d,%s)(%d,%.15g,%.15"
  6. ;                      "g,%.15g)(%d,%.15g,%.15g,%.15g)(%d,%.15g,%.15g,%.15g)") (entget(car(entsel))))
  7. ;(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "insert into test4 values (%d, %.15g, '%s');" 9  3.14159 "welcome to the xdcad.net")
  8. ;(xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 1  3.14159 "welcome to the xdcad.net")
  9. ;(xdrx-sqlite-getformat (entget(car(entsel))))
  10. ;(xdrx-sqlite-printfx "insert into test4 values (%d, %.15g, '%s');" 1  3.14159 "welcome to the xdcad.net")
  11. ;(xdrx-sqlite-getformatx (entget(car(entsel))))
  12. ;(xdrx-sqlite-printf "%s %s" t nil)
  13. (xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 8  3.14159 "welcome to the xdcad.net")
  14. ;(xdrx-sqlite-open  "d:\\xdsoft\\mysqlite.db")
  15. ;(chkerr(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "create table 5(no int, num1 int, num2 int, num3 float);"))
  16. ;(xdrx-sqlite-close "d:\\xdsoft\\mysqlite.db")



下面是示例应用:

  1. ;xdrx-sqlite-open
  2. ;xdrx-sqlite-close
  3. ;xdrx-sqlite-closeall
  4. ;xdrx-sqlite-query
  5. ;xdrx-sqlite-assocquery
  6. ;xdrx-sqlite-dml
  7. ;xdrx-sqlite-scalar
  8. ;xdrx-sqlite-scalar
  9. ;xdrx-sqlite-cmpstmt
  10. ;xdrx-sqlite-stmtbind
  11. ;xdrx-sqlite-stmtfnl
  12. ;xdrx-sqlite-ver
  13. ;xdrx-sqlite-sqlitever
  14. ;xdrx-sqlite-printf
  15. ;xdrx-sqlite-getformat
  16. ;xdrx-sqlite-printfx
  17. ;xdrx-sqlite-getformatx
  18. ;xdrx-sqlite-loadext
  19. ;xdrx-sqlite-lasterr
  20. ;xdrx-sqlite-dumperr
  21. ;xdrx-sqlite-keywords


  22. (defun chkerr (cmd)
  23.   (if (not cmd)
  24.     (progn
  25.       (princ (xdrx-sqlite-lasterr))
  26.       nil
  27.     )
  28.    t
  29.   )
  30. )


  31. (setq mydb "d:\\xdsoft\\mysqlite.db")

  32. (defun createdatabse ()
  33.   (chkerr (xdrx-sqlite-open mydb))
  34.   (chkerr (xdrx-sqlite-close mydb))
  35. )

  36. (defun c:enablekeys ( / a db)
  37.   (setq db mydb)
  38.   (chkerr (xdrx-sqlite-open db))
  39.   (xdrx-sqlite-query db "pragma foreign_keys = on;")
  40.   (setq a (xdrx-sqlite-query db "pragma foreign_keys;"))
  41.   (chkerr (xdrx-sqlite-close db))
  42.   a
  43. )

  44. ;;; create a new table
  45. (defun c:createtable ( / db)
  46.   (setq db mydb)
  47.   (chkerr (xdrx-sqlite-open db))
  48.   (chkerr (xdrx-sqlite-dml db "create table mytable (no int, firstname char(64), lastname char(64), age int);"))
  49.   (chkerr (xdrx-sqlite-close db))
  50. )

  51. ;;; insert data
  52. (defun c:addstuff (/ db)
  53.   (setq db mydb)
  54.   (chkerr (xdrx-sqlite-open db))
  55.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (1, 'donald', 'luck ' , 99);"))
  56.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);"))
  57.   (chkerr (xdrx-sqlite-dml db "insert into mytable values (3, 'minni' , 'mouse' , 29);"))
  58.   (chkerr (xdrx-sqlite-close db))
  59. )

  60. ;;; create a table json
  61. (defun c:createtablej ( / db)
  62.   (setq db mydb)
  63.   (chkerr (xdrx-sqlite-open db))
  64.   (chkerr (xdrx-sqlite-dml db "create table user (name, phone);"))
  65.   (chkerr (xdrx-sqlite-close db))
  66. )

  67. ;;; insert data json
  68. (defun c:addstuffj (/ db)
  69.   (setq db mydb)
  70.   (chkerr (xdrx-sqlite-open db))
  71.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('jenny', json('{\"cell\":\"+491765\", \"home\":\"704-8675309\"}'))"))
  72.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('oz', json('{\"cell\":\"+491765\", \"home\":\"704-498973\"}'))"))
  73.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('mick', json('{\"cell\":\"+591765\", \"home\": \"705-598973\"}'))"))
  74.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('dude', json('{\"cell\":\"+691765\", \"home\":\"704-698973\"}'))"))
  75.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('sally', json('{\"cell\":\"+591765\", \"home\": \"705-578973\"}'))"))
  76.   (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('vicky', json('{\"cell\":\"+691765\", \"home\":\"704-698973\"}'))"))
  77.   (chkerr (xdrx-sqlite-close db))
  78. )

  79. ;;; get data json
  80. (defun c:getmej ( / db)
  81.   (setq db mydb)
  82.   (chkerr (xdrx-sqlite-open db))
  83.   (setq a (xdrx-sqlite-query db "select user.name from user, json_each(user.phone) where json_each.value like '704-%';"))
  84.   (chkerr (xdrx-sqlite-close db))
  85.   a
  86. )

  87. ;;(("name") ("jenny") ("oz") ("dude") ("vicky"))

  88. ;;; get data, note all queries are returned in the format
  89. ;;; ((col1 name col2 name col3 name)(data1 data2 data3)....)
  90. ;;; exeptions are return as
  91. ;;; (nil . error message)
  92. (defun c:getme ( / db)
  93.   (setq db mydb)
  94.   (chkerr (xdrx-sqlite-open db))
  95.   (setq a (xdrx-sqlite-query db "select * from mytable;"))
  96.   (chkerr (xdrx-sqlite-close db))
  97.   a
  98. )

  99. (defun c:getme2 ( / a db)
  100.   (setq db mydb)
  101.   (chkerr (xdrx-sqlite-open db))
  102.   (setq a(xdrx-sqlite-assocquery db "select * from mytable where age=%d;" 101))
  103.   (chkerr (xdrx-sqlite-close db))
  104.   a
  105. )

  106. (defun c:testme ( / a db)
  107.   (setq db mydb)
  108.   (chkerr (xdrx-sqlite-open db))
  109.   (setq a (xdrx-sqlite-query db "select avg(age) from mytable;"))
  110.   (chkerr (xdrx-sqlite-close db))
  111.   a
  112. )


  113. (defun c:getmea ( / a db)
  114.   (setq db mydb)
  115.   (chkerr (xdrx-sqlite-open db))
  116.   (setq a (xdrx-sqlite-assocquery db "select * from mytable;"))
  117.   (chkerr (xdrx-sqlite-close db))
  118.   a
  119. )

  120. ;;; update a record,
  121. (defun c:changeme ( / db)
  122.   (setq db mydb)
  123.   (chkerr(xdrx-sqlite-open db))
  124.   (chkerr(xdrx-sqlite-dml db "update mytable set lastname='duck' where no=1;"))
  125.   (chkerr(xdrx-sqlite-close db))
  126. )
  127. ;;; run getme to see the changes

  128. ;;; a compiled statement example
  129. (defun c:compilestmt ( / db i)
  130.   (setq i 0)  
  131.   (setq db mydb)

  132.   ; create a new db
  133.   (chkerr(xdrx-sqlite-open db))  

  134.   ; make a new table
  135.   (chkerr(xdrx-sqlite-dml db "create table test2(no int, num1 int, num2 float, name char(64));"))

  136.   ; start a transaction
  137.   (chkerr(xdrx-sqlite-dml db "begin transaction;"))

  138.   ; this is our compiled statement, we will bind data to ?
  139.   (chkerr(xdrx-sqlite-cmpstmt db "insert into test2 values (?, ?, ?, ?);"))

  140.   ; add our values
  141.   (repeat 100
  142.     (chkerr(xdrx-sqlite-stmtbind (setq i (1+ i)) 87 12.012 "helloworld"))
  143.   )  

  144.   ; commit the transaction
  145.   (chkerr(xdrx-sqlite-dml db "commit transaction;"))

  146.   ; we must call this to clear the compiled statement
  147.   ; and finalize the transaction
  148.   (chkerr(xdrx-sqlite-stmtfnl))

  149.   ; close the db
  150.   (chkerr(xdrx-sqlite-close db))
  151. )

  152. ;;; check our data



  153. ;progecad 2.000,

  154. ;;; lets run a performance test, create a table and insert 10,000 records
  155. (defun c:bench1 ( / db end i start)
  156.   (setq i 0)
  157.   (setvar "cmdecho" 0)
  158.   (setq start (getvar "tdusrtimer"))

  159.   (setq db mydb)
  160.   (chkerr(xdrx-sqlite-open db))
  161.   (chkerr(xdrx-sqlite-dml db "create table test1(no int, name char(64));"))
  162.   (chkerr(xdrx-sqlite-dml db "begin transaction;"))
  163.   (repeat 1000
  164.      (chkerr(xdrx-sqlite-dml db
  165.         (strcat "insert into test1 values ("
  166.            (itoa (setq i (1+ i))) ", 'welcome to XDCAD.Net');")))
  167.   )
  168.   (chkerr (xdrx-sqlite-dml  db "commit transaction;"))
  169.   (chkerr (xdrx-sqlite-close  db))

  170.   (setq end (* 86400.0 (- (getvar "tdusrtimer") start)))
  171.   (princ "\n")
  172.   (princ end)
  173.   (princ)
  174. )
  175. ;;; autocad = 1.515 , bricscad = 0.967969 seconds on my old paint

  176. ;1.00000
  177. (defun c:bench2 ( / db end i pi start)
  178.   (setq i 0)
  179.   (setvar "cmdecho" 0)
  180.   (setq start (getvar "tdusrtimer"))

  181.   (setq db mydb)
  182.   (chkerr(xdrx-sqlite-open db))
  183.   (chkerr(xdrx-sqlite-dml db "create table test3(no int, num1 int, num2 int, num3 float);"))
  184.   (chkerr(xdrx-sqlite-cmpstmt db "insert into test3 values (?, ?, ?, ?);"))
  185.   (chkerr(xdrx-sqlite-dml db "begin transaction;"))
  186.   (repeat 10000
  187.      (chkerr(xdrx-sqlite-stmtbind (setq i (1+ i)) 87 12 3.13))
  188.   )
  189.   (chkerr (xdrx-sqlite-dml db "commit transaction;"))
  190.   (xdrx-sqlite-stmtfnl)
  191.   (chkerr (xdrx-sqlite-close db))

  192.   (setq end (* 86400.0 (- (getvar "tdusrtimer") start)))
  193.   (princ "\n")
  194.   (princ end)
  195.   (princ)
  196. )


  197. (defun c:bench3 ( / db end i pi start)
  198.   (setq i 0)
  199.   (setvar "cmdecho" 0)
  200.   (setq start (getvar "tdusrtimer"))

  201.   (setq db mydb)
  202.   (chkerr(xdrx-sqlite-open db))

  203.   (repeat 100
  204.     (setq q(xdrx-sqlite-query mydb "select * from test3;"))
  205.   )
  206.   (chkerr (xdrx-sqlite-close db))

  207.   (setq end (* 86400.0 (- (getvar "tdusrtimer") start)))
  208.   (princ "\n")
  209.   (princ end)
  210.   (princ)
  211. )


  212. ;;; autocad = 1.703 seconds on my old paint
  213. (defun c:bench4 ( / db end i start)
  214.   (setq i 0)
  215.   (setvar "cmdecho" 0)
  216.   (setq start (getvar "tdusrtimer"))

  217.   (setq db mydb)
  218.   (chkerr(xdrx-sqlite-open db))
  219.   (chkerr(xdrx-sqlite-dml db "create table test4(no int, num float ,name char(64));"))
  220.   (chkerr(xdrx-sqlite-dml db "begin transaction;"))
  221.   (repeat 10000
  222.      (chkerr(xdrx-sqlite-dml mydb "insert into test4 values (%d, %.15g, '%s');" (setq i (1+ i))  3.14159 "welcome to the xdcad.net"))
  223.   )
  224.   (chkerr (xdrx-sqlite-dml db "commit transaction;"))
  225.   (chkerr (xdrx-sqlite-close db))

  226.   (setq end (* 86400.0 (- (getvar "tdusrtimer") start)))
  227.   (princ "\n")
  228.   (princ end)
  229.   (princ)
  230. )


  231. ;(xdrx-sqlite-printf "-%x-" 16777215)
  232. ;(xdrx-sqlite-getformat (entget(car(entsel))))
  233. ;(xdrx-sqlite-printf "(%ld)(%s)(%ld)(%s)(%s)(%d)(%s)(%s)(%s)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)" (entget(car(entsel))))
  234. ;(xdrx-sqlite-getformatx (entget(car(entsel))))
  235. ;(xdrx-sqlite-printfx (strcat "(%d,%ld)(%d,%s)(%d,%ld)(%d,%s)(%d,%s)(%d,%d)(%d,%s)(%d,%s)(%d,%s)(%d,%.15g,%.15"
  236. ;                      "g,%.15g)(%d,%.15g,%.15g,%.15g)(%d,%.15g,%.15g,%.15g)") (entget(car(entsel))))
  237. ;(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "insert into test4 values (%d, %.15g, '%s');" 9  3.14159 "welcome to the xdcad.net")
  238. ;(xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 1  3.14159 "welcome to the xdcad.net")
  239. ;(xdrx-sqlite-getformat (entget(car(entsel))))
  240. ;(xdrx-sqlite-printfx "insert into test4 values (%d, %.15g, '%s');" 1  3.14159 "welcome to the xdcad.net")
  241. ;(xdrx-sqlite-getformatx (entget(car(entsel))))
  242. ;(xdrx-sqlite-printf "%s %s" t nil)
  243. (xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 8  3.14159 "welcome to the xdcad.net")
  244. ;(xdrx-sqlite-open  "d:\\xdsoft\\mysqlite.db")
  245. ;(chkerr(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "create table 5(no int, num1 int, num2 int, num3 float);"))
  246. ;(xdrx-sqlite-close "d:\\xdsoft\\mysqlite.db")


数据库管理软件 Navicat Premium 15.02注册版
微信截图_20210128225241.png
请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:数据库管理软件Navicat_Premium15x64(提取码:1234) 
下载次数:5  文件大小:0 Bytes  售价:5D豆 [记录]
下载权限: 不限 以上  [免费赚D豆]




SQLite数据库管理软件 DB Browser

                               
登录/注册后可看大图




论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2021-9-10 09:01:54 | 显示全部楼层
数据库管理软件Navicat_Premium15x64的解压密码是啥
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2023-10-26 23:42:12 | 显示全部楼层
高大上,终于sql了
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 2个

财富等级: 恭喜发财

发表于 2024-1-26 17:29:52 | 显示全部楼层
感谢楼主的分享, 太厉害了, 给力!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-24 18:35 , Processed in 0.426357 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表