Code
#!/usr/bin/python import mysql.connector from mysql.connector import Error sql = """select now();""" try: connection = mysql.connector.connect(host='127.0.0.1', database='test', user='root', password='12345') if connection.is_connected() : print('Connected to MySQL database') except Error as e: print(e) with connection.cursor() as cursor: cursor.execute(sql) result = [row[0] for row in cursor] Displays "Connected to MySQL database" and fails with the error:
Connected to MySQL database Traceback (most recent call last): File "./test.py", line 16, in <module> with connection.cursor() as cursor: AttributeError: __exit__ Why and how to make work with the operator with?