I faced a problem connected with mysql-connector in python. Inspite the database is connected successfully and connection's cursor is instance of mysql.connector.cursor.MySQLCursor, any actions with it like .execute() return None.
So query is the last line returns None. Are there any solutions?
import mysql.connector
with mysql.connector.connect(host='localhost',
user='Tec',
passwd='TecHeres3141',
database='abiturients') as db:
print(db.is_connected())
cursor = db.cursor()
print(type(cursor), cursor)
print(cursor.execute('SELECT CURRENT_TIME();'))```
CodePudding user response:
You need to run this line of code: print(cursor.fetchall())
to get all data that you got with your cursor.execute()
. For example if you are doing a simple SELECT * FROM table
you need to use fetchall()
to get all results and then print it. Also to apply changes to your database use db.commit()
.