我正在尝试从文件中读取JSON并将其插入到MySQL数据库中。但我收到以下错误:
Traceback (most recent call last):
File "loopedadd.py", line 44, in <module>
cursor.execute(add_record, data_record)
File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 536, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'function' object has no attribute 'encode'
我尝试与use_unicode=True, charset='utf8'
联系无济于事。
非常感谢任何有关如何修复此错误的帮助。
我的代码如下:
import mysql.connector
import Queue, json
cnx = mysql.connector.connect(user="root",
host="192.168.1.64",
password="....",
database="records",
port=3306)
print("connected")
cursor = cnx.cursor()
queue = Queue.Queue()
content = json.load(open("data.txt","r"))
for jsonobj in content:
queue.put(jsonobj)
add_record = (
"INSERT INTO records "
"(id, timestamp, location, value, unit) "
"VALUES (%s, %s, %s, %s, %s)")
while not queue.empty():
values = queue.get()
data_record = (values['id'], values['timestamp'],
values['location'], values['reading'], values['unit'])
cursor.execute(add_record, data_record)
cnx.commit()
queue.task_done()