标签:main model 数据库连接 创建 def sqlite com info pre
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)
}
标签:main model 数据库连接 创建 def sqlite com info pre
原文地址:https://www.cnblogs.com/c-x-a/p/12274835.html