hello guys,
while trying to connect Python with mysql, it shows error in line 17 -if (connection.is_connected()):
, saying "name connection not defined"
Please helP me out, here's my code:
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',database='abc',user='root',password='imt')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")