What Is SQLite?SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day.
More Information...Allowing AUTOLISP to support SQLITE will greatly improve the functions of LISP. Many operations can be enriched and efficiently performed with the help of SQL database query statements.
Such as LISP LIST keyword query, sorting, Table insert, delete, update...
Natural handling of LISP LIST table structures
XDRX API wraps SQLite.
The functions supported by XDRX API for SQLITE database are:xdrx-sqlite-open
xdrx-sqlite-close
xdrx-sqlite-closeall
xdrx-sqlite-query
xdrx-sqlite-assocquery
xdrx-sqlite-dml
xdrx-sqlite-scalar
xdrx-sqlite-scalar
xdrx-sqlite-cmpstmt
xdrx-sqlite-stmtbind
xdrx-sqlite-stmtfnl
xdrx-sqlite-ver
xdrx-sqlite-sqlitever
xdrx-sqlite-printf
xdrx-sqlite-getformat
xdrx-sqlite-printfx
xdrx-sqlite-getformatx
xdrx-sqlite-loadext
xdrx-sqlite-lasterr
xdrx-sqlite-dumperr
xdrx-sqlite-keywords
(setq db "d:\\xdsoft\\mysqlite.db")
1.xdrx-sqlite-open
Open database
(xdrx-sqlite-opendb)2.xdrx-sqlite-close
Close database
(xdrx-sqlite-close db)3.xdrx-sqlite-closeall
Close all open databases
(xdrx-sqlite-closeall)Sample code:
(chkerr
(xdrx
-sqlite
-open mydb
)) (chkerr
(xdrx
-sqlite
-close mydb
)) )
(defun c:enablekeys
( / a db
) (chkerr
(xdrx
-sqlite
-open db
)) (xdrx-sqlite-query db "pragma foreign_keys = on;")
(setq a
(xdrx
-sqlite
-query db
"pragma foreign_keys;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
4. xdrx-sqlite-query
Data table query function
(xdrx-sqlite-query db query)For example:
(xdrx-sqlite-query db "pragma foreign_keys = on;")
(xdrx-sqlite-query db "select * from mytable;")
5.xdrx-sqlite-assocquery
After replacing %d, %f... of the query string with parameters, perform the query
For example:
(xdrx-sqlite-assocquery db "select * from mytable where age=%d;" 101)
6.xdrx-sqlite-dml
Database editing functions, supporting insert, update, delete...
(xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);")
The above is the main function. The above function can basically do a lot of work. The database mainly performs operations such as opening, closing, creating, querying, inserting, updating, and deleting.
;;; create a new table
(defun c:createtable
( / db
) (chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "create table mytable (no int, firstname char(64), lastname char(64), age int);"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; insert data
(chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "insert into mytable values (1, 'donald', 'luck ' , 99);"))
(chkerr (xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);"))
(chkerr (xdrx-sqlite-dml db "insert into mytable values (3, 'minni' , 'mouse' , 29);"))
(chkerr
(xdrx
-sqlite
-close db
)) )
After executing createtable and addstuff on the above command line, use database editing software to open the mysqlite.db file.

;;; get data, note all queries are returned in the format
;;; ((col1 name col2 name col3 name)(data1 data2 data3)....)
;;; exeptions are return as
;;; (nil . error message)
(chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-query db
"select * from mytable;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
After executing getme on the command line, we get:
(("no" "firstname" "lastname" "age") (1 "donald" "luck " 99) (2 "mickey" "mouse" 101) (3 "minni" "mouse" 29))
Query eligible records with age greater than 30:
(defun c:getage
>30 ( / db
) (chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-query db
"select * from mytable where age > 30;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
Command: GETAGE>30
(("no" "firstname" "lastname" "age") (1 "donald" "luck " 99) (2 "mickey" "mouse" 101))The following is the usage of auxiliary functions:
;(xdrx-sqlite-printf "-%x-" 16777215)
;(xdrx-sqlite-getformat (entget(car(entsel))))
;(xdrx-sqlite-printf "(%ld)(%s)(%ld)(%s)(%s)(%d)(%s)(%s)(%s)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)(%.15g,%.15g,%.15g)" (entget(car(entsel))))
;(xdrx-sqlite-getformatx (entget(car(entsel))))
;(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"
; "g,%.15g)(%d,%.15g,%.15g,%.15g)(%d,%.15g,%.15g,%.15g)") (entget(car(entsel))))
;(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "insert into test4 values (%d, %.15g, '%s');" 9 3.14159 "welcome to the xdcad.net")
;(xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 1 3.14159 "welcome to the xdcad.net")
;(xdrx-sqlite-getformat (entget(car(entsel))))
;(xdrx-sqlite-printfx "insert into test4 values (%d, %.15g, '%s');" 1 3.14159 "welcome to the xdcad.net")
;(xdrx-sqlite-getformatx (entget(car(entsel))))
;(xdrx-sqlite-printf "%s %s" t nil)
(xdrx-sqlite-printf "insert into test4 values (%d, %.15g, '%s');" 8 3.14159 "welcome to the xdcad.net")
;(xdrx-sqlite-open "d:\\xdsoft\\mysqlite.db")
;(chkerr(xdrx-sqlite-dml "d:\\xdsoft\\mysqlite.db" "create table 5(no int, num1 int, num2 int, num3 float);"))
;(xdrx-sqlite-close "d:\\xdsoft\\mysqlite.db")
Here is a sample application:
;xdrx-sqlite-open
;xdrx-sqlite-close
;xdrx-sqlite-closeall
;xdrx-sqlite-query
;xdrx-sqlite-assocquery
;xdrx-sqlite-dml
;xdrx-sqlite-scalar
;xdrx-sqlite-scalar
;xdrx-sqlite-cmpstmt
;xdrx-sqlite-stmtbind
;xdrx-sqlite-stmtfnl
;xdrx-sqlite-ver
;xdrx-sqlite-sqlitever
;xdrx-sqlite-printf
;xdrx-sqlite-getformat
;xdrx-sqlite-printfx
;xdrx-sqlite-getformatx
;xdrx-sqlite-loadext
;xdrx-sqlite-lasterr
;xdrx-sqlite-dumperr
;xdrx-sqlite-keywords
(princ (xdrx
-sqlite
-lasterr
)) nil
)
t
)
)
(setq mydb
"d:\\xdsoft\\mysqlite.db")
(chkerr
(xdrx
-sqlite
-open mydb
)) (chkerr
(xdrx
-sqlite
-close mydb
)) )
(defun c:enablekeys
( / a db
) (chkerr
(xdrx
-sqlite
-open db
)) (xdrx-sqlite-query db "pragma foreign_keys = on;")
(setq a
(xdrx
-sqlite
-query db
"pragma foreign_keys;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
;;; create a new table
(defun c:createtable
( / db
) (chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "create table mytable (no int, firstname char(64), lastname char(64), age int);"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; insert data
(chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "insert into mytable values (1, 'donald', 'luck ' , 99);"))
(chkerr (xdrx-sqlite-dml db "insert into mytable values (2, 'mickey', 'mouse' , 101);"))
(chkerr (xdrx-sqlite-dml db "insert into mytable values (3, 'minni' , 'mouse' , 29);"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; create a table json
(defun c:createtablej
( / db
) (chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "create table user (name, phone);"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; insert data json
(defun c:addstuffj
(/ db
) (chkerr
(xdrx
-sqlite
-open db
)) (chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('jenny', json('{\"cell\":\"+491765\", \"home\":\"704-8675309\"}'))"))
(chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('oz', json('{\"cell\":\"+491765\", \"home\":\"704-498973\"}'))"))
(chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('mick', json('{\"cell\":\"+591765\", \"home\": \"705-598973\"}'))"))
(chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('dude', json('{\"cell\":\"+691765\", \"home\":\"704-698973\"}'))"))
(chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('sally', json('{\"cell\":\"+591765\", \"home\": \"705-578973\"}'))"))
(chkerr (xdrx-sqlite-dml db "insert into user (name, phone) values('vicky', json('{\"cell\":\"+691765\", \"home\":\"704-698973\"}'))"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; get data json
(chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-query db
"select user.name from user, json_each(user.phone) where json_each.value like '704-%';")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
;;(("name") ("jenny") ("oz") ("dude") ("vicky"))
;;; get data, note all queries are returned in the format
;;; ((col1 name col2 name col3 name)(data1 data2 data3)....)
;;; exeptions are return as
;;; (nil . error message)
(chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-query db
"select * from mytable;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
(defun c:getme2
( / a db
) (chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-assocquery db
"select * from mytable where age=%d;" 101)) (chkerr
(xdrx
-sqlite
-close db
)) a
)
(defun c:testme
( / a db
) (chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-query db
"select avg(age) from mytable;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
(defun c:getmea
( / a db
) (chkerr
(xdrx
-sqlite
-open db
)) (setq a
(xdrx
-sqlite
-assocquery db
"select * from mytable;")) (chkerr
(xdrx
-sqlite
-close db
)) a
)
;;; update a record,
(defun c:changeme
( / db
) (chkerr
(xdrx
-sqlite
-open db
)) (chkerr(xdrx-sqlite-dml db "update mytable set lastname='duck' where no=1;"))
(chkerr
(xdrx
-sqlite
-close db
)) )
;;; run getme to see the changes
;;; a compiled statement example
(defun c:compilestmt
( / db i
)
; create a new db
(chkerr
(xdrx
-sqlite
-open db
))
; make a new table
(chkerr(xdrx-sqlite-dml db "create table test2(no int, num1 int, num2 float, name char(64));"))
; start a transaction
(chkerr(xdrx-sqlite-dml db "begin transaction;"))
; this is our compiled statement, we will bind data to ?
(chkerr(xdrx-sqlite-cmpstmt db "insert into test2 values (?, ?, ?, ?);"))
; add our values
(chkerr
(xdrx
-sqlite
-stmtbind
(setq i
(1+ i
)) 87 12.012 "helloworld")) )
; commit the transaction
(chkerr(xdrx-sqlite-dml db "commit transaction;"))
; we must call this to clear the compiled statement
; and finalize the transaction
(chkerr(xdrx-sqlite-stmtfnl))
;
Database management software can go to https://www.navicat.com/en/products/navicat-for-sqliteSQLite database management software DB Browser:https://github.com/sqlitebrowser/sqlitebrowser/DB.Browser.for.SQLite-3.12.1-win32-v2.msi - Standard (MSI) installer for Win32 and WinXP
DB.Browser.for.SQLite-3.12.1-win32.zip - .zip (no installer) for Win32 and WinXP
DB.Browser.for.SQLite-3.12.1-win64-v2.msi - Standard (MSI) installer for Win64
DB.Browser.for.SQLite-3.12.1-win64.zip- .zip (no installer) for Win64
DB.Browser.for.SQLite-3.12.1-v2.dmg - For macOS
=============
The above LISP code uses the XDRX-API, which can be downloaded from
https://github.com/xdcad/XDrx-API and is updated at any time.
The XDRX API encapsulates AcDb, AcEd, AcGe, AcBr... C++ library, using C++ methods to develop LISP programs.Thousands of Lisp functions are available.
Modify message