@gorm连接mysql数据库
Navicate破解文件及安装文件下载地址:https://download.csdn.net/download/weixin_48294669/87233328
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type User struct {
Id int
Name string
Age int
Addr string
Pic string
}
func main() {
//用户名:密码@tcp(ip:port)/数据库?charset=utf-8&parseTime=True&loc=Local
db, err := gorm.Open("mysql", "root:abc123456@tcp(localhost:3306)/gorm_project?charset=utf8&parseTime=True&loc=Local")
if err != nil {
fmt.Println("Connect Mysql Error")
panic(err)
}
defer db.Close() //关闭连接
//创建表
// db.CreateTable(&User{})
db.Table("user").CreateTable(&User{}) //指定表名为user
//删除表
db.DropTable("users")
// db.DropTable(&User{})
// 检查表是否存在
isexist := db.HasTable("user")
if isexist {
fmt.Println("user表存在")
} else {
fmt.Println("user表不存在")
}
}