dsmvqp3124 2018-08-25 15:30
浏览 181
已采纳

选择并获取sqlx的用法

I'm using go 1.10.3 and I'm trying to use the sqlx package to get one row and enter it to a struct with Get(), or get several rows and enter them to a slice with Select().

lets start with getting one row into a struct.

I created the following struct:

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"`
}

for the query:

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 
`

and I created the following function to get product by 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
    }
}

and I got the error

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

it's as if Get() function is only for one column.. but the documentation clearly states it's not!

if I change the Get() function call to Psqldb.QueryRowx(QUERY_SELECT_PRODUCT, productId).StructScan(product)

then it does work.. but still.. trying to find out why Get() doesn't work.

next.. Select()

so this is the struct

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

sql query:

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`

and the function

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
}

and this is the error

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

it's like I totally misunderstood the usage of the sqlx library or I'm missing something..

any information regarding the issue would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanmu2941 2018-08-25 18:09
    关注

    The problem arises because you're passing **PsqlProduct to Get which thinks that you want to scan the query result into the pointed to pointer, hence "... dest type ptr with >1 columns ...".

    Just change:

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

    to:

    err := Psqldb.Get(product, QUERY_SELECT_PRODUCT,productId)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器