如何正确使用字符串来创建 Python MySQL 查询?
How do I properly use strings to create a Python MySQL query?
我正在尝试在 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 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 能够诊断格式错误的所有特性。
这是 table 创建命令(这个有效,经过数小时的调整):
sql=("CREATE TABLE IF NOT EXISTS `locations` ("
" `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
" `location_id` VARCHAR(48),"
" `is_valid` BOOLEAN,"
" `street_line_1` VARCHAR(48),"
" `street_line_2` VARCHAR(48),"
" `city` VARCHAR(16),"
" `postal_code` VARCHAR(8),"
" `state_code` CHAR(2),"
" `country_code` CHAR(2),"
" `latitude` DECIMAL(10,6),"
" `longitude` DECIMAL(10,6),"
" `accuracy` VARCHAR(12),"
" `is_active` BOOLEAN,"
" `is_commercial` BOOLEAN,"
" `is_forwarder` BOOLEAN,"
" `delivery_point` VARCHAR(18),"
" `last_sale_date` DATE,"
" `total_value` INT(12)"
") ENGINE = InnoDB")
这 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)")
我猜您的值不会包含“,”,因此 VALUES(%s %s....) 不正确。您需要改用 VALUES(%s, %s, ...)。
我正在尝试在 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 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 能够诊断格式错误的所有特性。
这是 table 创建命令(这个有效,经过数小时的调整):
sql=("CREATE TABLE IF NOT EXISTS `locations` ("
" `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
" `location_id` VARCHAR(48),"
" `is_valid` BOOLEAN,"
" `street_line_1` VARCHAR(48),"
" `street_line_2` VARCHAR(48),"
" `city` VARCHAR(16),"
" `postal_code` VARCHAR(8),"
" `state_code` CHAR(2),"
" `country_code` CHAR(2),"
" `latitude` DECIMAL(10,6),"
" `longitude` DECIMAL(10,6),"
" `accuracy` VARCHAR(12),"
" `is_active` BOOLEAN,"
" `is_commercial` BOOLEAN,"
" `is_forwarder` BOOLEAN,"
" `delivery_point` VARCHAR(18),"
" `last_sale_date` DATE,"
" `total_value` INT(12)"
") ENGINE = InnoDB")
这 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)")
我猜您的值不会包含“,”,因此 VALUES(%s %s....) 不正确。您需要改用 VALUES(%s, %s, ...)。