package main
import (
"crypto/md5"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"io"
"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"`
}
var (
engine *xorm.Engine
t = time.Now()
)
func (m *Member) SetTime() {
m.Member_createtime = t.Unix()
m.Member_updatetime = t.Unix()
}
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()
m := new(Member)
m.Member_name = "emma"
m.Member_count = 1
m.Member_ok = "aaa"
m.Member_del = 1
m.Member_email = "[email protected]"
m.Member_ip = "127.0.0.1"
//获取时间戳
m.SetTime()
//获得MD5加密后的密码
h := md5.New()
io.WriteString(h, "123456")
m.Member_pwd = fmt.Sprintf("%x", h.Sum(nil))
//将数据添加到数据库
adr, ade := engine.Insert(m)
if ade != nil {
panic(ade)
}
fmt.Printf("成功在 go_member 表创建 %d 条数据\n", adr)
}