如何正确使用字符串创建Python MySQL查询?
我正试图用Python编写一个MySQL查询,但当我试图执行它时,总是会出现语法错误#1064(42000)。已获取数据,但错误会阻止查询完成如何正确使用字符串创建Python MySQL查询?,python,mysql,string-formatting,mysql-connector,Python,Mysql,String Formatting,Mysql Connector,我正试图用Python编写一个MySQL查询,但当我试图执行它时,总是会出现语法错误#1064(42000)。已获取数据,但错误会阻止查询完成 mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for th
mysql.connector.errors.ProgrammingError: 1064
(42000): You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server
version for the right syntax to use near '1
'(data in address_line_1)' NULL '(data in city field)'
'(data in postal code field)' '(data in state code field)' 'US'
(latitude data) (longitude data) '(first two characters of is_active field data)' at line 1
它或多或少地告诉我错误在哪里,但不是什么原因造成的。我想我可能无法正确引用查询字符串中的某些内容。我不知道错误是什么,因为在我看来,查询似乎是正确的,而且我不知道Python MySQL能够诊断格式错误的所有特性
下面是表创建命令(这个命令在花费数小时调整内容后有效):
这是17个不是自动递增ID键的字段。下面是实际的插入查询:
sql = ("INSERT INTO `locations`(`location_id`, `is_valid`, `street_line_1`,"
" `street_line_2`, `city`, `postal_code`, `state_code`, `country_code`,"
" `latitude`, `longitude`, `accuracy`, `is_active`, `is_commercial`,"
" `is_forwarder`, `delivery_point`, `last_sale_date`, `total_value`)"
" VALUES(%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s)")
我错过了什么?
谢谢您的帮助。第一个建议是用逗号分隔%s
sql = ("INSERT INTO `locations`(`location_id`, `is_valid`, `street_line_1`,"
" `street_line_2`, `city`, `postal_code`, `state_code`, `country_code`,"
" `latitude`, `longitude`, `accuracy`, `is_active`, `is_commercial`,"
" `is_forwarder`, `delivery_point`, `last_sale_date`, `total_value`)"
" VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
第一个建议是用逗号分隔%s
sql = ("INSERT INTO `locations`(`location_id`, `is_valid`, `street_line_1`,"
" `street_line_2`, `city`, `postal_code`, `state_code`, `country_code`,"
" `latitude`, `longitude`, `accuracy`, `is_active`, `is_commercial`,"
" `is_forwarder`, `delivery_point`, `last_sale_date`, `total_value`)"
" VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
我猜您的值不包括“,”,因此值(%s%s…)不正确。您需要改为使用值(%s,%s…)。我猜您的值不会包括“,”,因此值(%s%s…)不正确。您需要改用值(%s,%s,…)