create_database.rkt 1.0 KB

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