dongzhou8764 2016-03-27 19:38
浏览 352
已采纳

去数据库/ SQL更改表名的大小写

I am trying to query a database using database/sql and the github.com/lib/pq Postgres driver. The error I'm encountering is:

pq: relation "itemprices_itemsale" does not exist

However looking at my query:

rows, err := db.Query("SELECT * FROM \"itemPrices_itemsale\" LIMIT 10")

You'll notice the capital 'P' in the table name. I've learned form poking around that Postgres will fold names in to lowercase if they are not quoted. I have quoted my table name so I'm not quite sure why this is happening. I'm fairly certain this is the issue as I'm able to query the table using that table name from a similar Python program and everything is working as expected.

Update: Using @JohnWeldon's suggestion:

var table = "itemPrices_itemsale"
rows, err := db.Query(fmt.Sprintf("SELECT * FROM %s LIMIT 10", pq.QuoteIdentifier(table)))
  • 写回答

1条回答 默认 最新

  • douzhongjian0752 2016-03-27 23:50
    关注

    Try using the QuoteIdentifier function in github.com/lib/pq to quote your table name:

    https://godoc.org/github.com/lib/pq#QuoteIdentifier

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

报告相同问题?

悬赏问题

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