mysql查询?

q7solyqu  于 2021-06-08  发布在  Mysql
关注(0)|答案(2)|浏览(287)

我正试图用python编写一个mysql查询,但在执行它时,总是出现语法错误#1064(42000)。已获取数据,但该错误阻止查询完成。

  1. mysql.connector.errors.ProgrammingError: 1064
  2. (42000): You have an error in your SQL syntax; check
  3. the manual that corresponds to your MySQL server
  4. version for the right syntax to use near '1
  5. '(data in address_line_1)' NULL '(data in city field)'
  6. '(data in postal code field)' '(data in state code field)' 'US'
  7. (latitude data) (longitude data) '(first two characters of is_active field data)' at line 1

它或多或少告诉我错误在哪里,但不是什么原因造成的。我想我可能在正确引用查询字符串中的某些内容时遇到了问题。我不知道错误是什么,因为对我来说,查询似乎是正确的,而且我不知道python-mysql能够诊断格式错误的所有特性。
下面是表创建命令(在花了几个小时调整之后,这个命令有效):

  1. sql=("CREATE TABLE IF NOT EXISTS `locations` ("
  2. " `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
  3. " `location_id` VARCHAR(48),"
  4. " `is_valid` BOOLEAN,"
  5. " `street_line_1` VARCHAR(48),"
  6. " `street_line_2` VARCHAR(48),"
  7. " `city` VARCHAR(16),"
  8. " `postal_code` VARCHAR(8),"
  9. " `state_code` CHAR(2),"
  10. " `country_code` CHAR(2),"
  11. " `latitude` DECIMAL(10,6),"
  12. " `longitude` DECIMAL(10,6),"
  13. " `accuracy` VARCHAR(12),"
  14. " `is_active` BOOLEAN,"
  15. " `is_commercial` BOOLEAN,"
  16. " `is_forwarder` BOOLEAN,"
  17. " `delivery_point` VARCHAR(18),"
  18. " `last_sale_date` DATE,"
  19. " `total_value` INT(12)"
  20. ") ENGINE = InnoDB")

有17个字段不是自动递增的id键。下面是实际的insert查询:

  1. sql = ("INSERT INTO `locations`(`location_id`, `is_valid`, `street_line_1`,"
  2. " `street_line_2`, `city`, `postal_code`, `state_code`, `country_code`,"
  3. " `latitude`, `longitude`, `accuracy`, `is_active`, `is_commercial`,"
  4. " `is_forwarder`, `delivery_point`, `last_sale_date`, `total_value`)"
  5. " VALUES(%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s)")

我错过了什么?谢谢你的帮助。

cedebl8k

cedebl8k1#

第一个建议是用逗号分隔%s

  1. sql = ("INSERT INTO `locations`(`location_id`, `is_valid`, `street_line_1`,"
  2. " `street_line_2`, `city`, `postal_code`, `state_code`, `country_code`,"
  3. " `latitude`, `longitude`, `accuracy`, `is_active`, `is_commercial`,"
  4. " `is_forwarder`, `delivery_point`, `last_sale_date`, `total_value`)"
  5. " VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)")
hgc7kmma

hgc7kmma2#

我猜您的值不包括“,”,因此值(%s%s…)不正确。您需要改用值(%s,%s,…)。

相关问题