How can I connect to MySQL in Python 3 on Windows? - python

I am using ActiveState Python 3 on Windows and wanted to connect to my MySQL database.
I heard that mysqldb was the module to use.
I can't find mysqldb for Python 3.
Is there a repository available where the binaries exist for mysqldb?
How can I connect to MySQL in Python 3 on Windows?

There are currently a few options for using Python 3 with mysql:
https://pypi.python.org/pypi/mysql-connector-python
Officially supported by Oracle
Pure python
A little slow
Not compatible with MySQLdb
https://pypi.python.org/pypi/pymysql
Pure python
Faster than mysql-connector
Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()
https://pypi.python.org/pypi/cymysql
fork of pymysql with optional C speedups
https://pypi.python.org/pypi/mysqlclient
Django's recommended library.
Friendly fork of the original MySQLdb, hopes to merge back some day
The fastest implementation, as it is C based.
The most compatible with MySQLdb, as it is a fork
Debian and Ubuntu use it to provide both python-mysqldb andpython3-mysqldb packages.
benchmarks here: https://github.com/methane/mysql-driver-benchmarks

You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.
This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.
Example:
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
print(r)
cur.close()
conn.close()

if you want to use MySQLdb first you have to install pymysql on your pc by typing in cmd of windows
pip install pymysql
then in python shell, type
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect("localhost" , "root" , "password")
this will establish the connection.

I also tried using pymysql (on my Win7 x64 machine, Python 3.3), without too much luck. I downloaded the .tar.gz, extract, ran "setup.py install", and everything seemed fine. Until I tried connecting to a database, and got "KeyError [56]". An error which I was unable to find documented anywhere.
So I gave up on pymysql, and I settled on the Oracle MySQL connector.
It comes as a setup package, and works out of the box. And it also seems decently documented.

Summary
Mysqlclient is the best alternative(IMHO) because it works flawlessly with
Python 3+, follows expected conventions (unlike mysql
connector), uses the object name mysqldb which enables convenient porting of
existing software and is used by Django for Python 3 builds
Is there a repository available where the binaries exist for mysqldb?
Yes. mysqlclient allows you to use mysqldb functions. Though,
remember this is not a direct port by mysqldb, but a build by mysqlclient
How can I connect to MySQL in Python 3 on Windows?
pip install mysqlclient
Example
#!/Python36/python
#Please change above path to suit your platform. Am running it on Windows
import MySQLdb
db = MySQLdb.connect(user="my-username",passwd="my-password",host="localhost",db="my-databasename")
cursor = db.cursor()
cursor.execute("SELECT * from my-table-name")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
I can't find mysqldb for Python 3.
mysqldb has not been ported yet

Untested, but there are some binaries available at:
Unofficial Windows Binaries

PyMySQL gives MySQLDb like interface as well. You could try in your initialization:
import pymysql
pymysql.install_as_MySQLdb()
Also there is a port of mysql-python on github for python3.
https://github.com/davispuh/MySQL-for-Python-3

Oracle/MySQL provides an official, pure Python DBAPI driver: http://dev.mysql.com/downloads/connector/python/
I have used it with Python 3.3 and found it to work great. Also works with SQLAlchemy.
See also this question: Is it still too early to hop aboard the Python 3 train?

On my mac os maverick i try this:
In Terminal type:
1)mkdir -p ~/bin ~/tmp ~/lib/python3.3 ~/src 2)export TMPDIR=~/tmp
3)wget -O ~/bin/2to3
4)http://hg.python.org/cpython/raw-file/60c831305e73/Tools/scripts/2to3
5)chmod 700 ~/bin/2to3 6)cd ~/src 7)git clone https://github.com/petehunt/PyMySQL.git 8)cd PyMySQL/
9)python3.3 setup.py install --install-lib=$HOME/lib/python3.3
--install-scripts=$HOME/bin
After that, enter in the python3 interpreter and type:
import pymysql.
If there is no error your installation is ok. For verification write a script to connect to mysql with this form:
# a simple script for MySQL connection
import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #Sure, this is
information for my db # close the connection db.close ()*
Give it a name ("con.py" for example) and save it on desktop. In Terminal type "cd desktop" and then
$python con.py
If there is no error, you are connected with MySQL server. Good luck!

CyMySQL
https://github.com/nakagami/CyMySQL
I have installed pip on my windows 7, with python 3.3
just
pip install cymysql
(you don't need cython)
quick and painless

This does not fully answer my original question, but I think it is important to let everyone know what I did and why.
I chose to continue using python 2.7 instead of python 3 because of the prevalence of 2.7 examples and modules on the web in general.
I now use both mysqldb and mysql.connector to connect to MySQL in Python 2.7. Both are great and work well. I think mysql.connector is ultimately better long term however.

I'm using cymysql with python3 on a raspberry pi
I simply installed by:
sudo pip3 install cython
sudo pip3 install cymysql
where cython is not necessary but should make cymysql faster
So far it works like a charm and very similar to MySQLdb

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...

imports
import pymysql
open database connection
db = pymysql.connect("localhost","root","","ornament")
prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM item"
cursor.execute(sql)
Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
item_title = row[1]
comment = row[2]
print ("Title of items are the following = %s,Comments are the following = %s" % \
(item_title, comment))

follow these steps:
import pymysql
install
python -m pip install PyMySQL
python -m pip install PyMySQL[rsa]
python -m pip install PyMySQL[ed25519]
finally try again to install pip install mysql-connector.
these steps solved my error

Related

How to connect python 3.11 with mysql? [duplicate]

I am using ActiveState Python 3 on Windows and wanted to connect to my MySQL database.
I heard that mysqldb was the module to use.
I can't find mysqldb for Python 3.
Is there a repository available where the binaries exist for mysqldb?
How can I connect to MySQL in Python 3 on Windows?
There are currently a few options for using Python 3 with mysql:
https://pypi.python.org/pypi/mysql-connector-python
Officially supported by Oracle
Pure python
A little slow
Not compatible with MySQLdb
https://pypi.python.org/pypi/pymysql
Pure python
Faster than mysql-connector
Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()
https://pypi.python.org/pypi/cymysql
fork of pymysql with optional C speedups
https://pypi.python.org/pypi/mysqlclient
Django's recommended library.
Friendly fork of the original MySQLdb, hopes to merge back some day
The fastest implementation, as it is C based.
The most compatible with MySQLdb, as it is a fork
Debian and Ubuntu use it to provide both python-mysqldb andpython3-mysqldb packages.
benchmarks here: https://github.com/methane/mysql-driver-benchmarks
You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.
This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.
Example:
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
print(r)
cur.close()
conn.close()
if you want to use MySQLdb first you have to install pymysql on your pc by typing in cmd of windows
pip install pymysql
then in python shell, type
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect("localhost" , "root" , "password")
this will establish the connection.
I also tried using pymysql (on my Win7 x64 machine, Python 3.3), without too much luck. I downloaded the .tar.gz, extract, ran "setup.py install", and everything seemed fine. Until I tried connecting to a database, and got "KeyError [56]". An error which I was unable to find documented anywhere.
So I gave up on pymysql, and I settled on the Oracle MySQL connector.
It comes as a setup package, and works out of the box. And it also seems decently documented.
Summary
Mysqlclient is the best alternative(IMHO) because it works flawlessly with
Python 3+, follows expected conventions (unlike mysql
connector), uses the object name mysqldb which enables convenient porting of
existing software and is used by Django for Python 3 builds
Is there a repository available where the binaries exist for mysqldb?
Yes. mysqlclient allows you to use mysqldb functions. Though,
remember this is not a direct port by mysqldb, but a build by mysqlclient
How can I connect to MySQL in Python 3 on Windows?
pip install mysqlclient
Example
#!/Python36/python
#Please change above path to suit your platform. Am running it on Windows
import MySQLdb
db = MySQLdb.connect(user="my-username",passwd="my-password",host="localhost",db="my-databasename")
cursor = db.cursor()
cursor.execute("SELECT * from my-table-name")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
I can't find mysqldb for Python 3.
mysqldb has not been ported yet
Untested, but there are some binaries available at:
Unofficial Windows Binaries
PyMySQL gives MySQLDb like interface as well. You could try in your initialization:
import pymysql
pymysql.install_as_MySQLdb()
Also there is a port of mysql-python on github for python3.
https://github.com/davispuh/MySQL-for-Python-3
Oracle/MySQL provides an official, pure Python DBAPI driver: http://dev.mysql.com/downloads/connector/python/
I have used it with Python 3.3 and found it to work great. Also works with SQLAlchemy.
See also this question: Is it still too early to hop aboard the Python 3 train?
On my mac os maverick i try this:
In Terminal type:
1)mkdir -p ~/bin ~/tmp ~/lib/python3.3 ~/src 2)export TMPDIR=~/tmp
3)wget -O ~/bin/2to3
4)http://hg.python.org/cpython/raw-file/60c831305e73/Tools/scripts/2to3
5)chmod 700 ~/bin/2to3 6)cd ~/src 7)git clone https://github.com/petehunt/PyMySQL.git 8)cd PyMySQL/
9)python3.3 setup.py install --install-lib=$HOME/lib/python3.3
--install-scripts=$HOME/bin
After that, enter in the python3 interpreter and type:
import pymysql.
If there is no error your installation is ok. For verification write a script to connect to mysql with this form:
# a simple script for MySQL connection
import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #Sure, this is
information for my db # close the connection db.close ()*
Give it a name ("con.py" for example) and save it on desktop. In Terminal type "cd desktop" and then
$python con.py
If there is no error, you are connected with MySQL server. Good luck!
CyMySQL
https://github.com/nakagami/CyMySQL
I have installed pip on my windows 7, with python 3.3
just
pip install cymysql
(you don't need cython)
quick and painless
This does not fully answer my original question, but I think it is important to let everyone know what I did and why.
I chose to continue using python 2.7 instead of python 3 because of the prevalence of 2.7 examples and modules on the web in general.
I now use both mysqldb and mysql.connector to connect to MySQL in Python 2.7. Both are great and work well. I think mysql.connector is ultimately better long term however.
I'm using cymysql with python3 on a raspberry pi
I simply installed by:
sudo pip3 install cython
sudo pip3 install cymysql
where cython is not necessary but should make cymysql faster
So far it works like a charm and very similar to MySQLdb
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...
imports
import pymysql
open database connection
db = pymysql.connect("localhost","root","","ornament")
prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM item"
cursor.execute(sql)
Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
item_title = row[1]
comment = row[2]
print ("Title of items are the following = %s,Comments are the following = %s" % \
(item_title, comment))
follow these steps:
import pymysql
install
python -m pip install PyMySQL
python -m pip install PyMySQL[rsa]
python -m pip install PyMySQL[ed25519]
finally try again to install pip install mysql-connector.
these steps solved my error

Trying to connect oracle db from python using cx_oracle and found "ppcx_Oracle.i386 : Python interface to Oracle" using yum list installed

When tried to import cx_Oracle getting error module not found.
Not sure what exactly "ppcx_Oracle.i386" is for and the way to utilize it.
=================================================================== Matched: ppcx_Oracle ====================================================================
ppcx_Oracle.i386 : Python interface to Oracle
is "ppcx_Oracle.i386" anything related to "cx_Oracle" package or i have to look for other options to install cx_Oracle to connect oracle database?
Environment : RHEL 5.4 and python 2.4,2.6
PS: Not having any issues in cx_Oracle since i have already used it in one of my servers.
Thanks in advance!!!
You should have Oracle Instant Client installed on the machine (or Oracle DB). The cx_oracle will use its shared libraries.
Then run this command to install cx_oracle itself:
python -m pip install cx_Oracle --upgrade
More on how to configure it and a code examples can be found here.
https://cx-oracle.readthedocs.io/en/latest/installation.html
ppcx_oracle.i386 looks like some non-official interface, I don't know it and could not give any recommendations about it.

Install ODBC Driver for Mac pyodbc

Working with my Mac trying to connect to SQL Server database. Following Microsoft's instructions. However, when I enter:
brew install --no-sandbox msodbcsql#13.1.9.2 mssql-tools#14.0.6.0
I get the error:
odbcinst: SQLInstallDriverEx failed with Invalid install path.
If I install the ODBC driver (on top of Microsoft's instructions web page) and manually load into python script, is that possible? Here is my python code:
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=server_name;"
"Database=Sandbox;"
"uid=username;pwd=password")
df = pd.read_sql_query('select * from table', cnxn)
In this code, how would I replace the zipped downloaded driver to the connect statement, thus bypassing brew?
In this code, how would I replace the zipped downloaded driver to the connect statement, thus bypassing brew?
The answer to this is no, you cannot do what you're asking -- you're talking across purposes. brew (Homebrew) is the package manager command that's required in this case to install the drivers on your Mac so your Python code can talk to the database. Put simply, you can't run your Python code without the drivers installed so there isn't a way of bypassing it.
There have been more recent changes in the Homebrew tap (https://github.com/Microsoft/homebrew-mssql-release) that you're using to install the drivers, including several issues that are akin to yours which are now closed/resolved. So, try the following:
# This updates the tap and Homebrew to the latest versions
brew update
# Install the drivers; note options like `--no-sandbox` are no longer in Homebrew
brew install msodbcsql#13.1.9.2 mssql-tools#14.0.6.0
This works in my macOS 10.14 environment with Homebrew 2.0.1. Check the file at /usr/local/etc/odbcinst.ini and you should see a reference to [ODBC Driver 13 for SQL Server] defined. You'll need to change your Python code to reflect the name of this new driver, like so:
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"

How to install MySQLdb in Python 3.5? [duplicate]

I am using ActiveState Python 3 on Windows and wanted to connect to my MySQL database.
I heard that mysqldb was the module to use.
I can't find mysqldb for Python 3.
Is there a repository available where the binaries exist for mysqldb?
How can I connect to MySQL in Python 3 on Windows?
There are currently a few options for using Python 3 with mysql:
https://pypi.python.org/pypi/mysql-connector-python
Officially supported by Oracle
Pure python
A little slow
Not compatible with MySQLdb
https://pypi.python.org/pypi/pymysql
Pure python
Faster than mysql-connector
Almost completely compatible with MySQLdb, after calling pymysql.install_as_MySQLdb()
https://pypi.python.org/pypi/cymysql
fork of pymysql with optional C speedups
https://pypi.python.org/pypi/mysqlclient
Django's recommended library.
Friendly fork of the original MySQLdb, hopes to merge back some day
The fastest implementation, as it is C based.
The most compatible with MySQLdb, as it is a fork
Debian and Ubuntu use it to provide both python-mysqldb andpython3-mysqldb packages.
benchmarks here: https://github.com/methane/mysql-driver-benchmarks
You should probably use pymysql - Pure Python MySQL client instead.
It works with Python 3.x, and doesn't have any dependencies.
This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.
Example:
import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
for r in cur:
print(r)
cur.close()
conn.close()
if you want to use MySQLdb first you have to install pymysql on your pc by typing in cmd of windows
pip install pymysql
then in python shell, type
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect("localhost" , "root" , "password")
this will establish the connection.
I also tried using pymysql (on my Win7 x64 machine, Python 3.3), without too much luck. I downloaded the .tar.gz, extract, ran "setup.py install", and everything seemed fine. Until I tried connecting to a database, and got "KeyError [56]". An error which I was unable to find documented anywhere.
So I gave up on pymysql, and I settled on the Oracle MySQL connector.
It comes as a setup package, and works out of the box. And it also seems decently documented.
Summary
Mysqlclient is the best alternative(IMHO) because it works flawlessly with
Python 3+, follows expected conventions (unlike mysql
connector), uses the object name mysqldb which enables convenient porting of
existing software and is used by Django for Python 3 builds
Is there a repository available where the binaries exist for mysqldb?
Yes. mysqlclient allows you to use mysqldb functions. Though,
remember this is not a direct port by mysqldb, but a build by mysqlclient
How can I connect to MySQL in Python 3 on Windows?
pip install mysqlclient
Example
#!/Python36/python
#Please change above path to suit your platform. Am running it on Windows
import MySQLdb
db = MySQLdb.connect(user="my-username",passwd="my-password",host="localhost",db="my-databasename")
cursor = db.cursor()
cursor.execute("SELECT * from my-table-name")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
I can't find mysqldb for Python 3.
mysqldb has not been ported yet
Untested, but there are some binaries available at:
Unofficial Windows Binaries
PyMySQL gives MySQLDb like interface as well. You could try in your initialization:
import pymysql
pymysql.install_as_MySQLdb()
Also there is a port of mysql-python on github for python3.
https://github.com/davispuh/MySQL-for-Python-3
Oracle/MySQL provides an official, pure Python DBAPI driver: http://dev.mysql.com/downloads/connector/python/
I have used it with Python 3.3 and found it to work great. Also works with SQLAlchemy.
See also this question: Is it still too early to hop aboard the Python 3 train?
On my mac os maverick i try this:
In Terminal type:
1)mkdir -p ~/bin ~/tmp ~/lib/python3.3 ~/src 2)export TMPDIR=~/tmp
3)wget -O ~/bin/2to3
4)http://hg.python.org/cpython/raw-file/60c831305e73/Tools/scripts/2to3
5)chmod 700 ~/bin/2to3 6)cd ~/src 7)git clone https://github.com/petehunt/PyMySQL.git 8)cd PyMySQL/
9)python3.3 setup.py install --install-lib=$HOME/lib/python3.3
--install-scripts=$HOME/bin
After that, enter in the python3 interpreter and type:
import pymysql.
If there is no error your installation is ok. For verification write a script to connect to mysql with this form:
# a simple script for MySQL connection
import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #Sure, this is
information for my db # close the connection db.close ()*
Give it a name ("con.py" for example) and save it on desktop. In Terminal type "cd desktop" and then
$python con.py
If there is no error, you are connected with MySQL server. Good luck!
CyMySQL
https://github.com/nakagami/CyMySQL
I have installed pip on my windows 7, with python 3.3
just
pip install cymysql
(you don't need cython)
quick and painless
This does not fully answer my original question, but I think it is important to let everyone know what I did and why.
I chose to continue using python 2.7 instead of python 3 because of the prevalence of 2.7 examples and modules on the web in general.
I now use both mysqldb and mysql.connector to connect to MySQL in Python 2.7. Both are great and work well. I think mysql.connector is ultimately better long term however.
I'm using cymysql with python3 on a raspberry pi
I simply installed by:
sudo pip3 install cython
sudo pip3 install cymysql
where cython is not necessary but should make cymysql faster
So far it works like a charm and very similar to MySQLdb
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...
imports
import pymysql
open database connection
db = pymysql.connect("localhost","root","","ornament")
prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM item"
cursor.execute(sql)
Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
item_title = row[1]
comment = row[2]
print ("Title of items are the following = %s,Comments are the following = %s" % \
(item_title, comment))
follow these steps:
import pymysql
install
python -m pip install PyMySQL
python -m pip install PyMySQL[rsa]
python -m pip install PyMySQL[ed25519]
finally try again to install pip install mysql-connector.
these steps solved my error

Python --> MSSQL

I'm at a loss here. I'm very new to python and odbc in general. However I need to find a way to have a python script in the backend of a web gui for an internal tool for my company, communicate with MSSQL server from: 1. my local mac os machine, 2.a linux server that hosts all of our internal tools.
I have tried installing freetds and configuring it as show:
http://blog.nguyenvq.com/blog/2013/04/06/guide-to-accessing-ms-sql-server-and-mysql-server-on-mac-os-x/
but I'm getting issues with driver not being located where the tutorial specifies after installing freetds.
I'm not quite sure where to even start with troubleshooting.
Can anyone give me a place to start with possibly making this work? It seemed to me python was going to be my best bet with this, but if there are other, better options -- I'm certainly open to them.
Thanks so much, I apologize for how open ended this is, but I'm not sure where to start. Every resource I'm finding is either miles over my head, or is too ambiguous to follow.
I'm not sure how to handle the OSx part of this, but you might be able to do something similar.
I have successfully used pyodbc (https://code.google.com/p/pyodbc/) to connect to SQL Server 2008.
Steps for RHEL/CentOS 6 (taken from http://funwithlinux.net/2013/07/connect-to-sql-server-with-python/)
1)Enable the EPEL
2)Install the Required Packages
yum install gcc gcc-c++ python-devel freetds unixODBC unixODBC-devel
3)Download latest stable version of pyodbc
4)Unzip, then build and install pyodbc with the following command:
python setup.py build install
5)edit /etc/odbcinst.ini to include the following:
[FreeTDS]
Driver = /usr/lib64/libtdsodbc.so.0
UsageCount = 1
6)Test with the following example (modified to your needs, obviously):
import pyodbc
cnxn = pyodbc.connect('DRIVER={FreeTDS};SERVER=dev-sql02;PORT=1433;UID=EXAMPLE\\myusero;PWD=xxx;DATABASE=fx_staging;UseNTLMv2=yes;TDS_Version=8.0;Trusted_Domain=EXAMPLE;')
cursor = cnxn.cursor()
cursor.execute("select state, zip from addresses")
row = cursor.fetchone()
if row:
print row

Categories

Resources