From 69d8276a7f9d833219253ca40cb6c17b7bc712d7 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Fri, 18 Jul 2014 12:15:15 +0100 Subject: Created DB connection and initialisation functions --- blavote.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 blavote.go (limited to 'blavote.go') 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 -- cgit v1.2.3