0

I wrote a script, and then it went wrong, but in the python shell, I typed the same code in the script and it worked. What happened? Here's my code:

# -*-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()

Here's my error message:

(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
2
  • So your line cursor = .. wasn't executed. Exception appeared in connect() method. Commented Mar 21, 2019 at 3:24
  • I changed MySQL 5.7 to 5.1, and the problem was solved without modifying the code. Thank you for your reply! Commented Mar 21, 2019 at 6:49

1 Answer 1

1

that's the kind of error you get when the user or password are wrong, maybe you should check the line where you start the connection, it's the reason why cursor is not defined.

Sign up to request clarification or add additional context in comments.

2 Comments

i can use the password enter mysql ,and in the python shell it worked,but when i ran this script,it made a mistake
I changed MySQL 5.7 to 5.1, and the problem was solved without modifying the code. Thank you for your reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.