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 //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..
//www.apachefriends.org/download.html
2. Download Python
//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
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 win32This is to check the Python version
Now that both have been confirmed type the following into the CMD...
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.
Now in your CMD - type
c:\Desktop>testconn.pyAnd thats it... your now fully connected from a python script to mysql...
Enjoy...
View Discussion
Improve Article
Save Article
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:
If Python is already installed, it will generate a message with the Python version
available.
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:
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-pythonLinux
mysql-connector method can be installed on Linux with the use of following command:
pip3 install mysql-connector