gpt4 book ai didi

mysql - PyMySQL 游标中的类型错误

转载 作者:行者123 更新时间:2023-11-30 21:50:40 27 4
gpt4 key购买 nike

我有以下简单的代码来测试我的 Python Mysql 连接。

connection = pymysql.connect(host = "localhost",
user = "root",
passwd = "",
db = "bugs",
charset = "utf8mb4",
cursorclass = "pymysql.cursors.Dictcursor")

try:
with connection.cursor() as cur:
cur.execute('''SELECT COUNT(*) FROM tbl_firefox''')

for row in cur.fetchall():
print("Number of rows in firefox table:", row)
connection.commit()
finally:
connection.close()

当我运行代码时出现类型错误

File "test2.py", line 31, in <module>
with connection.cursor() as cur:
File "C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py", line 833, in cursor
return self.cursorclass(self)
TypeError: 'str' object is not callable

我一直在查看其他 python-mysql 代码中的类似错误。但仍然没有运气。如果有人能指出错误,非常感谢。

最佳答案

引自 PyMySQL :

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()

with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()

应该是passwd以外的密码,cursorclass没有引号

关于mysql - PyMySQL 游标中的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47422466/

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