AutoLisp與SQL資料庫連結存取資料 (Using AutoLISP to read an access SQL database)

本篇解說AutoLisp如何直接與資料庫連線,存取資料。VisualLisp本就有提供方法,首先(vl-load-com),再使用VisualLisp提供的各種函式,便可達成。然而說來簡單,做起來卻是相當繁鎖複雜,令人望之卻步。

之前我會寫一個C++程式,由它負責存取資料庫,並且擔任AutoLisp與資料庫之間資料傳遞的角色。然而這種做法很麻煩也不甚理想,多一個程式的撰寫及維護。

最近找到一個國外網站:http://acad.fleming-group.com/Download/ADOLisp/ADOLisp.html
原來版主已經將這些複雜的函式語法,包成一個Lisp程式,取名為ADOLisp,供人免費下載使用,還有使用說明的pdf檔,此等佛心真令人敬佩感激。

以PDM之SQL資料庫為例,依其說明一步一步實做,請見以下:

(以下在AutoCAD F2文字區鍵入執行,或在VisualLisp編輯器中執行亦可)

;——— 載入程式 Loading program —————————–

 (load “ADOLISP_Library")

 

;——— 建立資料庫連線 Create a database connection —

 (setq conn “E:\\程式雛型\\Output_大霸精機\\pdm.udl")   ; pdm.udl為資料庫連線資訊檔
 (setq db (adolisp_connecttodb conn nil nil))          ; 若udl檔中已含登入的帳密資訊
 (setq db (adolisp_connecttodb conn ‘sa’ ‘pwd’))    ; 若udl檔中未含登入帳密資訊,則必須含有帳密…

udl檔設定內容:  pdm_udl

;——- 讀取資料 Accessing data ———————————
syntax 語法:(adolisp_dosql db sel)
ex1: 查詢資料表erp_material內件號’R110807’的中文品名

 (setq sel “select mtr_id,emtr_cname from erp_material where mtr_id=’R110807′")
 (adolisp_dosql db sel)

systax correct and gets data 語法對且查有資料,返回: ((“mtr_id" “emtr_cname") (“R110807″ “固定座"))
中文品名:固定座

 

ex2:

 (setq sel2 “select mtr_id,emtr_cname from erp_material where mtr_id=’R11080777′")

 (adolisp_dosql db sel2)

systax correct but no data 語法對但查無資料,返回: ((“mtr_id" “emtr_cname")) <–僅一個查詢欄名集合的子串列
件號:R11080777不存在 (part No ‘R1108077’ not exists)

 

ex3:

 (setq sel2 “selectt mtr_id,emtr_cname from erp_material where mtr_id=’R110807′")
 (adolisp_dosql db sel2)

systax error 語法錯則返回nil
錯誤的SQL指令:selectt (SQL ‘selectt’ not correct)

 

ex4:

 (setq sel2 “"select mtr_id,rr from erp_material where mtr_id LIKE ‘R1108%'")
 (adolisp_dosql db sel2)

systax correct but field not exists 語法對旦有欄名不存在,返回nil
欄名:rr不存在 (Field ‘rr’ not exists)

 

ex5:

 (setq sel2 “select mtr_id,emtr_cname from erp_material where mtr_id LIKE ‘R1108%'")
 (adolisp_dosql db sel2)

Syntax correct and gets 10 data 查出10筆以R1108開頭的件號,返回一個含有有11個element(元件)的list(串列)
((“mtr_id" “emtr_cname")
(“R110807″ “固定座")
(“R110810″ “c112234″)
(“R110822″ “迫緊座")
(“R110834″ “迫緊座")
(“R110846″ “迫緊座")
(“R110858″ “主動軸心")
(“R110861″ “迫緊環")
(“R110873″ “c112234″)
(“R110885″ “迫緊座")
(“R110897″ “迫緊座")
)

;———– 更新資料 Updating data ——————————-
將件號R110807的中文名改為cc22

 (setq upd “update erp_material set emtr_cname=’cc22′ where mtr_id=’R110807′")
 (adolisp_dosql db upd)

若語法對返回:T , 若錯則:nil

 

;———- 除錯 Debug ————————————————

 (adolisp_errorprinter)

返回上一次執行的SQL敘述(不管對錯成功或失敗),必須在AutoCAD文字區執行

 

;——  查詢db資料庫所有Table and View  ———————

 (adolisp_gettablesandviews db)

若db存在,返回一個list,內含兩子list,一為所有table名稱另一為所有view名稱
若db不存在,返回nil

 

;—  解除資料庫連線 Releasing database connection ——

 (adolisp_disconnectfromdb db)

若db原為開啟中,執行完返回:T
若db已被取消連線,再次執行則返回: 錯誤: *錯誤* 功能內部已發生一個錯誤引數太多
若db原就不存在,也返回: 錯誤: *錯誤* 功能內部已發生一個錯誤引數太多

取消連線之後,db並非是nil,若欲其為nil,必須自行執行 (setq db nil)

;—————————————————————————–

Tagged: ,

1 thoughts on “AutoLisp與SQL資料庫連結存取資料 (Using AutoLISP to read an access SQL database)

  1. […] 有關ADOLISP_Library函式庫程式的細節請參見較早的這篇 […]

發表留言