sql连接
This commit is contained in:
parent
7b6ca91f0b
commit
bf741e9082
76
main.go
76
main.go
|
|
@ -1,53 +1,51 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
// v1
|
|
||||||
"github.com/jinzhu/gorm" //必须引入
|
"gorm.io/driver/mysql"
|
||||||
_ "github.com/jinzhu/gorm/dialects/mysql" //必须引入
|
"gorm.io/gorm"
|
||||||
// "gorm.io/driver/mysql"
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Video struct { //数据添加结构体
|
type Video struct {
|
||||||
// gorm.Model
|
|
||||||
Vid int `gorm:"primary_key"`
|
|
||||||
Vimg string
|
Vimg string
|
||||||
Articleid int
|
|
||||||
Video string
|
Video string
|
||||||
|
Articleid int
|
||||||
|
// Vf gorm.DeletedAt 定义软删除
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
|
dsn := "root:123456@tcp(127.0.0.1:3306)/companyinfo?charset=utf8mb4&parseTime=True&loc=Local"
|
||||||
db, err := gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/companyinfo?charset=utf8")
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||||
// db.SingularTable(true) 取消默认加s
|
QueryFields: true,
|
||||||
db.LogMode(true) //开启sqldebug模式
|
})
|
||||||
defer db.Close() //使用完数据库后断开
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
panic("failed to connect database")
|
||||||
} else {
|
|
||||||
fmt.Println(db)
|
|
||||||
}
|
}
|
||||||
|
// result := db.Unscoped().Where("Vimg=?", "40").Delete(&Video{})
|
||||||
// db.Select("Name", "Age", "CreatedAt").Create(&user)
|
// result := db.Unscoped().Where("vimg=20").Find(Video{}) //查找软删除的记录
|
||||||
// db.AutoMigrate(&Video{})
|
// db.AutoMigrate(&Video{}) //迁移
|
||||||
// var video = Video{Vimg: "11", Articleid: 3, Video: "22"} //结构体添加数据
|
// var video = Video{}
|
||||||
video := Video{}
|
// video := Video{Vimg: "42", Video: "12", Articleid: 12}
|
||||||
fmt.Printf("%v", video)
|
// fmt.Println(video)
|
||||||
// result := db.Create(&video) // 通过数据的指针来创建
|
// db.Create(&video)
|
||||||
// result := db.First(&video) //查询第一条
|
// result := db.First(Video{})
|
||||||
result := db.Where("vid = ?", 2).Find(Video{}) //查询
|
result := db.Model(Video{}).Where("vid=?", 28).Updates(map[string]interface{}{"Vimg": "40"})
|
||||||
// result := db.Model(Video{}).Where("vid = ?", 28).Update(map[string]interface{}{"Vimg": "77", "video": "21"}) //更新数据
|
// println(result)
|
||||||
// result := db.Where("vid = ?", 29).Delete(Video{}) //删除数据
|
// db.Where("vid>?", 28).Delete(Video{})
|
||||||
fmt.Println(video)
|
// var videos = Video{}
|
||||||
|
// db.Raw("select Vimg,Video,Articleid from videos where Vid =?", 3).Scan(Video{}) //原生sql查询 Raw Scan
|
||||||
// fmt.Println("$v", result1)
|
// result := db.Where("vid=?", 28).First(&videos)
|
||||||
if result != nil {
|
// result := db.Not("vid=?", 2).Find(&videos) //查询vid不等于2的
|
||||||
fmt.Println("succ")
|
// result := db.Where("vid=?", 28).Or("vimg=?", 12).Find(&videos) //查询 vid=28或者vimg=12
|
||||||
} else {
|
// result := db.Where(map[string]interface{}{"Vimg": "41"}).Find(&videos)
|
||||||
fmt.Println("fail")
|
// result := db.Order("Vimg desc,Video").Find(&videos)
|
||||||
}
|
// result := db.Limit(3).Find(&videos)
|
||||||
// user.ID // 返回插入数据的主键
|
// result := db.Offset(1).Find(&videos)
|
||||||
// result.Error // 返回 error
|
// result := db.Find(&videos)
|
||||||
// result.RowsAffected // 返回插入记录的条数
|
// db.Session(&gorm.Session{QyeryFields: true}).Find(&videos)
|
||||||
|
// result := db.Clauses(hints.New("MAX_EXECUTION_TIME(10000)")).Find(&Video{})
|
||||||
|
fmt.Println(result.RowsAffected)
|
||||||
|
errors.Is(result.Error, gorm.ErrRecordNotFound)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue