重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

golang修仙记之gorm(一)-创新互联

学习了如何连接数据库、简单的错误处理、关闭数据库、创建表、创建表中的一条记录、读取表的记录、更新表的记录、删除标的记录

创新互联建站作为成都网站建设公司,专注成都网站建设公司、网站设计,有关企业网站建设方案、改版、费用等问题,行业涉及凿毛机等多个领域,已为上千家企业服务,得到了客户的尊重与认可。
package main

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    "time"
)

type User struct {
    gorm.Model
    Name     string
    Age      int
    Birthday time.Time
}

func main() {
    // connect the database
    db, err := gorm.Open("mysql", "user:password@tcp(ip:port)/database?charset=utf8&parseTime=True&loc=Local")

    // panic handle
    if err != nil {
        panic("failed to connect database")
    }

    // close the connection
    defer db.Close()

    // Migrate the schema
    db.AutoMigrate(&User{})

    // create user of table
    db.Create(&User{Name: "Jinzhu", Age: 19, Birthday: time.Now()})

    // read the user of table
    var user User
    db.First(&user, 1)
    db.First(&user, "Name = ?", "Jinzhu")

    //update
    db.Model(&user).Update("Name", "Michael")

    //delete
    db.Delete(&user)
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前题目:golang修仙记之gorm(一)-创新互联
当前URL:http://cqcxhl.com/article/egisp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP