我正在尝试检索mysql中的所有行。
import mysql.connector,sys
user,pw, host,db = 'root','12345','127.0.0.1','CA2Database'
cnx = mysql.connector.connect(user=user, password=pw, host=host, database=db)
cursor = cnx.cursor()
try:
print(cursor.execute('SELECT * FROM student'))
except mysql.connector.Error as err:
print(err)
print("Error Code:", err.errno)
print("SQLSTATE", err.sqlstate)
print("Message", err.msg)
finally:
print()
输出为None
。表中有数据。
https://imgur.com/a/APUuZot
答案 0 :(得分:1)
可能您缺少一些必要的函数调用。这样做:
user,pw, host,db = 'root','12345','127.0.0.1','CA2Database'
cnx = mysql.connector.connect(user=user, password=pw, host=host, database=db)
cursor = cnx.cursor()
sql_query = 'SELECT * FROM student;'
cursor.execute(sql_query)
data = cursor.fetchall()
cnx.commit()
cursor.close()