Answers for "python mysql adapter"

4

how to connect to mysql database in python

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees')
cnx.close()
Posted by: Guest on March-23-2020
0

mysql connector python

import mysql.connector
try:
    connection = mysql.connector.connect(host='localhost',database='dbname',user='root',password='xxxxx')
    # print(dir(connection))
    # print(connection.connection_id)
    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("except")
    print("Error while connecting to MySQL", e)
finally:
    if (connection.is_connected()):
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Posted by: Guest on April-26-2020

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language