Python Forum

Full Version: mysql db connection using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
There no need to use package from distro,use as recommend PyMySQL(Pure Python MySQL Driver).
I use pyenv,so my python and pip is set to 3.6
Normal for Python 3 without pyenv is to use pip3 pip3 install PyMySQL.
# Test python and pip
mint@mint ~/mp3_env $ pip -V
pip 9.0.1 from /home/mint/.pyenv/versions/3.6.4/lib/python3.6/site-packages (python 3.6)
mint@mint ~/mp3_env $ python -V
Python 3.6.4

# Install
mint@mint ~/mp3_env $ pip install PyMySQL
Collecting PyMySQL
  Using cached https://files.pythonhosted.org/packages/e5/07/c0f249aa0b7b0517b5843eeab689b9ccc6a6bb0536fc9d95e65901e6f2ac/PyMySQL-0.8.0-py2.py3-none-any.whl
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.8.0
Test that it work.
Python 3.6.4 (default, Mar 15 2018, 15:35:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql.cursors
>>> # no error
Then is just to follow Example.
I'm using python2.7
(Apr-21-2018, 05:45 PM)sunstar20 Wrote: [ -> ]I'm using python2.7
Look at Requirements, so it work fine for 2.7.
If want to use Python 3 as you should,look at Linux Python 3 environment.
I suggest you install MySQL Connector Python because it is official Oracle driver for MySQL for working with Python and it works with both Python 3 and Python 2.

You can install connector using pip or you can download DEB Package for MySQL Connector Python and install it.
Use this link to install and verify the installation Install MySQL connector Python
To verify installation you can import mysql.connector and check if the program gives an error if not your installation is complete.

follow this article to connect to MySQL from python and execute database queries.Python MySQL connection
Pages: 1 2