douchi5822 2015-07-22 23:46
浏览 21
已采纳

使用'和'指示数据库列名称的正确方法是什么?

I have a table in my DB that the column names all start with caps (ie: FMR_Number, Primary_Value). The column name are out of my control. All the other columns in the DB (in other tables) are lower case. I have mapped a struct to the table but I keep getting a missing destination err from the query. I believe it is because the name mapper is set to ToLower (which is what it needs for everywhere else). I attempted to use this format

StructField string 'db:"RealColumnName"'  

to specify the mapping explicitly but I get an illegal rune literal. When I go look at examples in other peoples code I see it being represented in the way I just tried it. If I reverse the ' and " to

StructField string "db:'RealColumnName'"  

I don't get the illegal rune literal error but I do get the missing destination err. What am I doing wrong here? I am using sqlx.

My Struct

type pah struct {
    FMR_Fund_Number                  string  'db:"FMR_Fund_Number"'
    Business_Data_Date               string  'db:"Business_Data_Date"'
    Outbound_Composition_Code        string  'db:"Outbound_Composition_Code"'
    Composition_Sub_Code             string  'db:"Composition_Sub_Code"'
    Composition_Sub_Code_Description string  'db:"Composition_Sub_Code_Description"'
    Position_Name                    string  'db:"Position_Name"'
    Primary_Value                    float64 'db:"Primary_Value"'
    Record_Order                     int     'db:"Record_Order"'
    Tier_Indicator                   string  'db:"Tier_Indicator"'
}

My db call

func loadAssetAllocation() AssetAllocations {
    pahGroup := []pah{}
    pahQuery := "SELECT FMR_Fund_Number,Business_Data_Date,Outbound_Composition_Code,Composition_Sub_Code,Composition_Sub_Code_Description,Primary_Value,Tier_Indicator,Record_Order FROM PAH WHERE FMR_Fund_Number = \"312\" AND Outbound_Composition_Code = \"MDCAT\" AND Business_Data_Date = (SELECT Business_Data_Date FROM PAH ORDER BY Business_Data_Date DESC LIMIT 1) AND Composition_Sub_Code <> \"TAXADV\" ORDER BY Record_Order ASC"
    assetAlloc := AssetAllocations{}
    err := ffDB.Select(&pahGroup, pahQuery)
    fmt.Println("pahQuery AssetAllocation error: ", err)
    assetAlloc.AssetAll = pahGroup
    return assetAlloc
}
  • 写回答

1条回答 默认 最新

  • dongzhazhuo0572 2015-07-23 00:34
    关注

    Struct tags use backticks - the ` character (commonly under the Esc key on US keyboard layouts) - not single quotes.

    Your struct should look like the below, noting that underscores in variable names aren't particularly idiomatic in Go.

    The reason you use struct tags is often to map exported (capitalised) Go struct field names to the names used by other things (JSON, databases, HTML forms, etc).

    type pah struct {
        FMRFundNumber                  string  `db:"FMR_Fund_Number"`
        BusinessDataDate               string  `db:"Business_Data_Date"`
        OutboundCompositionCode        string  `db:"Outbound_Composition_Code"`
        CompositionSubCode             string  `db:"Composition_Sub_Code"`
        CompositionSubCodeDescription string  `db:"Composition_Sub_Code_Description"`
        PositionName                    string  `db:"Position_Name"`
        PrimaryValue                    float64 `db:"Primary_Value"`
        RecordOrder                     int     `db:"Record_Order"`
        TierIndicator                   string  `db:"Tier_Indicator"`
    }
    

    See the docs: http://golang.org/pkg/reflect/#StructTag and http://golang.org/pkg/encoding/json/#Marshal

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP