7
|
1 |
package main
|
|
2 |
|
|
3 |
import (
|
|
4 |
"database/sql"
|
|
5 |
"github.com/coopernurse/gorp"
|
|
6 |
_ "github.com/mattn/go-sqlite3"
|
|
7 |
"log"
|
|
8 |
"os"
|
|
9 |
)
|
|
10 |
|
|
11 |
type Sentence struct {
|
|
12 |
Id int64
|
|
13 |
Word string
|
|
14 |
Sentence string
|
|
15 |
}
|
|
16 |
|
|
17 |
func main() {
|
|
18 |
db, err := sql.Open("sqlite3", "lingvigilo.db")
|
|
19 |
if err != nil {
|
|
20 |
log.Fatal("Fatal error: ", err)
|
|
21 |
}
|
|
22 |
defer db.Close()
|
|
23 |
|
|
24 |
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
|
|
25 |
|
|
26 |
dbmap.TraceOn("dbmap", log.New(os.Stderr, "sql", 0))
|
|
27 |
|
|
28 |
dbmap.AddTableWithName(Sentence{}, "sentences").SetKeys(true, "Id")
|
|
29 |
|
|
30 |
dbmap.DropTables()
|
|
31 |
|
|
32 |
err = dbmap.CreateTables()
|
|
33 |
if err != nil {
|
|
34 |
log.Fatal("Fatal error: ", err)
|
|
35 |
}
|
|
36 |
|
|
37 |
mySentence := &Sentence{0, "potato", "Open the potato"}
|
|
38 |
|
|
39 |
err = dbmap.Insert(mySentence)
|
|
40 |
if err != nil {
|
|
41 |
log.Fatal("Fatal error: ", err)
|
|
42 |
}
|
|
43 |
}
|