xorm添加单条数据
Posted kevin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xorm添加单条数据相关的知识,希望对你有一定的参考价值。
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)
}
以上是关于xorm添加单条数据的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot Elasticsearch7.6.2实现创建索引删除索引判断索引是否存在获取/添加/删除/更新索引别名单条/批量插入单条/批量更新删除数据递归统计ES聚合的数据