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 7,225 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,910 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,440 points
31,811 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,210 points
12,992 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
41,382 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
6,759 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,058 views
0 votes
1 answer
+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,790 points
3,479 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
84,737 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,910 points
2,001 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP