CBSE Class 12 Computer Science Question 21 of 47

Interface Python with SQL — Question 11

Back to all questions
11
Question

Question 11

Which function of connection is used to check whether connection to MySQL is successfully done or not?

import mysql.connector as mycon
con = mycon.connect               #ConnectionString
if ...............:
    print("Connected!")
else:
    print("Error! Not Connected")
  1. con.connected()
  2. con.isconnected()
  3. con.is_connected()
  4. con.is_connect()
Answer

con.is_connected()

Reason — In the provided code using mysql.connector, the correct function to check whether the connection to MySQL is successfully established or not is con.is_connected(). If the connection is active, it prints "Connected!". Conversely, if the connection is not active, it prints "Error! Not Connected".