【问题标题】:mySQL Field Name Titles to avoid要避免的 mySQL 字段名称标题
【发布时间】:2021-11-10 05:08:33
【问题描述】:

我的变量值来自编辑后的网格单元格。该功能有效,但编辑的字段名称名为“读取”。我通过更改列名来修复它,但我很好奇为什么这是一个错误以及是否应该避免任何其他字段名称标题。

Message=(1064, "您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 'Read = 'ALL' where User_ID = 'j_adams58' 附近使用的正确语法' 在第 1 行")

表格字段

|用户ID |密码 |阅读 |编辑 |

 def onCellChanged(self,event):   
        #Establish connection
        self.connect_mysql()

        #Update database
        key_id =   str(self.GetCellValue(event.GetRow(),1))  
        target_col = str(self.GetColLabelValue(event.GetCol()))
        key_col = str(self.GetColLabelValue(1))                                     
        nVal =  self.GetCellValue(event.GetRow(),event.GetCol())
        sql_update = f"""Update {tbl} set {target_col} = %s where {key_col} = %s"""
        row_data = ''
       
        self.cursor.execute(sql_update, (nVal, key_id,))
        
       #Close connection
        self.close_connection()

【问题讨论】:

    标签: python mysql-python


    【解决方案1】:

    Read 是 MySQL 中的保留关键字,因此您不应将其用作列名,但如果您确实需要,您应该能够通过在其周围放置单个反引号来访问它,例如 `Read` ,但同样,这是非常糟糕的风格,你不应该这样做。您还应该避免使用其他关键字,但通常最好先尝试将在 SQL 中运行的查询,以便检查是否可以。

    见:https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 2020-04-15
      相关资源
      最近更新 更多