shows name connection not defined

0 votes

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")
Sep 17, 2020 in Python by Imthiiiii
• 120 points

edited Sep 17, 2020 by Gitika 1,774 views

1 answer to this question.

0 votes

Hi, @Imthiii,

Try removing the bracket you have given in the line-17th 

if (connection.is_connected()):
to
This
if connection.is_connected():
answered Sep 17, 2020 by Rajiv
• 8,880 points
still gives the same error

Hey, @There,

Did you try out the solution given by @Rajiv? It should resolve the error.

Related Questions In Python

0 votes
2 answers

NameError: name 'raw_input' is not defined

Hi, There may a problem with your python ...READ MORE

answered Jun 25, 2020 in Python by MD
• 95,220 points
25,847 views
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,150 points
8,127 views
0 votes
3 answers

Python error "NameError: name 'sr' is not defined"

NameError: name 'xx' is not defined Python knows ...READ MORE

answered Mar 19, 2020 in Python by rahul
• 360 points
34,012 views
0 votes
1 answer

Python error "Python NameError: global name 'Thread' is not defined"

There is a built-in function with the ...READ MORE

answered May 31, 2019 in Python by Riya
4,068 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,189 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,650 views
0 votes
3 answers

Python Pandas: ValueError: DataFrame constructor not properly called!

The syntax is not right. The right ...READ MORE

answered Mar 28, 2019 in Python by Ritu
56,043 views
0 votes
1 answer

'tuple' object is not callable

Hey, @There, This kind of error happens once ...READ MORE

answered Aug 13, 2020 in Python by Rajiv
• 8,880 points
1,332 views