BCNPythonMeetup_20140522_Python&MySQL
-
Upload
miquelcm -
Category
Technology
-
view
1.465 -
download
1
Transcript of BCNPythonMeetup_20140522_Python&MySQL
PYTHON AND MYSQLMiquel Camprodon i Masnou
Python Meetup Group
Barcelona, May 22th 2014
ABOUT THIS TALK
We will see how to interact
from Python
to a relational DB: MySQL
Examples on a Ubuntu 12.04
WHO AM I?
Miquel Camprodon i Masnou
Mathematician and computer engineer
Working as a data analyst and programmer
Working at: Kernel Analytics
Barcelona
Data analysis
WHY MYSQL?
Not the most glamorous database
Still the most used open source database
Easy to install, configure, and interact with
A market standard
Available in many hostings and cloud environments
MYSQL CHARACTERISTICS
Open source RDBMS owned by Oracle
Two storage engines: MyISAM, InnoDB
< 5.5, MyISAM the default storage Not support for ACID transactions Not support Referential Integrity Not support for Crash recovery Better performance on SELECTS
>= 5.5, InnoDB the default storage ACID transactions, RI, Crash recovery Lower performance on SELECT statements
INSTALLING MYSQL
Examples on a Ubuntu 12.04
$ sudo apt-get install mysql-server
libmysqlclient-dev python-dev
(Asks for a password for the MySQL root account)
CREATE A NEW DATABASE
$ mysql –u root -p
mysql> SET storage_engine=INNODB;
mysql> CREATE DATABASE testdb;
mysql> GRANT ALL PRIVILEGES ON testdb.*
TO ‘testuser’@’localhost’ IDENTIFIED BY
‘pass123’;
mysql> quit;
PYTHON WITH MYSQL MODULES
_mysql module
Implements the MySQL C API directly.
Not compatible with the Python DB interface.
MySQLdb module
Python wrapper around _mysql
Compatible with the Python DB API
Preferred way of working with MySQL
CONFIGURING THE PYTHON ENVIRONMENT
Create Virtualenv
$ virtualenv vepymysql
Activate
$ source vepymysql/bin/activate
Install Python MySQL interface
(vepymysql)$ pip install MySQL-python
EXAMPLES
From MySQL Python Tutorial
Connect to the database
Create and populate a table
Retrieve data
Retrieve data one by one row
Access results by column name
Show column headers
Prepared statements
EXAMPLES
Connect to the database
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb');
cur = con.cursor()
cur.execute("SELECT VERSION()")
ver = cur.fetchone()
con.close()
EXAMPLES
Create and populate a table
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb');
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Writers")
cur.execute("CREATE TABLE Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))")
cur.execute("INSERT INTO Writers(Name) VALUES('Jack L. ')")
cur.execute("INSERT INTO Writers(Name) VALUES('Will S. ')")
EXAMPLES
Retrieve data
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Writers")
rows = cur.fetchall()
EXAMPLES
Retrieve data one by one row
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:cur = con.cursor()
cur.execute("SELECT * FROM Writers")
for i in range(cur.rowcount):
row = cur.fetchone()
print row[0], row[1]
EXAMPLES
Access results by column name
import MySQLdb as mdbcon = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM Writers LIMIT 4")
rows = cur.fetchall()
for row in rows:print row["Id"], row["Name"]
EXAMPLES
Show column headers
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Writers LIMIT 5")
rows = cur.fetchall()
desc = cur.description
print "%s %3s" % (desc[0][0], desc[1][0])
for row in rows:
print "%2s %3s" % row
EXAMPLES
Prepared statementsimport MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:
cur = con.cursor()
cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s", ("Thomas Mann", "2"))
print "Number of rows updated:", cur.rowcount
cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s",
(("Fiodor Dostovievsky", "3"), ("Dante Alighieri", "4")))
print "Number of rows updated:", cur.rowcount
TRANSACTION MANAGEMENT
MyISAM Transactions not supported
COMMIT and ROLLBACK do nothing
InnoDB Transaction support
A transaction needs to end with COMMIT or ROLLBACK
Context manager with performs automatically the transaction management.
EXAMPLES
Transaction management
import MySQLdb as mdb
try: con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Writers")
cur.execute("CREATE TABLE Writers(Id INT PRIMARY KEY, Name VARCHAR(25))")
con.commit()
except mdb.Error, e:
if con:
con.rollback()
print "Error %d: %s" % (e.args[0],e.args[1])
finally:
if con:
con.close()
EXAMPLES
With context manager
import MySQLdb as mdb
con = mdb.connect('localhost', 'testuser', 'pass123', 'testdb')
with con:
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Writers")
cur.execute("CREATE TABLE Writers(Id INT PRIMARY KEY AUTO_INCREMENT, \
Name VARCHAR(25))")
WHEN TO USE MYSQL?
MySQL easy to learn and work with
If you interact with different customers
If you are looking for a simple hosting
If you are continuing previous work from others
-> Probably you will work with MySQL
If you are starting your project from scratch
-> Think in various options, including MySQL
MORE INFO
InnoDB vs MyISAM
InnoDB as the Default MySQL Storage Engine
MySQLdb User’s Guide
MySQL Python Tutorial