我写了一个脚本,然后出错了,但是在python shell中,我在脚本中键入了相同的代码,它起作用了。发生了什么?这是我的代码:
# -*-coding:utf-8-*-
import pymysql
try:
conn = pymysql.connect('localhost', 'root', 'Hjd4132!', 'pytest')
cursor = conn.cursor()
cursor.execute('SELECT * from demo')
data = cursor.fetchone()
print(data[3])
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
这是我的错误消息:
(1045, u"Access denied for user 'root'@'localhost' (using password: YES)")
Traceback (most recent call last):
File "demo.py", line 12, in <module>
cursor.close()
NameError: name 'cursor' is not defined
答案 0 :(得分:1)
这是当用户名或密码错误时出现的错误,也许您应该检查启动连接的行,这就是未定义游标的原因。