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