Foundation of Go Language —— 20. Database Connection (Not translated)
»
    beego中orm支持的三种数据库

    MySQL:http://github.com/go-sql-driver/mysql
    PostgreSQL:http://github.com/lib/pq
    Sqlite3:http://github.com/mattn/go-sqlite3


    orm的使用方法为(代码例子来自https://pkg.go.dev/search?q=orm&m=package,经修改):
    
    import (
        "github.com/astaxie/beego/orm"
        _ "github.com/go-sql-driver/mysql"
    )
    
    // Model Struct
    type User struct {
        Id   int    `orm:"auto"`
        Name string `orm:"size(100)"`
    }
    
    func init() {
        // register model
        orm.RegisterModel(new(User))
    
        // set default database
        orm.RegisterDriver("mysql", orm.DRMySQL)
        maxIdle := 30
        maxConn := 30
        orm.RegisterDataBase("orm_test_db", "mysql", "root:root@/orm_test?charset=utf8", maxIdle, maxConn)
    
        // create table
        orm.RunSyncdb("orm_test_db", false, true)
    }
    
    func useOrmExample() {
        o := orm.NewOrm()
    
        user := User{Name: "Eric.Alexandar"}
    
        // insert
        if id, err := o.Insert(&user); err == nil {
            println(id)
        }
    
        // update
        user.Name = "beego author: astaxie"
        if num, err := o.Update(&user); err == nil {
            println(num)
        }
    
        // read one
        u := User{Id: user.Id}
        if err := o.Read(&u); err == nil {
            println(u.Name)
        }
    
        // delete
        if num, err := o.Delete(&u); err == nil {
            println(num)
        }
    }
                            

    ————www.v-signon.com学习者共勉
                        
«
--Alex.Zhang
--www.v-signon.com Learningers Co-Encouraged
Back
Personal Art: www.up-task.com Unit: Individual
中文 Русский 京ICP备19038994号-2
If the content on this website infringes upon your any rights, please contact me at 1307776259@qq.com for removal