gpt4 book ai didi

go - 使用 sqlx 选择并获取用法

转载 作者:数据小太阳 更新时间:2023-10-29 03:10:54 25 4
gpt4 key购买 nike

我正在使用 go 1.10.3,我正在尝试使用 sqlx 包获取一行并将其输入到带有 Get() 的结构中,或者获取多行并输入它们使用 Select() slice 。

让我们从将一行放入结构开始。

我创建了以下结构:

type PsqlProduct struct {
Id int64 `db:"product_id"`
Name string `db:"product_name"`
Desc sql.NullString `db:"product_desc"`
YearManufacture sql.NullInt64 `db:"year_manufacture"`
Quantity sql.NullInt64 `db:"quantity"`
}

对于查询:

QUERY_SELECT_PRODUCT = `select wd.product.id as product_id,
trans_p_name.text as product_name,
trans_p_desc.text as product_desc,
wd.product.year_manufacture, wd.product.quantity
from wd.product
join wd.text_translation as trans_p_name
on trans_p_name.text_id = wd.product.product_name_trans_id and trans_p_name.lang_id=1
left join wd.text_translation as trans_p_desc
on trans_p_desc.text_id = wd.product.product_desc_trans_id and trans_p_desc.lang_id=1
where wd.product.id = $1
`

我创建了以下函数来通过 id 获取产品:

func PsqlGetProductById(productId int) *Product {
product := new(PsqlProduct)
err := Psqldb.Get(&product, QUERY_SELECT_PRODUCT,productId)
if err != nil {
log.Fatalf("error: %v",err)
return nil
} else {

newp := Product{
ID: uint(product.Id),
Name: product.Name,
}
if product.Quantity.Valid {
newp.Quantity = uint16(product.Quantity.Int64)
}
if product.YearManufacture.Valid {
newp.YearManufacture = uint16(product.YearManufacture.Int64)
}
if product.Desc.Valid {
newp.Desc = product.Desc.String
}
return &newp
}
}

我得到了错误

error: scannable dest type ptr with >1 columns (5) in result

好像 Get() 函数只适用于一列..但文档明确指出它不是!

如果我将 Get() 函数调用更改为 Psqldb.QueryRowx(QUERY_SELECT_PRODUCT, productId).StructScan(product)

然后它确实起作用了..但仍然..试图找出为什么 Get() 不起作用。

下一步.. Select()

这就是结构

type PsqlCategory struct {
Id int64 `db:"category_id"`
Name string `db:"category_name"`
ParentCategoryId sql.NullInt64 `db:"parent_category_id"`
}

sql查询:

QUERY_SELECT_CATEGORIES = `
select category.id as category_id,
text_translation.text as category_name,
category.parent_category_id
from category
join text_translation on text_translation.text_id=category.category_name_trans_id
and text_translation.lang_id = 1`

和函数

func PsqlGetCategories() []Category {
categories := []PsqlCategory{}
err := Psqldb.Select(&categories, QUERY_SELECT_CATEGORIES)
if err != nil {
log.Fatalf("could not parse categories: %v", err)
return nil
}
var nCategories []Category
for _, cat := range categories {
newCat := Category{
Id: cat.Id,
Name: cat.Name,
}
if cat.ParentCategoryId.Valid {
newCat.ParentCategoryId = cat.ParentCategoryId.Int64
}
nCategories = append(nCategories, newCat)
}
return nCategories
}

这是错误

could not parse categories: pq: relation "category" does not exist

这就像我完全误解了 sqlx 库的用法或者我遗漏了一些东西..

如能提供有关此问题的任何信息,我们将不胜感激。

最佳答案

问题的出现是因为您将 **PsqlProduct 传递给 Get,它认为您想要将查询结果扫描到指向的指针中,因此 “...具有 >1 列的目标类型 ptr ...”

只是改变:

err := Psqldb.Get(&product, QUERY_SELECT_PRODUCT,productId)

到:

err := Psqldb.Get(product, QUERY_SELECT_PRODUCT,productId)

关于go - 使用 sqlx 选择并获取用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com