Author Topic: LISP to query MS Access Database return results as list of strings  (Read 1750 times)

0 Members and 1 Guest are viewing this topic.

WPerciful

  • Guest
In Feb fixo helped me to write a SQLCon function. I used the function to query SQL databases. I have tried to write one enabling me to query an access database in the same way. But I am at a total loss.  Any help would be GREAT!

Code: [Select]
(defun SQLControl ( query_statement / adocmd ADOConn adorst  data field fields n sqlQuery
  tmp data1 data2)
;; Fixo
;; http://www.cadtutor.net/forum/showthread.php?84548-LISP-to-query-MS-SQL-Server-Database-return-results-as-list-of-strings
 (defun tostring (received / temp )
 ; Function Syntax: (tostring received)
 ; received:   Variable to be converted to a string
  (cond                   
   ((= (type received) 'STR) received)           
   ((= (type received) 'INT) (itoa received))         
   ((= (type received) 'REAL) (rtos received))         
   ((= (type received) 'LIST)               
       (progn               
        (setq temp "")           
        (foreach listitem received         
         (setq temp
          (strcat temp " "
           (datatostring listitem)
          )
         )
        )               
        (setq temp (substr temp 2))         
        (setq received temp)         
       )               
   )                   
  )                     
 )
 (setq SqlCon "Provider=sqloledb;Data Source=SQL\\DESIGNSQL;Initial Catalog=engineering;user id=DraftingDesign;Password=12345"
  ADOConn(vlax-create-object "ADODB.Connection")
  ADORst (vlax-create-object "ADODB.Recordset")
  data1 (list )
  data2 (list )
 )
 (vlax-invoke-method ADOConn 'Open SqlCon nil "" -1)
 (setq sqlQuery query_statement
  ADOcmd (vlax-create-object "ADODB.command")
 )
 (vlax-put-property ADOcmd "ActiveConnection" ADOConn)
; optional
; (vlax-put-property cmd2 "CommandTimeout" 30)
 (vlax-put-property ADOcmd "CommandText" sqlQuery)
 (vlax-put-property ADOcmd "CommandType" 1)
 (setq ADORst(vl-catch-all-apply 'vlax-invoke-method (list ADOcmd 'Execute nil 2 1)));OK
 (setq fields (vlax-get-property ADORst 'Fields))
 (vl-catch-all-apply 'vlax-invoke-method (list ADORst 'movefirst))
 (while (not (equal :vlax-true (vlax-get-property ADORst 'eof)))
  (setq tmp nil n 0)
  (while (not (vl-catch-all-error-p (vl-catch-all-apply 'vlax-get-property (list fields 'item n))))
   (setq field (vlax-variant-value (vlax-get-property (vlax-get-property fields 'item n) 'value)))
   (setq tmp (cons field tmp))
   (setq n (1+ n))
  )
  (setq data (append data (list (reverse tmp))))
  (vl-catch-all-apply 'vlax-invoke-method (list ADORst 'movenext)))
; use garbage cleaner before of the closing connection:
 (gc) 
 (vlax-invoke-method ADOConn 'Close)
 (vlax-release-object ADORst)
 (vlax-release-object ADOcmd)
 (vlax-release-object ADOConn)
; (alert (vl-princ-to-string data)); must use (vl-string-trim " " strvalue) for string values within the data list
 (foreach line data
  (foreach item line
   (setq data1
    (append data1
     (list
      (cond
       ((= item nil)"")
       ((= (type item) 'SYM)(substr (vl-symbol-name item) 7))
       ((/=(type item) 'STR)(tostring item))
       (T item)
      )
     )
    )
   )
  )
  (setq data2 (append data2 (list data1))
   data1 (list )
  )
 )
 data2
)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: LISP to query MS Access Database return results as list of strings
« Reply #1 on: January 15, 2015, 10:55:17 AM »
What exactly is the problem? Note you need a driver matching ACad's bitlevel, i.e. 64bit instead of (the usual) 32 bit. Also note you need a specific driver related to your MDB file's version. See these two threads:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.