create_database.rkt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. (require 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. ; add a dummy entry
  21. (query-exec conn "INSERT INTO predictions (ID, date, prediction, outcome) values (0, ?, \"\",0)"
  22. (date->string (seconds->date (current-seconds))))
  23. (disconnect conn)
  24. (write-string (string-append "Database created at " dbloc "\n")))
  25. ; load configuration file
  26. (require (file "../config.rkt"))
  27. ; make sure we can use the sqlite3 connection
  28. (cond (not (sqlite3-available?))
  29. (error "Sqlite3 library not available."))
  30. ; create the database and add the `predictions` table if it doesn't exist
  31. (if (not (file-exists? dbloc))
  32. (createdb dbloc)
  33. (write-string "Database exists. Exiting."))