gpt4 book ai didi

python - 属性错误: 'function' object has no attribute 'encode' error when writing to MySQL database

转载 作者:行者123 更新时间:2023-11-29 17:49:17 26 4
gpt4 key购买 nike

我正在尝试从文件中读取 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()

最佳答案

我决定尝试MySQLdb现在效果很好。不知道 MySQL connector 出了什么问题

关于python - 属性错误: 'function' object has no attribute 'encode' error when writing to MySQL database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49525028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com