package main
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"encoding/json"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func main() {
fmt.Println("in main")
db,err :=gorm.Open("sqlite3","test.db")
if err!=nil{
panic("数据库连接失败")
}
defer db.Close()
//创建
db.AutoMigrate(&Product{})
product:=&Product{Code: "L123",Price:1000}
json_p,err:= json.Marshal(product)
if err != nil {
}
logrus.Infof("json_p %v",string(json_p))
db.First(product,1)
db.First(product,"code = ?","L123")
db.Model(product).Update("Price",3000)
db.Delete(product)
}
|
请发表评论