使用单引号(’)和双引号(”)来表示数据库列名的正确方式是什么?

huangapple go评论89阅读模式
英文:

What is the correct way to use ' and " for indicating db column names?

问题

我在我的数据库中有一张表,其中所有列的列名都以大写字母开头(例如:FMR_Number,Primary_Value)。列名是我无法控制的。数据库中的所有其他列(在其他表中)都是小写字母。我已经将一个结构体映射到了这个表,但是在查询时我一直收到一个缺少目标的错误。我相信这是因为名称映射器被设置为 ToLower(这是其他地方所需的)。我尝试使用以下格式来显式指定映射:

StructField string 'db:"RealColumnName"'

但是我得到了一个非法的 rune 字面量错误。当我查看其他人的代码示例时,我看到它以我刚刚尝试的方式表示。如果我将 ' 和 " 颠倒为:

StructField string "db:'RealColumnName'"

我就不会得到非法的 rune 字面量错误,但是我会得到缺少目标的错误。我在这里做错了什么?我正在使用 sqlx。

我的结构体:

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

我的数据库调用:

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
}
英文:

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 &#39;db:&quot;RealColumnName&quot;&#39;  

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 &quot;db:&#39;RealColumnName&#39;&quot;  

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  &#39;db:&quot;FMR_Fund_Number&quot;&#39;
    Business_Data_Date               string  &#39;db:&quot;Business_Data_Date&quot;&#39;
    Outbound_Composition_Code        string  &#39;db:&quot;Outbound_Composition_Code&quot;&#39;
    Composition_Sub_Code             string  &#39;db:&quot;Composition_Sub_Code&quot;&#39;
    Composition_Sub_Code_Description string  &#39;db:&quot;Composition_Sub_Code_Description&quot;&#39;
    Position_Name                    string  &#39;db:&quot;Position_Name&quot;&#39;
    Primary_Value                    float64 &#39;db:&quot;Primary_Value&quot;&#39;
    Record_Order                     int     &#39;db:&quot;Record_Order&quot;&#39;
    Tier_Indicator                   string  &#39;db:&quot;Tier_Indicator&quot;&#39;
}

My db call

func loadAssetAllocation() AssetAllocations {
    pahGroup := []pah{}
    pahQuery := &quot;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 = \&quot;312\&quot; AND Outbound_Composition_Code = \&quot;MDCAT\&quot; AND Business_Data_Date = (SELECT Business_Data_Date FROM PAH ORDER BY Business_Data_Date DESC LIMIT 1) AND Composition_Sub_Code &lt;&gt; \&quot;TAXADV\&quot; ORDER BY Record_Order ASC&quot;
    assetAlloc := AssetAllocations{}
    err := ffDB.Select(&amp;pahGroup, pahQuery)
    fmt.Println(&quot;pahQuery AssetAllocation error: &quot;, err)
    assetAlloc.AssetAll = pahGroup
    return assetAlloc
}

答案1

得分: 2

结构标签使用反引号(`)字符(通常在美国键盘布局的Esc键下方),而不是单引号。

你的结构体应该像下面这样,注意在变量名中使用下划线在Go语言中不是特别惯用的。

使用结构标签的原因通常是将Go导出的(首字母大写的)结构体字段名称映射到其他内容使用的名称(JSON、数据库、HTML表单等)。

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

参考文档:http://golang.org/pkg/reflect/#StructTag 和 http://golang.org/pkg/encoding/json/#Marshal

英文:

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:&quot;FMR_Fund_Number&quot;`
    BusinessDataDate               string  `db:&quot;Business_Data_Date&quot;`
    OutboundCompositionCode        string  `db:&quot;Outbound_Composition_Code&quot;`
    CompositionSubCode             string  `db:&quot;Composition_Sub_Code&quot;`
    CompositionSubCodeDescription string  `db:&quot;Composition_Sub_Code_Description&quot;`
    PositionName                    string  `db:&quot;Position_Name&quot;`
    PrimaryValue                    float64 `db:&quot;Primary_Value&quot;`
    RecordOrder                     int     `db:&quot;Record_Order&quot;`
    TierIndicator                   string  `db:&quot;Tier_Indicator&quot;`
}

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

huangapple
  • 本文由 发表于 2015年7月23日 07:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/31575886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定