I want to connect to a mySQL Server and get all users from the users table. But if i try to execute my code, it says:
/bin/python /home/luca/PycharmProjects/Test/sql.py
Traceback (most recent call last):
File "/home/luca/PycharmProjects/Test/sql.py", line 18, in <module>
for x in get_user_by_username("testuser"):
File "/usr/lib/python3.8/site-packages/mysql/connector/cursor.py", line 861, in fetchone
row = self._fetch_row()
File "/usr/lib/python3.8/site-packages/mysql/connector/cursor.py", line 831, in _fetch_row
if not self._have_unread_result():
File "/usr/lib/python3.8/site-packages/mysql/connector/cursor.py", line 351, in _have_unread_result
return self._connection.unread_result
ReferenceError: weakly-referenced object no longer exists
Process finished with exit code 1
Here is my code
import mysql.connector
def get_user_by_username(username):
mydb = mysql.connector.connect(
host="localhost",
user="testuser",
passwd="k3gc8pHPvEtGqND",
database="test"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM users")
return mycursor
for x in get_user_by_username("testuser"):
print(x)
Why is this happening and how can I fix it?