Import mysql connector in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.
    MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies.
     

    Connecting to the Database

    In the following example we will be connecting to MySQL database using connect()
    Example:
     

    Python3

    import mysql.connector

    conn = mysql.connector.connect(user = 'username',

                                   host = 'localhost',

                                  database = 'database_name')

    print(conn)

    conn.close()

    Output:
     

    Import mysql connector in python

    Also for the same, we can use connection.MySQLConnection() class instead of connect():
    Example:
     

    Python3

    from mysql.connector import connection

    conn = connection.MySQLConnection(user = 'username',

                                  host = 'localhost',

                                  database = 'database_name')

    print(conn)

    conn.close()

    Output:
     

    Import mysql connector in python

    Another way is to pass the dictionary in the connect() function using ‘**’ operator:
    Example:
     

    Python3

    from mysql.connector import connection

    dict = {

      'user': 'root',

      'host': 'localhost',

      'database': 'College'

    }

    conn = connection.MySQLConnection(**dict)

    print(conn)

    conn.close()

    Output:
     

    Import mysql connector in python


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisite: Python Language Introduction
     
    MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases.
    A connector is employed when we have to use MySQL with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

    Installation:

    To install Python-mysql-connector module, one must have Python and PIP, preinstalled on their system. To check if your system already contains Python, go through the following instructions:

    Open the Command line(search for cmd in the Run dialog( + R).
    Now run the following command:

    python --version

    If Python is already installed, it will generate a message with the Python version available.

    Import mysql connector in python

    If Python is not present, go through How to install Python on Windows and Linux? and follow the instructions provided.
     
    PIP is a package management system used to install and manage software packages/libraries written in Python. These files are stored in a large “on-line repository” termed as Python Package Index (PyPI).
    To check if PIP is already installed on your system, just go to the command line and execute the following command:

    pip -V

    Import mysql connector in python

    If PIP is not present, go through How to install PIP on Windows and Linux?

    Windows

    mysql-connector method can be installed on Windows with the use of following command:

    pip install mysql-connector-python

    Import mysql connector in python

    Linux

    mysql-connector method can be installed on Linux with the use of following command:

    pip3 install mysql-connector

    Import mysql connector in python

    How do I import MySQL Connector in Python?

    How to connect MySQL database in Python.
    Install MySQL connector module. Use the pip command to install MySQL connector Python. ... .
    Import MySQL connector module. ... .
    Use the connect() method. ... .
    Use the cursor() method. ... .
    Use the execute() method. ... .
    Extract result using fetchall() ... .
    Close cursor and connection objects..

    Is MySQL Connector a library in Python?

    0 (PEP 249). It is written in pure Python and does not have any dependencies except for the Python Standard Library. ... There are various versions of MySQL Connector/Python available:.

    What is MySQL Connector connect in Python?

    connect() Method. This method sets up a connection, establishing a session with the MySQL server. If no arguments are given, it uses the already configured or default values. For a complete list of possible arguments, see Section 7.1, “Connector/Python Connection Arguments”.

    Which command is used to install MySQL Connector in Python?

    Pip Command to install MySQL Connector python MySQL Connector Python is available on pypi.org, so you can install it using the pip command.