Reputation: 365
I'm trying to write a MySQL query in Python, but I keep getting syntax error # 1064 (42000) when I try to execute it. The data is fetched, but the error prevents the query from finishing.
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
It tells me more or less where the error was, but not what caused it. I think I might be having trouble properly quoting something in the query string. I can't tell what the error is, because, to me, the query seems right, and I don't know all the peculiarities of Python MySQL to be able to diagnose formatting errors.
Here's the table creation command (this one works, after spending hours tweaking things):
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")
That's 17 fields that are not the auto-incremented ID key. Here's the actual insert query:
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)")
What am I missing? Thank you for the help.
Upvotes: 0
Views: 73
Reputation: 990
I guess your values won't include "," so VALUES(%s %s....) is incorrect. You need to use VALUES(%s, %s, ...) instead.
Upvotes: 1
Reputation: 133360
a first suggestion is for separate the %s with comma
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)")
Upvotes: 2