good morning!!!!

Skip to content
Snippets Groups Projects
Commit 8a2e37c3 authored by Carlos Nieto's avatar Carlos Nieto
Browse files

Adding SQLite example.

parent 9bb2fd73
No related branches found
No related tags found
No related merge requests found
all:
cat example.sql | sqlite3 example.db
DROP TABLE IF EXISTS "birthdays";
CREATE TABLE "birthdays" (
"name" varchar(50) DEFAULT NULL,
"born" varchar(12) DEFAULT NULL
);
package main
import (
"fmt"
"time"
"upper.io/db"
_ "upper.io/db/sqlite"
)
var settings = db.Settings{
Database: `example.db`,
}
type Birthday struct {
Name string `field:"name"`
Born time.Time `field:"born"`
}
func main() {
sess, err := db.Open("sqlite", settings)
if err != nil {
fmt.Println("Please create the `example.db` sqlite3 database.")
return
}
defer sess.Close()
birthdayCollection, err := sess.Collection("birthdays")
if err != nil {
fmt.Println(err.Error())
return
}
err = birthdayCollection.Truncate()
if err != nil {
fmt.Println(err.Error())
return
}
birthdayCollection.Append(Birthday{
Name: "Hayao Miyazaki",
Born: time.Date(1941, time.January, 5, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Nobuo Uematsu",
Born: time.Date(1959, time.March, 21, 0, 0, 0, 0, time.UTC),
})
birthdayCollection.Append(Birthday{
Name: "Hironobu Sakaguchi",
Born: time.Date(1962, time.November, 25, 0, 0, 0, 0, time.UTC),
})
var res db.Result
res = birthdayCollection.Find()
var birthdays []Birthday
err = res.All(&birthdays)
if err != nil {
panic(err.Error())
return
}
for _, birthday := range birthdays {
fmt.Printf("%s was born in %s.\n", birthday.Name, birthday.Born.Format("January 2, 2006"))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment