summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Robinson <joe@mumsnet.com>2014-07-18 12:15:15 +0100
committerJoe Robinson <joe@mumsnet.com>2014-07-18 12:15:15 +0100
commit69d8276a7f9d833219253ca40cb6c17b7bc712d7 (patch)
tree5c525f3dcb1801eb17a531ce79e9f203cf0d0151
parentf2ebfd9265a5089d38854c49599bf000152155b6 (diff)
Created DB connection and initialisation functions
-rw-r--r--blavote.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/blavote.go b/blavote.go
new file mode 100644
index 0000000..808b7af
--- /dev/null
+++ b/blavote.go
@@ -0,0 +1,54 @@
+package main
+
+import "fmt"
+import "github.com/mxk/go-sqlite/sqlite3"
+
+var version string
+
+func main() {
+
+ version = "0.01"
+
+ _, err := connectDb("blavote.db")
+
+ if (err != nil) {
+ fmt.Print("Could not connect to vote database: ")
+ fmt.Println(err)
+ return
+ }
+
+}
+
+func connectDb(name string) (*sqlite3.Conn, error) {
+ db, err := sqlite3.Open(name)
+
+ if (err != nil) {
+ return nil, err
+ }
+
+ //Check the version in the DB, if it doesn't exist we'll create it
+ //Can use this later to update the DB if needed
+ sql := "select * from info where key = 'version'"
+ _, err = db.Query(sql)
+
+ if (err != nil) {
+ fmt.Println("info table does not exist, creating database")
+ initTables(db)
+ return nil, err
+ }
+
+ return db, err
+}
+
+func initTables(db *sqlite3.Conn) {
+
+ db.Exec("create table info(id int, key text, value text)")
+ db.Exec("insert into info (key, value) values('version', '0.01')")
+
+ db.Exec("create table users(id int, name text, admin boolean)")
+ db.Exec("create table polls(id int, text text, user_id int)")
+ db.Exec("create table options(id int, text text, poll_id int)")
+ db.Exec("create table votes(id int, user_id int, poll_id int, option_id int)")
+
+
+} \ No newline at end of file