diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/setup.go | 27 | ||||
-rw-r--r-- | db/setup_test.go | 29 | ||||
-rw-r--r-- | db/testdata/test.db | bin | 0 -> 12288 bytes |
3 files changed, 56 insertions, 0 deletions
diff --git a/db/setup.go b/db/setup.go new file mode 100644 index 0000000..5cd1041 --- /dev/null +++ b/db/setup.go @@ -0,0 +1,27 @@ +package db + +import ( + "database/sql" + "errors" + "log" + "os" + + _ "github.com/mattn/go-sqlite3" +) + +func SetupDB(path string) (*sql.DB, error) { + os.Create(path) + db, err := sql.Open("sqlite3", path) + if err != nil { + return db, errors.New("Cannot open that damn database file") + } + stmt := `drop table if exists datamap; + create table datamap(id integer no null primary key, name text); + ` + _, err = db.Exec(stmt) + if err != nil { + log.Printf("%q: %s\n", err, stmt) + } + + return db, nil +} diff --git a/db/setup_test.go b/db/setup_test.go new file mode 100644 index 0000000..e256f9c --- /dev/null +++ b/db/setup_test.go @@ -0,0 +1,29 @@ +package db + +import "testing" + +func TestOpenSQLiteFile(t *testing.T) { + // db, err := sql.Open("sqlite3", "testdata/test.db") + db, err := SetupDB("./testdata/test.db") + defer db.Close() + if err != nil { + t.Fatal("Expected to be able to set up the database.") + } + stmt := `insert into datamap(id, name) values(1,'cock')` + _, err = db.Exec(stmt) + if err != nil { + t.Errorf("Cannot add record to db") + } + rows, err := db.Query("select name from datamap") + if err != nil { + t.Errorf("Cannot run select statement") + } + defer rows.Close() + for rows.Next() { + var name string + err = rows.Scan(&name) + if err != nil { + t.Errorf("Cannot scan resulting row") + } + } +} diff --git a/db/testdata/test.db b/db/testdata/test.db Binary files differnew file mode 100644 index 0000000..8a7fa2f --- /dev/null +++ b/db/testdata/test.db |