Main Content
Connecting Python to sqlite and MySQL databases Archive - Originally posted on "The Horse's Mouth" - 2010-04-28 20:15:46 - Graham Ellis
For developing a Python application, and for light use, the SQLite database may be a good choice ... but then you may want to be able to expand the system to use a central database with a more classic server such as MySQL at a later date.
Using Polymorphism in Python, and with a careful choice of which database connector you use, you should be able to write code that is easily ported.
I have written a simple demonstration that connects to a database (whether it's a local file via SQlite or a server via MySQL), creates two new tables, populates the tables with data, then does a left join.
The connection to the databases differs. Here it is for SQlite:
import sqlite3
conn = sqlite3.connect('/tmp/example')
and here it is for MySQL:
import mysql.connector
conn = mysql.connector.Connect(host='192.168.200.115',\
 ' user='demo',password='abc123',database='test')
But thereafter, the code is the same - so it will work equally on a local SQLite database, and on a database on my server at 192.168.200.115. Source code for the sqlite example - [here] and for the MySQL example [here] .
The sqlite drivers are now INCLUDED with Python Distributions. MySQL connector/Python is available at places including [here] at Launchpad. Stated o be in early condition, but some people swear by it already!
Some other articles
Y115 - Additional Python Facilities Some gems from Intermediate Python Command line parameter handling in Python via the argparse module Json load from URL, recursive display, Python 3.4 Running an operating system command from your Python program - the new way with the subprocess module Json is the new marshall, pickle and cPickle / Python Python - an interesting application Handling JSON in Python (and a csv, marshall and pickle comparison) JSON from Python - first principles, easy example Teaching dilemma - old tricks and techniques, or recent enhancements? A demonstration of how many Python facilities work together Python regular expressions - repeating, splitting, lookahead and lookbehind Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Running operating system commands from your Python program Python decorators - your own, staticmethod and classmethod Model - View - Controller demo, Sqlite - Python 3 - Qt4 This article Regular Expressions in Python Python - what is going on around me? Python - how it saves on compile time Serialization - storing and reloading objects Testing code in Python - doctest, unittest and others Python Regular Expressions A series of tyre damages Ignore case in Regular Expression Regular expressions made easy - building from components Turning objects into something you can store - Pickling (Python) Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP Sending an email from Python Python - listing out the contents of all variables Python 3000 - the next generation Keeping your regular expressions simple Python to MySQL Splitting the difference What and why for the epoch Examples - Gadfly, NI Number, and Tcl to C interface The elegance of Python Y113 - Python and SQL databases example of SQLite using a local database file through SQLalchemy SQLAlchemy - first examples with a Python Object Relationship Mapping system Graphing presentations in Python - huge data, numpy and matplotlib Accessing a MySQL database from Python with mysql.connector Cacheing class for Python - using a local SQLite database as a key/value store SQL databases from Python - an SQLite example Steering our Python courses towards wxPython, SQLite and Django A framework with python - Django - first steps Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Model - View - Controller demo, Sqlite - Python 3 - Qt4 This article S200 - SQLite SQL databases from Python - an SQLite example Which database should I use? MySQL v SQLite Ruby / SQLite3 example program, showing JOIN v LEFT JOIN Files or Databases? MySQL, SQLite, or Oracle? Steering our Python courses towards wxPython, SQLite and Django Perl - database access - DBD, DBI and DBIx modules Factory methods and SqLite in use in a Python teaching example Model - View - Controller demo, Sqlite - Python 3 - Qt4 This article PyQt (Python and Qt) and wxPython - GUI comparison The future of MySQL S156 - Interfacing Applications to MySQL Databases Accessing a MySQL database from Python with mysql.connector MySQL, MySQLi, PDO or something else - how best to talk to databases from PHP Needle in a haystack - finding the web server overload Perl - database access - DBD, DBI and DBIx modules How to display information from a database within a web page Joining a MySQL table from within a Python program This article Checking the database connection manually Mysqldump fails as a cron job - a work around Hiding a MySQL database behind a web page Uploading to a MySQL database through PHP - examples and common questions Downloading data for use in Excel (from PHP / MySQL) Easy selection of multiple SQL conditions from PHP Using a MySQL database to control mod_rewrite via PHP Viewing images held in a MySQL database via PHP Python to MySQL Using a MySQL database from Perl mysql_connect or mysql_pconnect in PHP?