forked from SourceFellows/gobuch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (41 loc) · 978 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Customer struct {
gorm.Model
FirstName string
LastName string
}
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
// LogMode enable
db.LogMode(true)
// Migrate the schema
err = db.AutoMigrate(&Customer{}).Error
if err != nil {
log.Fatalf("error migrating ", err)
}
// Create
customer := Customer{FirstName: "Hans", LastName: "wurst"}
db.Create(&customer)
var foundCustomer Customer
// Plain SQL
db.First(&foundCustomer, "First_Name = ?", "Hans")
// Template
db.Where(&Customer{FirstName: "Hans"}).First(&foundCustomer)
fmt.Println("Gefunden wurde:", foundCustomer.FirstName)
// Update - update product's price to 2000
db.Model(&foundCustomer).Update("LastName", "Meiser")
err = db.Delete(&foundCustomer).Error
if err != nil {
panic(err)
}
}