How to connect mysql to python in windows


Python can be used in database applications.

One of the most popular databases is MySQL.


MySQL Database

To be able to experiment with the code examples in this tutorial, you should have MySQL installed on your computer.

You can download a free MySQL database at https://www.mysql.com/downloads/.


Install MySQL Driver

Python needs a MySQL driver to access the MySQL database.

In this tutorial we will use the driver "MySQL Connector".

We recommend that you use PIP to install "MySQL Connector".

PIP is most likely already installed in your Python environment.

Navigate your command line to the location of PIP, and type the following:

Download and install "MySQL Connector":

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install mysql-connector-python

Now you have downloaded and installed a MySQL driver.


Test MySQL Connector

To test if the installation was successful, or if you already have "MySQL Connector" installed, create a Python page with the following content:

If the above code was executed with no errors, "MySQL Connector" is installed and ready to be used.



Create Connection

Start by creating a connection to the database.

Use the username and password from your MySQL database:

demo_mysql_connection.py:

import mysql.connector

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

print(mydb)

Run example »

Now you can start querying the database using SQL statements.



This is a quick tutorial on how to get Python 3.7 working with Mysql
Thanks to all from who I got answers to my questions
- hope this helps somebody someday.
----------------------------------------------------
My System:
Windows Version: Pro 64-bit

REQUIREMENTS.. download and install these first...
1. Download Xampp..
https://www.apachefriends.org/download.html
2. Download Python
https://www.python.org/downloads/windows/

--------------
//METHOD
--------------
Install xampp first after finished installing - install Python 3.7.
Once finished installing both - reboot your windows system.
Now start xampp and from the control panel - start the mysql server.
Confirm the versions by opening up CMD and in the terminal type

c:\>cd c:\xampp\mysql\bin

c:\xampp\mysql\bin>mysql -h localhost -v
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.21-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

This is to check the MYSQL version

c:\xampp\mysql\bin>python
Python 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 18:42:04) [MSC v.1913 64 bit (AMD64)] on win32

This is to check the Python version
Now that both have been confirmed type the following into the CMD...

c:\xampp\mysql\bin>pip install pymysql

After the install of pymysql is completed.
create a new file called "testconn.py" on your desktop or whereever for quick access.
Open this file with sublime or another text editor and put this into it.
Remember to change the settings to reflect your database.

#!/usr/bin/python
import pymysql
pymysql.install_as_MySQLdb() 

import MySQLdb
db = MySQLdb.connect(user="yourusernamehere",passwd="yourpasswordhere",host="yourhosthere",db="yourdatabasehere")
cursor = db.cursor()
cursor.execute("SELECT * from yourmysqltablehere")
data=cursor.fetchall()
for row in data :
    print (row)
db.close()

Now in your CMD - type

c:\Desktop>testconn.py

And thats it... your now fully connected from a python script to mysql...
Enjoy...

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.

    How to connect mysql to python in windows

    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

    How to connect mysql to python in windows

    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

    How to connect mysql to python in windows

    Linux

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

    pip3 install mysql-connector

    How to connect mysql to python in windows

    How do I connect MySQL with 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..

    How do I connect Python to Windows 10 with MySQL?

    Type the command for the package you want to install:.
    To install the mysqlclient package, type the following command: Copy pip install mysqlclient..
    To install the mysql-connector-python package, type the following command: Copy pip install mysql-connector-python..
    To install the pymysql package, type the following command:.

    How do I connect to MySQL on Windows?

    To connect to MySQL Server:.
    Locate the MySQL Command-Line Client. ... .
    Run the client. ... .
    Enter your password. ... .
    Get a list of databases. ... .
    Create a database. ... .
    Select the database you want to use. ... .
    Create a table and insert data. ... .
    Finish working with the MySQL Command-Line Client..

    How do you connect to a database in Python?

    To create a connection between the MySQL database and Python, the connect() method of mysql. connector module is used. We pass the database details like HostName, username, and the password in the method call, and then the method returns the connection object.