How do I connect to a MySQL database using a python program?
|
Connecting to MYSQL with Python in 3 steps1 - Setting You must install a MySQL driver before doing anything. Unlike PHP, only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install. For Windows user, you can get a exe of MySQLdb. For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb in command line to download.) For Mac, you can install MySQLdb using Macport. 2 - Usage After installing, reboot. This is not mandatory, but will prevent me from answering 3 or 4 others questions in this post if something goes wrong. So please reboot. Then it is just like using another package :
Of course, there are thousand of possibilities and options, this is a very basic example. You will have to look at the documentation. A good starting point. 3 - More advanced usage Once you know how it works, you may want to use an ORM to avoid writting SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy. I strongly advice you to use it: your life is going to be much easier. I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, where using big tools like SQLAlchemy or Django is overkill :
This example works out of the box. Nothing other than having peewee ( |
|||||||||||||||||||||
|
Here's one way to do it.
From here. |
|||||||||||||||||||||
|
Oracle (MySQL) now supports a pure Python connector. That means no binaries to install: it's just a Python library. It's called "Connector/Python". |
|||||||||||||||||||||
|
If you do not need MySQLdb, but would accept any library, I would very, very much recommend MySQL Connector/Python from MySQL: http://dev.mysql.com/downloads/connector/python/. It is one package (around 110k), pure Python, so it is system independent, and dead simple to install. You just download, double-click, confirm license agreement and go. There is no need for Xcode, MacPorts, compiling, restarting … Then you connect like:
|
|||||||||||||||||||||
|
As a db driver, there is also oursql. Some of the reasons listed on that link, which say why oursql is better:
So how to connect to mysql with oursql?Very similar to mysqldb:
The tutorial in the documentation is pretty decent. And of course for ORM SQLAlchemy is a good choice, as already mentioned in the other answers. |
|||||||||
|
Try using MySQLdb There is a how to page here: http://www.kitebird.com/articles/pydbapi.html From the page:
|
|||||
|
MySQLdb is the straightforward way. You get to execute SQL queries over a connection. Period. My preferred way, which is also pythonic, is to use the mighty SQLAlchemy instead. Here is a query related tutorial, and here is a tutorial on ORM capabilities of SQLALchemy. |
|||||
|
For python 3.3 CyMySQL https://github.com/nakagami/CyMySQL I have pip installed on my windows 7, just pip install cymysql (you don't need cython) quick and painless |
|||||||||
|
protected by Mark Feb 27 '14 at 13:40
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?