如何使用sqlx对表字段和参数进行查询?

时间:2019-07-12 21:51:37

标签: go sqlx

我想问一下采用以下结构来准备查询状态的可能策略:

select TABLE_COLUM from TABLE where OTHER_TABLE_COLUMN = {'$ 1 PARAMETER'},

语言是Go和sqlx库。

我正在使用具有以下结构的函数:

type Translation struct {
    Translation string
}
type TranslateParams struct {
    Originlanguaje string 
    Wordtotranslate string
    Targetlanguaje string 
}
//TranslateWordUsingDB... TranslateWordUsingDB search one word translation in DB
func ObtainTranslationUsingDB(validationforuse bool,originlang string, word string, targetlang string)([]Translation, error){
    translation := []Translation{}
    if validationforuse == true{
        fmt.Println("preparing....")
        params := TranslateParams{
            Originlanguaje  : originlang,
            Wordtotranslate : word,
            Targetlanguaje  : targetlang,
        }
        nstmt, err := db.Conn.PrepareNamed(`select :targetlanguaje as translation from translations where :originlanguaje = :wordtotranslate`)
        if err != nil{
            return translation, err
        }
        fmt.Println("prepared")
        error := nstmt.Select(&translation, params)
        if error != nil {
            return translation, error
        }
        fmt.Println(translation)
    }
    return translation, nil
}

但这不起作用,我还没有管理https://jmoiron.github.io/sqlx/#namedParams的方法 因为它们将TABLE_COLUMN替换为“ TABLE_COLUMN”,这将使查询无法正常工作。

0 个答案:

没有答案