「Sqlitelsp」の版間の差分
編集の要約なし  | 
				編集の要約なし  | 
				||
| (同じ利用者による、間の4版が非表示) | |||
| 2行目: | 2行目: | ||
AutoLISP からデータベースを操作するのは Microsoft ActiveX Data Objects (ADO) を使う方法がありますが、軽い Sqlite 直接操作したいねっていうことでなんやかんややられて SQLiteLsp として公開されている ARX アドオンによる LISP 関数のメモです。  | AutoLISP からデータベースを操作するのは Microsoft ActiveX Data Objects (ADO) を使う方法がありますが、軽い Sqlite([https://www.sqlite.org]) 直接操作したいねっていうことでなんやかんややられて SQLiteLsp として公開されている nRX(ARX,BRX,GRX...)アドオンによる LISP 関数のメモです。  | ||
* AutoCAD, BricsCAD, ZWCAD   | * AutoCAD, BricsCAD, GstarCAD, ZWCAD で使える。(2023年7月現在)  | ||
入手先とか細かいところはフォーラムのスレッドを読むといいでしょう。  | 入手先とか細かいところはフォーラムのスレッドを読むといいでしょう。  | ||
[http://www.theswamp.org/index.php?topic=28286.0]  | [http://www.theswamp.org/index.php?topic=28286.0 theswamp.org]  | ||
=== SQLiteLsp の関数 ===    | === SQLiteLsp の関数 ===    | ||
* [[DSQL_OPEN]]  | * [[DSQL_OPEN]] : DBを開く  | ||
* [[DSQL_CLOSE]]  | * [[DSQL_CLOSE]] : DBを閉じる  | ||
* [[DSQL_CLOSEALL]]  | * [[DSQL_CLOSEALL]]:DBをすべて閉じる  | ||
* [[DSQL_QUERY]]  | * [[DSQL_QUERY]]: SQLクエリを実行  | ||
* [[DSQL_ASSOCQUERY]]  | * [[DSQL_ASSOCQUERY]]:クエリ文字列の %d、%f... をパラメータに置き換えた後、クエリを実行。  | ||
* [[DSQL_DML]]  | * [[DSQL_DML]] : データベースを編集・挿入、更新、削除...etc.  | ||
* [[DSQL_SCALAR]]  | * [[DSQL_SCALAR]] :   | ||
* [[DSQL_SCALARSTR]]  | * [[DSQL_SCALARSTR]] :  | ||
* [[DSQL_CMPSTMT]]  | * [[DSQL_CMPSTMT]] :  | ||
* [[DSQL_STMTBIND]]  | * [[DSQL_STMTBIND]] :  | ||
* [[DSQL_STMTFNL]]  | * [[DSQL_STMTFNL]] :  | ||
* [[DSQL_VER]]  | * [[DSQL_VER]]: SQLiteLsp のバージョン取得  | ||
* [[DSQL_SQLITEVER]]  | * [[DSQL_SQLITEVER]]:SQLite のバージョン取得  | ||
* [[DSQL_PRINTF]]  | * [[DSQL_PRINTF]]:C の Printf スタイルでSQL  | ||
* [[DSQL_GETFORMAT]]  | * [[DSQL_GETFORMAT]]:  | ||
* [[DSQL_PRINTFX]]  | * [[DSQL_PRINTFX]]:  | ||
* [[DSQL_GETFORMATX]]  | * [[DSQL_GETFORMATX]] :  | ||
* [[DSQL_LOADEXT]]  | * [[DSQL_LOADEXT]] :  | ||
* [[DSQL_LASTERR]]  | * [[DSQL_LASTERR]] :  | ||
* [[DSQL_DUMPERR]]  | * [[DSQL_DUMPERR]] :  | ||
* [[DSQL_KEYWORDS]]  | * [[DSQL_KEYWORDS]] :  | ||
* [[DSQL_FUNCLIST]]  | * [[DSQL_FUNCLIST]] :  | ||
サンプル   | |||
* 引用:https://bricscadapi.wordpress.com/2010/08/25/sqlite-and-lisp-part1/  | |||
<pre class="brush:autolisp;">  | |||
; テーブル作成例  | |||
(DEFUN C:CREATETABLE (/ db)  | |||
  (SETQ DB "C:\\MySQLite.db")  | |||
  (DSQL_OPEN DB)  | |||
  (DSQL_DML DB "CREATE TABLE Products (ID INT unique, Name TEXT, Desc TEXT, Cost FLOAT);")  | |||
  ;  | |||
  (DSQL_DML DB "begin transaction;")  | |||
  (DSQL_DML DB "REPLACE INTO Products VALUES (1,'シンク12x12','12x12 Sink', %g );" 175.50)  | |||
  (DSQL_DML DB "REPLACE INTO Products VALUES (2,'シンク18x24','18x24 Sink', %g );" 185.50)  | |||
  (DSQL_DML DB "REPLACE INTO Products VALUES (3,'シンク24x24','24x24 Sink', %g );" 205.50)  | |||
  (DSQL_DML DB "commit transaction;")  | |||
  ;  | |||
  (DSQL_CLOSE DB)  | |||
)  | |||
; ファイル内のブロックを検索・集計して、結果を XML ファイルに書き込むシンプルな Lisp ルーチン  | |||
(defun listXML (ls / itm sl str tstr)  | |||
  (setq str ""  | |||
        str (strcat str "<?xml version=\"1.0\" encoding=\"utf-8\" ?><products>"))  | |||
  (foreach sl ls  | |||
    (setq str (strcat str "<product ") tstr "")  | |||
    (foreach itm sl  | |||
      (setq tstr (strcat tstr (vl-princ-to-string (car itm)) "=\"" (vl-princ-to-string (cdr itm)) "\" "))  | |||
    )  | |||
    (setq str (strcat str tstr " />"))  | |||
  )  | |||
  (setq str (strcat str "</products>"))  | |||
)  | |||
(defun getTotal (ls / cnt e1 e2 len lsout)  | |||
  (setq lsout '())  | |||
  (setq ls (vl-sort ls  | |||
        (function (lambda (e1 e2)  | |||
              (< (cdr(car e1)) (cdr(car e2)))))))      | |||
   ;CAB @ TheSwamp                             | |||
  (while (setq itm (car ls))  | |||
    (setq len (length ls)  | |||
          ls (vl-remove itm ls)  | |||
          cnt (- len (length ls))  | |||
          lsout (cons (cons (cons "QTY" cnt ) itm) lsout)  | |||
    )  | |||
  )  | |||
)  | |||
(defun c:doit (/ activedocument c db iacadapplication  modelspace s)  | |||
  (vl-load-com)  | |||
  (setq ls '()  | |||
        DB "C:\\MySQLite.db"  | |||
        s (ssget (list (cons 0 "INSERT")))  | |||
        c 0  | |||
  )  | |||
  (if s  | |||
    (progn  | |||
      (DSQL_OPEN DB)  | |||
      (While (< c (sslength s))  | |||
        (setq e (vlax-ename->vla-object (cdr (car (entget (ssname s c)))))  | |||
              res (DSQL_ASSOCQUERY DB "SELECT * FROM Products where Name=('%s');" (vlax-get e 'name))  | |||
        )  | |||
        (if res  | |||
          (setq ls (append(list res) ls))  | |||
        )  | |||
        (setq c (1+ c))  | |||
      )  | |||
      (DSQL_CLOSE DB)  | |||
    )  | |||
    (alert (strcat "製品が見つからなったよ"))  | |||
  )  | |||
  (setq fl (open "c:\\bom.xml" "w"))  | |||
  (princ(listXML(getTotal  ls)) fl)  | |||
  (close fl)  | |||
  (princ)  | |||
)  | |||
;; C の Printfスタイルフォーマット  | |||
;;; DSQL_DMLFサンプル  | |||
(DSQL_DMLF "C:\\MySQLite.db" "insert into Test4 values (%d, %.15g, '%s');" 9  3.14159 "Welcome to the Swamp")  | |||
;;; DSQL_PRINTF サンプル  | |||
(DSQL_PRINTF "Color %s, number1 %d, number2 %05d, hex %x, float %5.2f, unsigned value %u" "red" 123456 89 255 3.14159 250)  | |||
</pre>  | |||
2023年7月11日 (火) 08:19時点における最新版
SQLiteLsp 関数
AutoLISP からデータベースを操作するのは Microsoft ActiveX Data Objects (ADO) を使う方法がありますが、軽い Sqlite([1]) 直接操作したいねっていうことでなんやかんややられて SQLiteLsp として公開されている nRX(ARX,BRX,GRX...)アドオンによる LISP 関数のメモです。
- AutoCAD, BricsCAD, GstarCAD, ZWCAD で使える。(2023年7月現在)
 
入手先とか細かいところはフォーラムのスレッドを読むといいでしょう。 theswamp.org
SQLiteLsp の関数
- DSQL_OPEN : DBを開く
 - DSQL_CLOSE : DBを閉じる
 - DSQL_CLOSEALL:DBをすべて閉じる
 - DSQL_QUERY: SQLクエリを実行
 - DSQL_ASSOCQUERY:クエリ文字列の %d、%f... をパラメータに置き換えた後、クエリを実行。
 - DSQL_DML : データベースを編集・挿入、更新、削除...etc.
 - DSQL_SCALAR :
 - DSQL_SCALARSTR :
 - DSQL_CMPSTMT :
 - DSQL_STMTBIND :
 - DSQL_STMTFNL :
 - DSQL_VER: SQLiteLsp のバージョン取得
 - DSQL_SQLITEVER:SQLite のバージョン取得
 - DSQL_PRINTF:C の Printf スタイルでSQL
 - DSQL_GETFORMAT:
 - DSQL_PRINTFX:
 - DSQL_GETFORMATX :
 - DSQL_LOADEXT :
 - DSQL_LASTERR :
 - DSQL_DUMPERR :
 - DSQL_KEYWORDS :
 - DSQL_FUNCLIST :
 
サンプル 
; テーブル作成例
(DEFUN C:CREATETABLE (/ db)
  (SETQ DB "C:\\MySQLite.db")
  (DSQL_OPEN DB)
  (DSQL_DML DB "CREATE TABLE Products (ID INT unique, Name TEXT, Desc TEXT, Cost FLOAT);")
  ;
  (DSQL_DML DB "begin transaction;")
  (DSQL_DML DB "REPLACE INTO Products VALUES (1,'シンク12x12','12x12 Sink', %g );" 175.50)
  (DSQL_DML DB "REPLACE INTO Products VALUES (2,'シンク18x24','18x24 Sink', %g );" 185.50)
  (DSQL_DML DB "REPLACE INTO Products VALUES (3,'シンク24x24','24x24 Sink', %g );" 205.50)
  (DSQL_DML DB "commit transaction;")
  ;
  (DSQL_CLOSE DB)
)
; ファイル内のブロックを検索・集計して、結果を XML ファイルに書き込むシンプルな Lisp ルーチン
(defun listXML (ls / itm sl str tstr)
  (setq str ""
        str (strcat str "<?xml version=\"1.0\" encoding=\"utf-8\" ?><products>"))
  (foreach sl ls
    (setq str (strcat str "<product ") tstr "")
    (foreach itm sl
      (setq tstr (strcat tstr (vl-princ-to-string (car itm)) "=\"" (vl-princ-to-string (cdr itm)) "\" "))
    )
    (setq str (strcat str tstr " />"))
  )
  (setq str (strcat str "</products>"))
)
 
(defun getTotal (ls / cnt e1 e2 len lsout)
  (setq lsout '())
  (setq ls (vl-sort ls
        (function (lambda (e1 e2)
              (< (cdr(car e1)) (cdr(car e2)))))))    
   ;CAB @ TheSwamp                           
  (while (setq itm (car ls))
    (setq len (length ls)
          ls (vl-remove itm ls)
          cnt (- len (length ls))
          lsout (cons (cons (cons "QTY" cnt ) itm) lsout)
    )
  )
)
 
(defun c:doit (/ activedocument c db iacadapplication  modelspace s)
  (vl-load-com)
  (setq ls '()
        DB "C:\\MySQLite.db"
        s (ssget (list (cons 0 "INSERT")))
        c 0
  )
  (if s
    (progn
      (DSQL_OPEN DB)
      (While (< c (sslength s))
        (setq e (vlax-ename->vla-object (cdr (car (entget (ssname s c)))))
              res (DSQL_ASSOCQUERY DB "SELECT * FROM Products where Name=('%s');" (vlax-get e 'name))
        )
        (if res
          (setq ls (append(list res) ls))
        )
        (setq c (1+ c))
      )
      (DSQL_CLOSE DB)
    )
    (alert (strcat "製品が見つからなったよ"))
  )
  (setq fl (open "c:\\bom.xml" "w"))
  (princ(listXML(getTotal  ls)) fl)
  (close fl)
  (princ)
)
;; C の Printfスタイルフォーマット
;;; DSQL_DMLFサンプル
(DSQL_DMLF "C:\\MySQLite.db" "insert into Test4 values (%d, %.15g, '%s');" 9  3.14159 "Welcome to the Swamp")
;;; DSQL_PRINTF サンプル
(DSQL_PRINTF "Color %s, number1 %d, number2 %05d, hex %x, float %5.2f, unsigned value %u" "red" 123456 89 255 3.14159 250)