create_database.rkt 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #lang racket/base
  2. ;; This program creates a sqlite3 database and then creates an empty table
  3. ;; for predictions.
  4. (require racket/date
  5. db)
  6. ; give us the date in YYYY-MM-DD format
  7. (date-display-format 'iso-8601)
  8. ; create the database and create the table
  9. (define (createdb dbloc)
  10. (write-string (string-append "Creating database " dbloc "\n"))
  11. (define conn (sqlite3-connect #:database dbloc
  12. #:mode 'create))
  13. (query-exec conn "CREATE TABLE predictions (ID INTEGER NOT NULL,
  14. date TEXT NOT NULL,
  15. prediction TEXT DEFAULT NULL,
  16. categories TEXT DEFAULT '',
  17. forecast float DEFAULT NULL,
  18. outcome int DEFAULT NULL,
  19. comments TEXT DEFAULT '')")
  20. (disconnect conn)
  21. (write-string (string-append "Database created at " dbloc "\n")))
  22. ; load configuration file
  23. (require (file "../config.rkt"))
  24. ; make sure we can use the sqlite3 connection
  25. (cond (not (sqlite3-available?))
  26. (error "Sqlite3 library not available."))
  27. ; create the database and add the `predictions` table if it doesn't exist
  28. (if (not (file-exists? dbloc))
  29. (createdb dbloc)
  30. (write-string "Database exists. Exiting."))