```go
package main
import (
"fmt"
"github.com/jinzhu/gorm"
)
import _ "github.com/go-sql-driver/mysql"
type UserInfo struct {
Id int `gorm: "primary_key;AUTO_INCREMENT:number"`
UserName string `gorm:"column:name"`
Phone string
}
func main() {
db, _ := gorm.Open("mysql", "root:123456@(127.0.0.1:3306)/gormstudy?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
db.SingularTable(true)
has := db.HasTable(&UserInfo{})
if !has {
db.AutoMigrate(&UserInfo{})
fmt.Println("创建表")
}
// 添加数据
/* p1 := UserInfo{UserName:"abc1",Phone:"1231"}
p2 := UserInfo{UserName:"abc2",Phone:"1232"}
p3 := UserInfo{UserName:"abc3",Phone:"1233"}
p4 := UserInfo{UserName:"abc4",Phone:"1234"}
db.Create(&p1)
db.Create(&p2)
db.Create(&p3)
db.Create(&p4)*/
// 删除Username为abc2的记录
user :=&UserInfo{UserName:"abc2"}
db.Delete(user)
}
```
更多评论
我猜你是想删除username字段为"abc2"的记录,这里应该用db.Where("username = 'abc2' ").Delete(user)
#3