【问题标题】:Python: Inserting row into database with formattingPython:使用格式化将行插入数据库
【发布时间】:2011-06-30 20:27:17
【问题描述】:

我正在尝试将一行插入到我在数据库中设置的表中。

我的代码:

cursor.execute("""INSERT INTO scan (prefix, code_id, answer, station) VALUES(%s, %s, %s, %s, timestamp, comport)""",[(prefix, code_id, answer, station)])

我收到一个错误,说它不喜欢“[]”中的内容。

我不完全理解“%s”是如何工作的。我尝试了很多例子,但都失败了。

我从http://mysql-python.sourceforge.net/MySQLdb.html得到这个例子

【问题讨论】:

  • 可惜这么多回答者似乎没有听说过 SQL 注入。永远记住小约翰尼桌子,伙计们。
  • @Daniel 你有避免 SQL 注入的建议吗?
  • 你的想法是对的,RoundTower 应该让你走上正轨。请,请忽略古纳丁和加布里埃尔的回答,因为他们肯定会给你带来各种各样的麻烦。

标签: python mysql mysql-python


【解决方案1】:

为什么你有两组括号内的参数?第二个参数应该是一个包含四个项目的序列,以匹配查询中的四个%ss。相反,您给了它一个包含一个项目的列表:该项目是一个包含四个项目的序列。

请尝试:

cursor.execute("""INSERT INTO scan (prefix, code_id, answer, station) VALUES(%s, %s, %s, %s, timestamp, comport)""",(prefix, code_id, answer, station))

【讨论】:

  • 回溯(最近一次调用最后一次):文件“test.py”,第 48 行,在 cursor.execute("""INSERT INTO scan (prefix, code_id, answer, station) VALUES (%s, %s, %s, %s, timestamp, comport)""",(prefix, code_id, answer, station)) 文件 "C:\Python27\lib\site-packages\MySQLdb\cursors.py" ,第 174 行,在执行 self.errorhandler(self, exc, value) 文件“C:\Python27\lib\site-packages\MySQLdb\connections.py”,第 36 行,在默认的 lterrorhandler 中引发 errorclass,errorvalue _mysql_exceptions.OperationalError: (1136,“列计数与第 1 行的值计数不匹配”)
  • 呃,因为您在第一组括号中有四列定义要输入的列,第二组中有六列定义实际值...
  • @Daniel 啊,我明白了。因此,如果我将其他 2 列添加到第一列,理论上它应该可以正常工作吗?
  • @daniel 当我尝试将其他 2 个添加到第一个括号中时,我得到了 Traceback(最近一次调用最后):文件“test.py”,第 48 行,在 cursor.execute ("""INSERT INTO scan (prefix, code_id, answer, station,timesta mp,comport) VALUES(%s, %s, %s, %s, timestamp, comport)""",[(prefix, code_id, ans wer, station)]) 文件“C:\Python27\lib\site-packages\MySQLdb\cursors.py”,第 159 行,在执行查询 = 查询 % db.literal(args) 类型错误:格式字符串的参数不足
  • 但是现在您已经恢复了参数列表中不必要的方括号!另外,您需要从某处获取timestampcomport 的值。
【解决方案2】:
cursor.execute('INSERT INTO scan (prefix, code_id, answer, station) VALUES("%s", "%s", "%s", "%s")' %(prefix, code_id, answer, station))

如果您错过了%s 周围的"",即如果您在值中仅使用%s 而不是这个"%s",则可能会出现错误。

【讨论】:

  • Traceback (most recent call last): File "test.py", line 48, in <module> cursor.execute("INSERT INTO scan (prefix, code_id, answer, station) VALUES(% s, %s, %s, %s, timestamp, comport)" %(prefix, code_id, answer, station)) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defau lterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1136, "Column count doesn't match value cou nt at row 1")
  • 我不明白你为什么用timestamp, comport
  • @Guanidene 我正在使用时间戳,comport 因为它们是我要插入的表中的字段。
  • @Guanidene 我刚刚使用了您发布的编辑片段,除了操作错误显示字段列表中的未知列站外,我在这里得到了相同的结果
  • @MarcBrigham 让我们continue this discussion in chat
【解决方案3】:

您可以使用 Python 新的“格式”字符串方法:

cursor.execute("""INSERT INTO scan (prefix, code_id, answer, station) VALUES({}, {}, {}, {}, timestamp, comport)""".format(prefix, code_id, answer, station))

您还可以对参数进行编号,例如“{0}”、“{1}”等。根据您的 Python 版本,这可能是必需的(我认为 2.6 已经支持不带编号的参数)。

有关使用此功能的参考,请查看http://docs.python.org/library/string.html#format-examples

【讨论】:

  • 这不起作用。我收到了 _mysql_operational 错误。 station 是否为 INT 数据类型有关系吗?
  • @gabriel Traceback (most recent call last): File "test.py", line 48, in <module> cursor.execute("""INSERT INTO scan (prefix, code_id, answer, station) VALUES ({}, {}, {}, {}, timestamp, comport)""".format(prefix, code_id, answer, station) ) File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defau lterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1136, "Column count doesn't match value cou nt at row 1")
  • 你也需要给“timestamp”和“comport”赋值(不仅仅是他们的名字)。此外,您需要在字段列表中添加“timestamp”和“comport”。它将是这样的: INSERT INTO scan (prefix, code_id, answer, station, timestamp, comport) VALUES({}, {}, {}, {}, {}, {}) 然后,在“格式”参数,您需要再输入两个值:'timestamp' 和 'comport'。
  • Timestamp 会在我创建字段时自动生成时间戳。我在 MySQL 中使用它的日期时间值
  • @gabriel comport 未分配值
猜你喜欢
  • 2013-05-05
  • 1970-01-01
  • 2018-07-13
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多