type Tom struct{
Id int
Name string
Score int
Created int64}funcInsert(){
tt :=&Tom{
Name:"huang",
Score:78,
Created: time.Now().Unix(),}
db.Table("tom").Create(tt)//insert into tom (name,score,created)vlaue('huang',78,1640921525)}
删
funcDelete(){
db.Table("tom").Delete("","id=?",3)//delete from tom where id = 3}
改
funcUpdate(){
tt :=&Tom{
Name:"huang",
Score:78,
Created: time.Now().Unix(),}
db.Table("tom").Where("id=?",29).Update(&tt)//update tom set name='huang',score=78,created=1640921525 where id=29
db.Table("tom").Where("id=?",29).Update("name","sam")//update tom set name=sam where id=29}
查
funcSelect(){var(
name string
score int)
ns :=make([]string,0) db.Table("tom").Select("name,score").Where("id=?",29).Row().Scan(&name,&score)
fmt.Printf("name:%s,score:%d\n",name,score)//select name,score from tom where id =29
rows,err := db.Table("tom").Select("name").Where("id >?",29).Rows()//select namefrom tom where id >29if err !=nil{
fmt.Println(err)return}for rows.Next(){var n string
err = rows.Scan(&n)if err !=nil{continue}
ns =append(ns,n)}
fmt.Println(ns)}
左连接
funcLeftJoin(){var class int
db.Table("class").Select("class").Joins("left join tom on tom.id=class.uid").Where("tom.name=?","sam").Row().Scan(&class)//select class from class join tom on tom.id=class.uid where tom.name='sam'}