数据库
准备工作:
1.注册数据库驱动(内置postgresql,mysql,sqlite等)
orm.RegisterDriver(“postgres”, orm.DRPostgres)
2.注册数据库(orm必须注册一个别名为default的数据库,作为默认使用)
orm.RegisterDataBase(“default”, “postgres”, “root:root@9.88.126.88:5432/test”)
3.注册模型
orm.RegisterModel(new(AutoNotes)) // Auto是表结构体
4.自动建表(第一个参数:数据库别名;第二个参数:遇到错误强制执行;第三个参数表示显示所有信息)
orm.RunSyncdb(“default”, false, true) // 开启第二个会默认每次删除表,再新建表。所以一般关闭
基本增删改查:
o = orm.NewOrm()
// Ormer define the orm interface
type Ormer interface {
Read(md interface{}, cols ...string) error
ReadForUpdate(md interface{}, cols ...string) error
ReadOrCreate(md interface{}, col1 string, cols ...string) (bool, int64, error)
Insert(interface{}) (int64, error)
InsertOrUpdate(md interface{}, colConflitAndArgs ...string) (int64, error)
InsertMulti(bulk int, mds interface{}) (int64, error)
Update(md interface{}, cols ...string) (int64, error)
Delete(md interface{}, cols ...string) (int64, error)
LoadRelated(md interface{}, name string, args ...interface{}) (int64, error)
QueryM2M(md interface{}, name string) QueryM2Mer
QueryTable(ptrStructOrTableName interface{}) QuerySeter
Using(name string) error
Begin() error
BeginTx(ctx context.Context, opts *sql.TxOptions) error
Commit() error
Rollback() error
Raw(query string, args ...interface{}) RawSeter
Driver() Driver
DBStats() *sql.DBStats
}
Ormer 常用的方法:
// head表结构
type Head struct {
ID int `orm:"column(id);pk;auto"`
Weight int
Volume int
User *User `orm:"reverse(one)"`
}
1.o.Read()
// 默认通过主键赋值查找-这里没有给主键赋值,所以主键为零值,查找head表时按照主键id=0查找,找不到数据
head1 := &models.Head{Volume: 21}
if err := o.Read(head1); err != nil {
fmt.Println("read head1 failed, err is ", err) // 返回错误:<QuerySeter> no row found
}
fmt.Println("head1 ", *head1) // {0 0 21 <nil>}
// 可以根据多个字段进行查找-指定字段volume,weight作为查找条件
if err := o.Read(head, "Volume", "Weight"); err != nil {
fmt.Println("read head failed, err is ", err)
}
fmt.Println("head ", *head) // {3 11 21 <nil>}
2.o.Insert()
head1 := &models.Head{Volume: 11, Weight: 12}
head2 := &models.Head{Volume: 13, Weight: 14, ID: 99}
// 没有指定主键的值,所以主键是自增+1。n返回的是这条数据插入后的主键的值
n, err := o.Insert(head1)
if err != nil {
fmt.Println("insert head1 failed, err is ", err)
}
fmt.Println("n1=", n) // 原来主键的值到4了,现在就是5
// head修改主键值为99,所以返回99,后面主键开始从99网上累加
n, err = o.Insert(head2)
if err != nil {
fmt.Println("insert head2 failed, err is ", err)
}
fmt.Println("n2=", n)
3.Update()
head1 := &models.Head{Volume: 11}
// update默认按照主键查找对应一条记录,主键值为空,则不会更新
n, err := o.Update(head1)
if err != nil {
fmt.Println("Update head1 failed, err is ", err) // 不会报错,update head set.....where id = 0
}
fmt.Println("update head1 n=", n) //更新不成功,返回0
// update默认全部字段更新,如果不指定字段,没有值的字段会默认更新为零值
head2 := &models.Head{Volume: 88, ID: 3}
n, err = o.Update(head2)
if err != nil {
fmt.Println("Update head2 failed, err is ", err)
}
fmt.Println("update head2 n=", n) // 更新成功1条记录,返回1
// 指定更新的字段
head3 := &models.Head{Weight: 18, ID: 1}
n, err = o.Update(head3, "Weight")
if err != nil {
fmt.Println("Update head3 failed, err is ", err)
}
fmt.Println("update head3 n=", n) // 1
4.Delete()
head1 := &models.Head{ID: parames.ID, Weight: 33}
// 没有指定删除字段,默认按照主键(ID)查找对应的值删除,weight不起作用,传入值不对应也可以
n, err := o.Delete(head1)
if err != nil {
fmt.Println("delete head1 failed, err is ", err)
}
fmt.Println("delete head1 n=", n) // 1 id唯一,删除对应那条
head2 := &models.Head{Weight: 11}
// 指定通过Weight字段查找对应数据,查到3条,删除对应的3条记录
n, err = o.Delete(head2, "Weight")
if err != nil {
fmt.Println("delete head2 failed, err is ", err)
}
fmt.Println("delete head2 n=", n) // 3
head3 := &models.Head{Weight: 12}
// 不指定字段,也没有主键的值,则主键值默认为0.所以删除0条数据,返回0
n, err = o.Delete(head3)
if err != nil {
fmt.Println("delete head3 failed, err is ", err)
}
fmt.Println("delete head3 n=", n) // 0
5.o.InsertMulti()
heads := []models.Head{
{Volume: 13, Weight: 14},
{Volume: 22, Weight: 32},
{Volume: 21, Weight: 5},
{Volume: 8, Weight: 36},
{Volume: 45, Weight: 63},
}
// 语句类似于:insert into head("volume", "weight") values(13,14),(22,32),(21,5),(2,36)
// 因为bulk=4,所以把前四个作为一条SQL执行。最后一条再单独执行一条SQL insert into head("volume", "weight") values(45,63)
// 第一个参数bulk代表的是同时插入的数量, 第二个slice
n, err := o.InsertMulti(4, heads)
if err != nil {
fmt.Println("InsertMulti heads failed, err is ", err)
}
fmt.Println("InsertMulti heads n=", n) // 5 返回成功插入的条数
// todo…
版权声明:本文为danWuDe原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。