版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
使用values查询列表
// 通过values获取DB中的列表数据
func GetAddressByUserId(id string) (dataList []orm.Params, err error) {
// 数据封装的列表
var list []orm.Params
o := orm.NewOrm()
nums, err := o.Raw("SELECT create_time,is_delete,update_time,address,door FROM address WHERE uid = ?", id).Values(&list)
if nums == 0 {
}
return list, err
}
返回数据样式
{
"code": 200,
"msg": "",
"data": [
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "2栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "2栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "3栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "4栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "5栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "6栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "7栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
},
{
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "8栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
}
]
}
使用queryRows查询列表
注:此方法必须有对应的struct进行映射
// 通过queryRow获取DB中的数据
type AddressInfo struct {
CreateTime string `json:"create_time"`
IsDelete int `json:"is_delete"`
UpdateTime string `json:"update_time"`
Address string `json:"address"`
Door string `json:"door"`
}
func GetAddressByUserId2(id string) (dataList []AddressInfo, err error) {
// 数据封装的列表
var list []AddressInfo
o := orm.NewOrm()
nums, err := o.Raw("SELECT create_time,is_delete,update_time,address,door FROM address WHERE uid = ?", id).QueryRows(&list)
if nums == 0 {
}
return list, err
}
返回数据
{
"code": 200,
"msg": "",
"data": [
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "2栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "2栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "3栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "4栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "5栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "6栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "7栋223房间"
},
{
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "8栋223房间"
}
]
}
通过values查询单条数据
其实就是通过values获取一个列表 取巧 从列表中拿第一条数据
// 通过values获取DB中的单条数据
func GetAddressByAddressId(id string) (data orm.Params, err error) {
// 数据封装的列表
var list []orm.Params
o := orm.NewOrm()
nums, err := o.Raw("SELECT create_time,is_delete,update_time,address,door FROM address WHERE uid = ?", id).Values(&list)
if nums > 0 {
return list[0], err
}
return nil, err
}
返回结果
{
"code": 200,
"msg": "",
"data": {
"address": "哈哈",
"create_time": "2018-12-28 19:53:03",
"door": "2栋223房间",
"is_delete": "0",
"update_time": "2019-07-18 16:07:11"
}
}
使用queryRow获取单条数据
// 通过queryRow获取DB中的数据
type AddressInfo struct {
CreateTime string `json:"create_time"`
IsDelete int `json:"is_delete"`
UpdateTime string `json:"update_time"`
Address string `json:"address"`
Door string `json:"door"`
}
func GetAddressByAddressId2(id string) (data AddressInfo, err error) {
var address AddressInfo
o := orm.NewOrm()
err = o.Raw("SELECT create_time,is_delete,update_time,address,door FROM address WHERE uid = ?", id).QueryRow(&address)
return address, err
}
返回数据
{
"code": 200,
"msg": "",
"data": {
"create_time": "2018-12-28 19:53:03",
"is_delete": 0,
"update_time": "2019-07-18 16:07:11",
"address": "哈哈",
"door": "2栋223房间"
}
}
总结
整体来讲 使用queryRow和queryRows的时候 必须创建对应的struct
使用values查询单条或者列表数据 都可以直接映射为map key为数据库字段名称小写
如果能帮当您 万分荣幸~