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 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题