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 in Python by Imthiiiii
• 120 points

edited Sep 17 by Gitika 153 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 by Rajiv
• 3,160 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 in Python by MD
• 65,200 points
19,203 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,030 points
6,087 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 in Python by rahul
• 360 points
27,573 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
2,905 views
–1 vote
1 answer

Python error saying "NameError: name 'email' is not defined"

you need to define the variable email READ MORE

answered Nov 29, 2019 in Python by Casper
• 160 points
2,804 views
+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 1,452 views
0 votes
0 answers
+4 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,800 points
1,085 views
0 votes
1 answer

'tuple' object is not callable

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

answered Aug 13 in Python by Rajiv
• 3,160 points
382 views