Skip to content

Instantly share code, notes, and snippets.

@seehuhn
Created August 9, 2013 22:11
Show Gist options
  • Save seehuhn/6197736 to your computer and use it in GitHub Desktop.
Save seehuhn/6197736 to your computer and use it in GitHub Desktop.
A test program to illustrate time.Time support in the github.com/seehuhn/sqlite3 module.
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/seehuhn/sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", "ex1.db")
if err != nil {
panic(err)
}
defer db.Close()
_, err = db.Exec("DROP TABLE IF EXISTS test")
if err != nil {
panic(err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS test (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time DATETIME
)`)
if err != nil {
panic(err)
}
t1, err := time.Parse("2006-01-02 15:04:05 -0700",
"2013-08-09 21:30:43 +0800")
if err != nil {
panic(err)
}
_, err = db.Exec(`INSERT INTO test(time) VALUES (?)`, t1)
if err != nil {
panic(err)
}
var t2 time.Time
err = db.QueryRow("SELECT time FROM test").Scan(&t2)
if err != nil {
panic(err)
}
fmt.Println(t1, t2)
fmt.Println(t1.UnixNano() == t2.UnixNano())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment