Initial commit of newLISP.
[newlisp.git] / examples / sqlite.cgi
blob299f8552ba434e1932c0c4bc6add92c06bc0fe60
1 #!/usr/bin/newlisp
3 ;; this is a quick and dirty script for talking to your SQLite database
4 ;; on your webserver
5 ;;
6 ;; sqlite.cgi - version 1.0 - initial release
7 ;; sqlite.cgi - version 1.1 - replaced deprecated 'getenv' with 'env'
8 ;;
9 ;; requirements:
11 ;; (1) A 'sqlite.so' or 'sqlite.dll' library for your server platform
12 ;; edit the file 'sqlite.lsp' for the correct location.
14 ;; (2) The files 'cgi.lsp' and 'sqlite.lsp', edit the (load ...) statements
15 ;; below for the correct locations of these files.
19 (print "Content-type: text/html\n\n")
21 (load "cgi-bin/cgi.lsp")
22 (load "cgi-bin/sqlite.lsp")
24 (set 'log-file "sqlite-log.txt") 
26 ;; log access, log is only written, if log-file exists
28 (set 'fle (open log-file "u"))
29 (if fle (begin
30     (seek fle -1)
31     (set 'ip-no (env "REMOTE_ADDR"))
32     (unless (starts-with ip-no "99")
33       (write-line (string 
34            (date (+ (apply date-value (now)) (* 7 60 60)))
35            " - " ip-no " - " (env "HTTP_USER_AGENT")) fle))
36     (close fle)))
39 (if (empty? CGI:params)
40     (set 'mode "input")
41     (set 'mode (CGI:get "mode")))
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 (define (display-table data)
46     (println {<table border=1>})
47     (dolist (line data)
48         (println {<tr>})
49         (dolist (cell line)
50             (println {<td>} cell {</td>}))
51         (println {</tr>}) )
52     (println {<table>}))
56 ;; get input sql ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 (if (= mode "input")
59     (begin
60        (println 
61 [text]
63 <br>
64 <center>
65 <form action="/sqlite.cgi" method="POST">
66 Enter Name of Database:&nbsp;&nbsp;<input type="text" name="database">
67 <br>
68 <p>Enter SQL</p>
69 <textarea name="sql-str" rows=10 cols=80></textarea>
70 <br>
71 <input type="submit" value="Go">
72 <input type="hidden" name="mode" value="sql">
73 </center>
75 [/text]
76 )))
78 (if (= mode "sql")
79     (begin
80         (println {<br>})
81         (set 'database (CGI:get "database"))
82         (set 'sql-str (CGI:get "sql-str"))
83         (SQLite:open database)
84         (set 'data (SQLite:sql sql-str))
85         (if (not data) 
86             (println (SQLite:error) {<br>}) 
87             (if (list? data)
88                 (display-table data)
89                 (println data)))
91         (println {<br>Hit the [back] button on your browser to got back<br>})))
93 (exit)
95 ;; eof ;;