example.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ;;; Code is mostly adapted from
  2. ;;; https://notabug.org/guile-sqlite3/guile-sqlite3/src/9405dda61347ff26c57e3be7531dc9dc5a19cf40/tests/basic.scm
  3. (import
  4. (sqlite3)
  5. (ice-9 format))
  6. (define (sqlite-exec* db sql key value)
  7. (let ((stmt (sqlite-prepare db sql)))
  8. (sqlite-bind stmt key value)
  9. (sqlite-map display stmt)
  10. (sqlite-finalize stmt)
  11. #t))
  12. (define db-name "database.db")
  13. (when (file-exists? db-name)
  14. (format #t "Removing leftover database ~a~%" db-name)
  15. (delete-file db-name))
  16. (let ([db (sqlite-open db-name (logior SQLITE_OPEN_CREATE SQLITE_OPEN_READWRITE))])
  17. (sqlite-db? db)
  18. (sqlite-exec db "CREATE TABLE project (
  19. REFERENCE INTEGER PRIMARY KEY,
  20. name TEXT,
  21. website TEXT
  22. )")
  23. ;; Do not forget to close the database.
  24. (sqlite-close db))
  25. (let ([db (sqlite-open db-name (logior SQLITE_OPEN_CREATE SQLITE_OPEN_READWRITE))])
  26. (sqlite-db? db)
  27. (sqlite-exec db
  28. "INSERT INTO project VALUES (1, 'Guile', '');
  29. INSERT INTO project VALUES (2, 'Guix', 'gnu.org');")
  30. ;; Do not forget to close the database.
  31. (sqlite-close db))
  32. ;;; The procedure sqlite-trace is not available in version 0.1.2 -- It
  33. ;;; might have been added in version 0.1.3.
  34. ;; (let ([db (sqlite-open db-name (logior SQLITE_OPEN_CREATE SQLITE_OPEN_READWRITE))])
  35. ;; (sqlite-trace db
  36. ;; SQLITE_TRACE_STMT
  37. ;; (lambda (trace p x)
  38. ;; (test-assert "trace"
  39. ;; (string=? (pointer->string
  40. ;; (sqlite-expanded-sql p))
  41. ;; "select * from project where 'bla' = 'bla'"))))
  42. ;; (sqlite-exec* db "select * from project where 'bla' = :foo" 'foo "bla")
  43. ;; (sqlite-close db))