【发布时间】:2018-11-04 19:25:22
【问题描述】:
我用 python 编写了一个脚本来从网站上抓取一些数据并将它们存储在 mysql 中。如果我选择两个选项来插入数据,我的脚本会成功完成这项工作:
mycursor.execute("INSERT INTO webdata (name,bubble,review) VALUES ('{}','{}','{}')".format(name,bubble,review))
mycursor.execute("INSERT INTO webdata (name,bubble,review) VALUES (%s,%s,%s)",(name,bubble,review))
但是,当我尝试使用 python's new string formatting 执行相同操作时会引发错误,如下所示:
mycursor.execute("INSERT INTO webdata (name,bubble,review) VALUES (f'{name},{bubble},{review}')")
它抛出的错误:
line 429, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''{name},{bubble},{review}')' at line 1
我哪里出了问题以及如何解决它,因为我非常愿意坚持最后的格式样式?
【问题讨论】:
-
对参数化查询使用第二个选项:bobby-tables.comdon`t do string interpolation
标签: python mysql python-3.x string-formatting