尝试以二进制形式插入 uuid 数据 - 总是出现语法错误
Trying to insert uuid data as binary - always get syntax error
我正在尝试通过 python 在 MySql DB
中插入一个 uuid
数据作为 binary(16)
。我正在按照 python 教程进行操作,仅针对插入语法,并且总是得到一个 syntax error
并且我不知道错误在哪里。
对于 uuid
部分,我在 SO
中看到了一些 post,我认为应该可行。
ps:我是新手。
我有一个单独的 class 作为连接句柄。
代码:
import uuid
import json
from db_connection import Connection
f = open('logs.txt', "r")
lines = f.readline()
json_line = json.loads(lines)
print(json_line)
add_consumer = ("INSERT INTO consumer "
"(id) "
"VALUES (%s)")
#consultation purpose - from:https://dev.mysql.com/doc/connector-python/en/connector-python-#example-cursor-transaction.html
#
# add_employee = ("INSERT INTO employees "
# "(first_name, last_name, hire_date, gender, birth_date) "
# "VALUES (%s, %s, %s, %s, %s)")
# add_salary = ("INSERT INTO salaries "
# "(emp_no, salary, from_date, to_date) "
# "VALUES (%(emp_no)s, %(salary)s, %(from_date)s, %(to_date)s)")
connection = Connection()
connection.open_connection()
cursor = connection.get_cursor()
consumer = json_line['authenticated_entity']['consumer_id']['uuid']
consumer_uuid = uuid.UUID(consumer)
print("\n", consumer, "\n", consumer_uuid)
#prints
#72b34d31-4c14-3bae-9cc6-516a0939c9d6
#72b34d31-4c14-3bae-9cc6-516a0939c9d6
cursor.execute(add_consumer, consumer_uuid.bytes)
connection.commit_data()
cursor.close()
connection.close_connection()
错误:
Traceback (most recent call last):
File "data_parse.py", line 33, in <module>
cursor.execute(add_consumer, consumer_uuid.bytes)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor.py", line 577, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 695, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 582, 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 MySQL server version for the right syntax to use near '%s)' at line 1
cursor.execute() 的参数参数必须是元组或字典。
示例:
cursor.execute(add_consumer, (consumer_uuid.bytes,) )
我正在尝试通过 python 在 MySql DB
中插入一个 uuid
数据作为 binary(16)
。我正在按照 python 教程进行操作,仅针对插入语法,并且总是得到一个 syntax error
并且我不知道错误在哪里。
对于 uuid
部分,我在 SO
中看到了一些 post,我认为应该可行。
ps:我是新手。
我有一个单独的 class 作为连接句柄。
代码:
import uuid
import json
from db_connection import Connection
f = open('logs.txt', "r")
lines = f.readline()
json_line = json.loads(lines)
print(json_line)
add_consumer = ("INSERT INTO consumer "
"(id) "
"VALUES (%s)")
#consultation purpose - from:https://dev.mysql.com/doc/connector-python/en/connector-python-#example-cursor-transaction.html
#
# add_employee = ("INSERT INTO employees "
# "(first_name, last_name, hire_date, gender, birth_date) "
# "VALUES (%s, %s, %s, %s, %s)")
# add_salary = ("INSERT INTO salaries "
# "(emp_no, salary, from_date, to_date) "
# "VALUES (%(emp_no)s, %(salary)s, %(from_date)s, %(to_date)s)")
connection = Connection()
connection.open_connection()
cursor = connection.get_cursor()
consumer = json_line['authenticated_entity']['consumer_id']['uuid']
consumer_uuid = uuid.UUID(consumer)
print("\n", consumer, "\n", consumer_uuid)
#prints
#72b34d31-4c14-3bae-9cc6-516a0939c9d6
#72b34d31-4c14-3bae-9cc6-516a0939c9d6
cursor.execute(add_consumer, consumer_uuid.bytes)
connection.commit_data()
cursor.close()
connection.close_connection()
错误:
Traceback (most recent call last):
File "data_parse.py", line 33, in <module>
cursor.execute(add_consumer, consumer_uuid.bytes)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/cursor.py", line 577, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 695, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 582, 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 MySQL server version for the right syntax to use near '%s)' at line 1
cursor.execute() 的参数参数必须是元组或字典。
示例:
cursor.execute(add_consumer, (consumer_uuid.bytes,) )