python mysql connector

Connecting to MySQL in Python-: For creating Creating a connection using the mysql.connector.connect() function gives access to all connection-related features. There are several way for connection to MySQL here are some …

  • The mysql.connector.connect() function: This is the most flexible connection method. It provides a uniform way of creating connections using the C Extension or enabling the connection pooling and the failover-related options. This function works as a wrapper that returns an object of the appropriate class depending on the settings.
  • The MySQLConnection() constructor
  • The MySQLConnection.connect() method: It requires first instantiating the MySQLConnection class without arguments and then creating the connection.
  • The same as before using the MySQLConnection.connect() method but with the difference that the MySQLConnection.config() method is called explicitly to configure the connection.

 

python mysql connector

Example of connection -:It is time to combine the four ways of creating a connection as well as the most common connection option to create source code examples for creating MySQL Connector/Python connections. For a example how to connect using the four ways of creating the connection. The examples are in the same ..

import mysql.connector
connect_args = {
  "host": "127.0.0.1",
  "port": 3306,
  "user": "pyuser",
  "password": "Py@pp4Demo",
};
# ---- connect() function ----
db1 = mysql.connector.connect(
  **connect_args
)
print(
  "MySQL connection ID for db1: {0}"
  .format(db1.connection_id)
)
db1.close()
# ---- Explicit MySQLConnection ----
db2 = mysql.connector.MySQLConnection(
  **connect_args
)
print(
  "MySQL connection ID for db2: {0}"
  .format(db2.connection_id)
)
db2.close()
# ---- Two steps manually ----
db3 = mysql.connector.MySQLConnection()
db3.connect(**connect_args)
print(
  "MySQL connection ID for db3: {0}"
  .format(db3.connection_id)
)
db3.close()
# ---- All three steps manually ----
db4 = mysql.connector.MySQLConnection()
db4.config(**connect_args)
db4.connect()
print(
  "MySQL connection ID for db4: {0}"
  .format(db4.connection_id)
)
db4.close()

 

Create a database -:  For creating a database in MySQL we can use the create statement as like

Create database netnic

here netnic is the name of database. complete example of creating database using python

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE netnic")

 

By the help of this query we can create a database in MySQL.

Create a table -: To create a table in MySQL, use the “CREATE TABLE” statement.We can  define the name of the database when we create the connection as like

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="netnic"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

 

When we execute this statement  a table was created.

 

Leave a Comment