xorm创建表
Posted kevin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xorm创建表相关的知识,希望对你有一定的参考价值。
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"time"
)
type Member struct {
Memberid int64 `xorm:"int(10) pk not null autoincr ‘member_id‘"`
Member_name string `xorm:"char(50) not null"`
Member_pwd string `xorm:"char(32) not null"`
Member_count int64 `xorm:"smallint(6) not null default 0 "`
Member_ok string `xorm:"varchar(50) not null"`
Member_del int64 `xorm:"bigint(1) not null default 0 "`
Member_email string `xorm:"varchar(40) not null"`
Member_logintime time.Time `xorm:"updated"`
Member_createtime int64 `xorm:"int(11) not null default 0"`
Member_updatetime int64 `xorm:"int(11) not null default 0"`
Member_ip string `xorm:"varchar(40) not null"`
Member_version int64 `xorm:"version"`
}
type Delinfo struct {
Del_id int64 `xorm:"int(10) pk not null autoincr ‘del_id‘"`
Del_info string `xorm:"char(50) not null"`
}
var engine *xorm.Engine
func main() {
//数据库连接参数
params := fmt.Sprintf("%s:%[email protected](%s)/%s?charset=utf8&parseTime=true", "root", "root", "localhost:3306", "go")
var err error
//连接数据库
engine, err = xorm.NewEngine("mysql", params)
if err != nil {
panic(err)
}
//添加统一前缀
tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "go_")
engine.SetTableMapper(tbMapper)
defer engine.Close()
//创建表
err = engine.Sync2(new(Member), new(Delinfo))
if err != nil {
panic(err)
}
}
以上是关于xorm创建表的主要内容,如果未能解决你的问题,请参考以下文章