About this ebook
Related to MySQL for Python
Related ebooks
Python Essentials Rating: 5 out of 5 stars5/5Functional Python Programming Rating: 0 out of 5 stars0 ratingsThe SQL Workshop: Learn to create, manipulate and secure data and manage relational databases with SQL Rating: 0 out of 5 stars0 ratingsMastering Objectoriented Python Rating: 5 out of 5 stars5/5PostgreSQL 9.0 High Performance Rating: 4 out of 5 stars4/5Creating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5Web Scraping with Python Rating: 4 out of 5 stars4/5Python Web Scraping - Second Edition Rating: 5 out of 5 stars5/5Learning Flask Framework Rating: 4 out of 5 stars4/5Python Data Analysis Rating: 4 out of 5 stars4/5Python: Programming for Intermediates: Learn the Fundamentals of Python in 7 Days Rating: 4 out of 5 stars4/5Learning R Programming Rating: 5 out of 5 stars5/5Tiny Python Projects: Learn coding and testing with puzzles and games Rating: 4 out of 5 stars4/5Modern Python Cookbook Rating: 5 out of 5 stars5/5Python Tools for Visual Studio Rating: 0 out of 5 stars0 ratingsFlask By Example Rating: 0 out of 5 stars0 ratingsAdvance Core Python Programming: Begin your Journey to Master the World of Python (English Edition) Rating: 4 out of 5 stars4/5Building RESTful Python Web Services Rating: 4 out of 5 stars4/5NumPy Cookbook Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Mastering Python Rating: 0 out of 5 stars0 ratingsMastering Python Regular Expressions Rating: 5 out of 5 stars5/5Getting Started with Beautiful Soup Rating: 3 out of 5 stars3/5Practical SQL Rating: 4 out of 5 stars4/5Learn SQL with MySQL: Retrieve and Manipulate Data Using SQL Commands with Ease Rating: 0 out of 5 stars0 ratingsSQL in 30 Pages Rating: 4 out of 5 stars4/5Python GUI Programming Cookbook Rating: 5 out of 5 stars5/5
Information Technology For You
Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5The Design Inference Rating: 5 out of 5 stars5/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsCreating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Supercommunicator: Explaining the Complicated So Anyone Can Understand Rating: 3 out of 5 stars3/5Learning Microsoft Endpoint Manager: Unified Endpoint Management with Intune and the Enterprise Mobility + Security Suite Rating: 0 out of 5 stars0 ratingsCreating your MySQL Database: Practical Design Tips and Techniques Rating: 3 out of 5 stars3/5CompTia Security 701: Fundamentals of Security Rating: 0 out of 5 stars0 ratingsSummary of Super-Intelligence From Nick Bostrom Rating: 4 out of 5 stars4/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Personal Knowledge Graphs: Connected thinking to boost productivity, creativity and discovery Rating: 5 out of 5 stars5/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Cyber Security Consultants Playbook Rating: 0 out of 5 stars0 ratingsInkscape Beginner’s Guide Rating: 5 out of 5 stars5/5Excel VBA: A Step-By-Step Tutorial For Beginners To Learn Excel VBA Programming From Scratch: 1 Rating: 4 out of 5 stars4/5A Mind at Play: How Claude Shannon Invented the Information Age Rating: 4 out of 5 stars4/5Random Tech Rating: 0 out of 5 stars0 ratingsCompTIA A+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Core 1 Exam 220-1101 Rating: 0 out of 5 stars0 ratingsCompTIA ITF+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam FC0-U61 Rating: 5 out of 5 stars5/5AWS Certified Cloud Practitioner - Practice Paper 1: AWS Certified Cloud Practitioner, #1 Rating: 5 out of 5 stars5/5Bitcoin for Nonmathematicians:: Exploring the Foundations of Crypto Payments Rating: 0 out of 5 stars0 ratingsAI Unplugged: Your Everyday Guide to Understanding Artificial Intelligence Rating: 0 out of 5 stars0 ratingsHow to Find a Wolf in Siberia (or, How to Troubleshoot Almost Anything) Rating: 0 out of 5 stars0 ratingsLinux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5AWS Certified Cloud Practitioner: Study Guide with Practice Questions and Labs Rating: 5 out of 5 stars5/5CODING INTERVIEW: Advanced Methods to Learn and Excel in Coding Interview Rating: 0 out of 5 stars0 ratingsChatGPT: The Future of Intelligent Conversation Rating: 4 out of 5 stars4/5
Reviews for MySQL for Python
1 rating0 reviews
Book preview
MySQL for Python - Albert Lukaszewski
Table of Contents
MySQL for Python
Credits
About the Author
About the Reviewers
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. Getting Up and Running with MySQL for Python
Getting MySQL for Python
Using a package manager (only on Linux)
Using RPMs and yum
Using RPMs and urpm
Using apt tools on Debian-like systems
Using an installer for Windows
Using an egg file
Installing egg handling software
Using a package manager (Linux)
Without a package manager (Mac, Linux)
On Microsoft Windows
Installing MySQL for Python from an egg file
Using a tarball (tar.gz file)
Importing MySQL for Python
Accessing online help when you need it
MySQLdb
_mysql
Connecting with a database
Creating a connection object
Creating a cursor object
Interacting with the database
Closing the connection
Multiple database connections
Summary
2. Simple Querying
A brief introduction to CRUD
Forming a query in MySQL
SELECT
* (asterisk)
FROM
staff
; (semicolon)
Other helpful quantifiers
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT
INTO OUTFILE
Passing a query to MySQL
A simple SELECT statement
Modifying the results
Using user-defined variables
Determining characteristics of a database and its tables
Determining what tables exist
Assigning each table a number
Offering the options to the user
Allowing the user to detail a search query
Changing queries dynamically
Pattern matching in MySQL queries
Putting it into practice
Project: A command-line search utility
Preparing a database for searching
Planning your work, then working your plan
Develop a well-abstracted search functionality
Specifying the search term from the command-line
Implementing and incorporating the other functions: -t, -f, and -o
Including an option for an output file
Room to grow
Summary
3. Simple Insertion
Forming a MySQL insertion statement
INSERT
INTO
Table name
Column names
VALUES
; (semicolon)
Helpful ways to nuance an INSERT statement
INSERT...SELECT...
INSERT DELAYED…
INSERT...ON DUPLICATE KEY UPDATE...
Passing an insertion through MySQL for Python
Setting up the preliminaries
A simple INSERT statement
More complex INSERT commands
Using user-defined variables
Using metadata
Querying the database for its structure
Retrieving the table structure
Changing insertion values dynamically
Validating the value of name
Validating the value of price
Querying the user for a correction
Passing fish and price for validation
Essentials: close and commit
In need of some closure
What happened to commit?
Why are these essentials non-essential?
Project: A command-line insertion utility
The necessary modules
The main() thing
Coding the flag system
Testing the values passed by the user
Try to establish a database connection
Showing the tables
Showing the table structure, if desired
Accepting user input for the INSERT statement
Building the INSERT statement from the user input and executing it
Committing changes and closing the connection
Coding the other functions
valid_digit() and valid_string()
valid_table()
query()
Calling main()
Room to grow
Summary
4. Exception Handling
Why errors and warnings are good for you
Errors versus warnings: There's a big difference
The two main errors in MySQLdb
DatabaseError
InterfaceError
Warnings in MySQL for Python
Handling exceptions passed from MySQL
Python exception-handling
Catching an exception from MySQLdb
Raising an error or a warning
Making exceptions less intimidating
Catching different types of exceptions
Types of errors
DataError
IntegrityError
InternalError
NotSupportedError
OperationalError
ProgrammingError
Customizing for catching
Catching one type of exception
Catching different exceptions
Combined catching of exceptions
Raising different exceptions
Creating a feedback loop
Project: Bad apples
The preamble
Making the connection
Sending error messages
The statement class
The __init__ method
Storing the statement type
Forming the statement
Execute the MySQL statement
Handling any fallout
The main() thing
Try, try again
If all else fails
Room to grow
Summary
5. Results Record-by-Record
The problem
Why?
Computing resources
Local resources
Web applications
Network latency
Server-client communications
Apparent responsiveness
Pareto's Principle
How?
The fetchone() method
The fetchmany() method
Iteration: What is it?
Generating loops
while...if loops
The for loop
Iterators
Illustrative iteration
Iteration and MySQL for Python
Generators
Using fetchone() in a generator
Using fetchmany() in a generator
Project: A movie database
Getting Sakila
Creating the Sakila database
The structure of Sakila
Planning it out
The SQL statements to be used
Returning the films of an actor
Returning the actors of a film
Accepting user data
A MySQL query with class
The __init__ method: The consciousness of the class
Setting the query's type
Creating the cursor
Forming the query
Executing the query
Formatting the results
Formatting a sample
Formatting a larger set of results
The main() thing
Calling main()
Running it
Room to grow
Summary
6. Inserting Multiple Entries
The problem
Why not a MySQL script?
Lack of automation
Debugging the process
Inefficient I/O
Why not iterate?
A test sample: Generating primes
Comparing execution speeds
Introducing the executemany() method
executemany(): Basic syntax
executemany(): Multiple INSERT statements
executemany(): multiple SELECT statements
executemany(): Behind the scenes
MySQL server has gone away
Command-line option configuration
Using a configuration file
More than 16 MB is often unnecessary
Project: Converting a CSV file to a MySQL table
The preamble
The options
Defining the connection
Creating convert
The main() function
Calling main()
Room to grow
Summary
7. Creating and Dropping
Creating databases
Test first, create second
CREATE specifications
Specifying the default character set
Specifying the collation for a database
Declaring collation
Finding available character sets and collations
Removing or deleting databases
Avoiding errors
Preventing (illegal) access after a DROP
Creating tables
Covering our bases
Avoiding errors
Creating temporary tables
Dropping tables
Playing it safe
Avoiding errors
Removing user privileges
Doing it in Python
Creating databases with MySQLdb
Testing the output
Dynamically configuring the CREATE statement
Dropping databases with MySQLdb
Creating tables in Python
Verifying the creation of a table
Another way to verify table creation
Dropping tables with MySQLdb
Project: Web-based administration of MySQL
CGI vs PHP: What is the difference?
Basic CGI
Using PHP as a substitute for CGI
CGI versus PHP: When to use which?
Some general considerations for this program
Program flow
The basic menu
Authorization details
Three operational sections of the dialogue
The variables
Planning the functions
Code of each function
Connecting without a database
Connecting with a database
Database action
Table action
Query action
execute()
The HTML output
Basic definition
The message attribute
Defining header()
Defining footer()
Defining body()
Defining page()
Getting the data
Using CGI
Using PHP
Defining main()
Room to grow
Summary
8. Creating Users and Granting Access
A word on security
Creating users in MySQL
Forcing the use of a password
Restricting the client's host
Creating users from Python
Removing users in MySQL
DROPping users in Python
GRANT access in MySQL
Important dynamics of GRANTing access
The GRANT statement in MySQL
Using REQUIREments of access
Using a WITH clause
Granting access in Python
Removing privileges in MySQL
Basic syntax
After using REVOKE, the user still has access!?
Using REVOKE in Python
Project: Web-based user administration
New options in the code
Adding the functions: CREATE and DROP
Adding CREATE and DROP to main()
Adding the functions: GRANT and REVOKE
Adding GRANT and REVOKE to main()
Test the program
New options on the page
Room to grow
Summary
9. Date and Time Values
Date and time data types in MySQL
DATETIME
Output format
Input formats
Input range
Using DATETIME in a CREATE statement
DATE
Output and Input formats
Input range
TIMESTAMP
Input of values
Range
Defaults, initialization, and updating
YEAR
Two-digit YEAR values
Four-digit YEAR values
Valid input
TIME
Format
Invalid values
Date and time types in Python
Date and time functions
NOW()
CURDATE()
CURTIME()
DATE()
DATE_SUB() and DATE_ADD()
DATEDIFF()
DATE_FORMAT()
EXTRACT()
TIME()
Project: Logging user activity
The log framework
The logger() function
Creating the database
Using the database
Creating the table
Forming the INSERT statement
Ensure logging occurs
Room to grow
Summary
10. Aggregate Functions and Clauses
Calculations in MySQL
COUNT()
SUM()
MAX()
MIN()
AVG()
The different kinds of average
Mean
Median
Mode
Trimming results
DISTINCT
GROUP_CONCAT()
Specifying the delimiter
Customizing the maximum length
Using GROUP_CONCAT() with DISTINCT
Server-side sorting in MySQL
GROUP BY
ORDER BY
Using a universal quantifier
Sorting alphabetically or from low-to-high
Reversing the alphabet or sorting high-to-low
Sorting with multiple keys
Putting it in Python
Project: Incorporating aggregate functions
Adding to qaction()
New variables
New statement formation
Revising main()
Setting up the options
Changing the HTML form
Summary
11. SELECT Alternatives
HAVING clause
WHERE versus HAVING: Syntax
WHERE versus HAVING: Aggregate functions
WHERE versus HAVING: Application
Subqueries
Unions
Joins
LEFT and RIGHT joins
OUTER joins
INNER joins
NATURAL joins
CROSS joins
Doing it in Python
Subqueries
Unions
Joins
Project: Implement HAVING
Revising the Python backend
Revising qaction()
Revising main()
Revising the options
Revising the HTML interface
Room to grow
Summary
12. String Functions
Preparing results before their return
CONCAT() function
SUBSTRING() or MID()
TRIM()
Basic syntax
Options
Alternatives
REPLACE()
INSERT()
REGEXP
Accessing and using index data
LENGTH()
INSTR() or LOCATE()
INSTR()
LOCATE()
Nuancing data
ROUND()
FORMAT()
UPPER()
LOWER()
Project: Creating your own functions
Hello()
Capitalise()
DELIMITER
The function definition
Calling the function
Defining the function in Python
Defining the function as a Python value
Sourcing the MySQL function as a Python module
Sourcing the function as MySQL code
Room to grow
Summary
13. Showing MySQL Metadata
MySQL's system environment
ENGINE
The most popular engines
Transactions
Specifying the engine
ENGINE status
SHOW ENGINES
Profiling
SHOW PROFILE
SHOW PROFILES
SHOW system variables
Accessing database metadata
DATABASES
Using the USE command
Accessing metadata about tables
SHOW TABLES
SHOW TABLE STATUS
Showing columns from a table
FUNCTION STATUS
CREATE (DATABASE/FUNCTION/PROCEDURE/TABLE/VIEW)
Accessing user metadata
SHOW GRANTS
PRIVILEGES
Project: Building a database class
Writing the class
Defining fetchquery() and some core methods
Retrieving table status and structure
Retrieving the CREATE statements
Define main()—part 1
Writing resproc()
Define main()—part 2
The preamble
Modules and variables
Login and USE
Closing out the program
Room to grow
Summary
14. Disaster Recovery
Every database needs a backup plan
Offline backups
Live backups
Choosing a backup method
Copying the table files
Locking and flushing
LOCK TABLES
FLUSH
Unlocking the tables
Restoring the data
Delimited backups within MySQL
Using SELECT INTO OUTFILE to export data
Using LOAD DATA INFILE to import data
Archiving from the command line
mysqldump
Viewing the backup file
Other options
Restoring the data
mysqlhotcopy
Backing up a database with Python
Summary
Index
MySQL for Python
MySQL for Python
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2010
Production Reference: 1160910
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849510-18-9
www.packtpub.com
Cover Image by Vinayak Chittar (<[email protected]>)
Credits
Author
Albert Lukaszewski
Reviewers
Swaroop C H
Andy Dustman
Geert JM Vanderkelen
Acquisition Editor
Steven Wilding
Development Editor
Wilson D'souza
Technical Editors
Prashant Macha
Charumati Shankaran
Indexer
Hemangini Bari
Editorial Team Leader
Aanchal Kumar
Project Team Leader
Priya Mukherji
Project Coordinator
Prasad Rai
Proofreader
Aaron Nash
Production Coordinator
Shantanu Zagade
Cover Work
Shantanu Zagade
About the Author
Albert Lukaszewski is principal consultant for Lukaszewski Consulting Services in southeast Scotland. He has programmed computers for 30 years. Much of his experience has related to text processing, database systems, and Natural Language processing (NLP). Currently he consults on database applications for companies in the financial and publishing industries.
In addition to MySQL for Python, Albert Lukaszewski has also written About Python
, a column for the New York Times subsidiary, About.com.
Many people had a hand in this work beyond my typing at the keyboard. Some contributed by their effort and others by their sacrifice. Thanks to the team at Packt for their consistent understanding and support. I am particularly thankful to Steven Wilding for help and support above and beyond the call of duty.
Thanks also to Andy Dustman, Geert Vanderkelen, and Swaroop for their helpful review of this book and for making so many significant and helpful recommendations. This book would be much the poorer were it not for their suggestions.
To Richard Goodrich, who first introduced me to Python, thank you for liberating me from bondage to that other P-language. Funny what a little problem can lead to.
My heartfelt thanks and appreciation go to my wife, Michelle, and my sons, Cyrus and Jacob. The latter was born during the writing of this book and consistently brightens even the darkest Scottish weather with his smile. I appreciate your sacrifice. I could not have written this book without your support.
Finally, my thanks to my brother, Larry, who first introduced me to the world of computing. I would probably not know anything about computer programming if you had not left me your TRS-80. So this is all your fault, and I am glad you did it.
About the Reviewers
Swaroop C H has previously worked at Yahoo! and Adobe, has co-founded a startup, has written two technical books (one of which is used as a text book in more than ten universities worldwide), writes a popular blog that has been consistently rated one of the top ten blogs in India, and is a marathoner. More details at http://www.swaroopch.com/about/.
He has written two technical books—A Byte of Python and A Byte of Vim—beginner books to Python and Vim respectively. They are freely available under the Creative Commons license on his website www.swaroopch.com.
Andy Dustman (http://profiles.google.com/farcepest) is the primary author of MySQLdb, the MySQL interface for Python.
The MySQL-Python project is supported and funded purely by volunteers and donations by the user community at (http://sourceforge.net/projects/mysql-python/).
Andy has been using Python since 1997, and currently works on Django applications (using MySQL, of course) when not doing system and network administration. In his spare time, he rides motorcycles.
I would like to thank Kyle VanderBeek, who has recently become a co-developer on MySQLdb, and has helped to push me a bit to get some things done. 12 years is a long time to be working on a project, and motivation is sometimes hard to come by.
Ed Landa, for taking a chance on a grad school dropout, and for giving me the opportunity to release MySQLdb under an open source license.
Laura Michaletz, who encourages me and somehow manages to make me feel like a superstar.
And my wife, Wendy, for being there for me for three decades.
Geert JM Vanderkelen is a member of the MySQL Support Team at Sun, a wholly-owned subsidiary of Oracle. He is based in Germany and has worked for MySQL AB since April, 2005. Before joining MySQL he worked as developer, DBA and SysAdmin for various companies in Belgium and Germany. Today Geert specializes in MySQL Cluster and works together with colleagues around the world to ensure continued support for both customers and community. Geert is also the maintainer of MySQL Connector/Python.
Preface
Python is a dynamic programming language, which is completely enterprise ready, owing largely to the variety of support modules that are available to extend its capabilities. In order to build productive and feature-rich Python applications, we need to use MySQL for Python, a module that provides database support to our applications.
This book demonstrates how to boost the productivity of your Python applications by integrating them with the MySQL database server, the world's most powerful open source database. It will teach you to access the data on your MySQL database server easily with Python's library for MySQL using a practical, hands-on approach. Leaving theory to the classroom, this book uses real-world code to solve real-world problems with real-world solutions.
The book starts by exploring the various means of installing MySQL for Python on different platforms and how to use simple database querying techniques to improve your programs. It then takes you through data insertion, data retrieval, and error-handling techniques to create robust programs. The book also covers automation of both database and user creation, and administration of access controls. As the book progresses, you will learn to use many more advanced features of Python for MySQL that facilitate effective administration of your database through Python. Every chapter is illustrated with a project that you can deploy in your own situation.
By the end of this book, you will know several techniques for interfacing your Python applications with MySQL effectively so that powerful database management through Python becomes easy to achieve and easy to maintain.
What this book covers
Chapter 1, Getting Up and Running with MySQL for Python, helps you to install MySQL for Python specific software, how to import modules into your programs, connecting to a database, accessing online help, and creating a MySQL cursor proxy within your Python program. It also covers how to close the database connection from Python and how to access multiple databases within one program.
Chapter 2, Simple Querying, helps you to form and pass a query to MySQL, to look at user-defined variables, how to determine characteristics of a database and its tables, and program a command-line search utility. It also looks at how to change queries dynamically, without user input.
Chapter 3, Simple Insertion, shows forming and passing an insertion to MySQL, to look at the user-defined variables in a MySQL insertion, passing metadata between databases, and changing insertion statements dynamically without user input.
Chapter 4, Exception Handling, discusses ways to handle errors and warnings that are passed from MySQL for Python and the differences between them. It also covers several types of errors supported by MySQL for Python, and how to handle them effectively.
Chapter 5, Results Record-by-Record, shows situations in which record-by-record retrieval is desirable, to use iteration to retrieve sets of records in smaller blocks and how to create iterators and generators in Python. It also helps you in using fetchone() and fetchmany().
Chapter 6, Inserting Multiple Entries, discusses how iteration can help us execute several individual INSERT statements rapidly, when to use or avoid executemany(), and throttling how much data is inserted at a time.
Chapter 7, Creating and Dropping, shows to create and delete both databases and tables in MySQL, to manage database instances with MySQL for Python, and to automate database and table creation.
Chapter 8, Creating Users and Granting Access, focuses on creating and removing users in MySQL, managing database privileges with MySQL for Python, automating user creation and removal, to GRANT and REVOKE privileges, and the conditions under which that can be done.
Chapter 9, Date and Time Values, discusses what data types MySQL supports for date and time, when to use which data type and in what format and range, and frequently used functions for handling matters of date and time.
Chapter 10, Aggregate Functions and Clauses, shows how MySQL saves us time and effort by pre-processing data, how to perform several calculations using MySQL's optimized algorithms, and to group and order returned data by column.
Chapter 11, SELECT Alternatives, discusses how to use HAVING clauses, how to create temporary subtables, subqueries and joins in Python, and the various ways to join tables.
Chapter 12, String Functions, shows how MySQL allows us to combine strings and return the single, resulting value, how to extract part of a string or the location of a part, thus saving on processing, and how to convert cases of results.
Chapter 13, Showing MySQL Metadata, discusses the several pieces of metadata about a given table that we can access, which system variables we can retrieve, and how to retrieve user privileges and the grants used to give them.
Chapter 14, Disaster Recovery, focuses on when to implement one of several kinds of database backup plans, what methods of backup and disaster recovery MySQL supports, and how to use Python to back up databases
What you need for this book
The content of this book is written against MySQL 5.5, Python 2.5.2, and MySQL for Python 1.2.2. Development of the examples was done with MySQL 5.0, but everything was confirmed against the 5.5 documentation. As for operating systems, any of the main three will do: Microsoft Windows, Linux, or Mac. Any additional requirements of modules are discussed in the book as they come up.
Who this book is for
This book is meant for intermediate users of Python who want hassle-free access to their MySQL database through Python. If you are a Python programmer who wants database-support in your Python applications, then this book is for you. This book is a must-read for every focused user of the MySQL for Python library who wants real-world applications using this powerful combination of Python and MySQL.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: We can include other contexts through the use of the include directive.
A block of code is set as follows:
import MySQLdb
mydb = MySQLdb.connect(host = 'localhost',
user = 'skipper',
passwd = 'mysecret',
db = 'fish')
Any command-line input or output is written as follows:
>>> print results ((1L, 'tuna', Decimal('7.50')), (2L, 'bass', Decimal('6.75')), (3L, 'salmon', Decimal('9.50')), (4L, 'catfish', Decimal('5.00')),
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: clicking the Next button moves you to the next screen
.
Reference to a particular section or chapter are shown in italics.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Errata
Note
Downloading the example code for this book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
Chapter 1. Getting Up and Running with MySQL for Python
It may seem rather unnecessary to start a book on MySQL for Python with a chapter on setting it up. There are, in fact, several ways to get MySQL for Python in a place such that your local Python installation can use it. Which one you use will depend as much on your familiarity with your operating system and with Python itself, as it will on which operating system and version of Python you are running.
In this chapter we will cover the following:
Where you can get MySQL for Python
Installing MySQL for Python
Importing the module into your programs
Accessing online help about the MySQL for Python API and its accompanying modules
How to connect to a database
How to create a MySQL cursor proxy within your Python program
How to close the database connection from Python
How to access multiple databases within one program
Getting MySQL for Python
How you get MySQL for Python depends on your operating system and the level of authorization you have on it. In the following subsections, we walk through the common operating systems and see how to get MySQL for Python on each.
Using a package manager (only on Linux)
Package managers are used regularly on Linux, but none come by default with Macintosh and Windows installations. So users of those systems can skip this section.
A package manager takes care of downloading, unpacking, installing, and configuring new software for you. In order to use one to install software on your Linux installation, you will need administrative privileges.
Administrative privileges on a Linux system can be obtained legitimately in one of the following three ways:
Log into the system as the root user (not recommended)
Switch user to the root user using su
Use sudo to execute a single command as the root user
The first two require knowledge of the root user's password. Logging into a system directly as the root user is not recommended due to the fact that there is no indication in the system logs as to who used the root account. Logging in as a normal user and then switching to root using su is better because it keeps an account of who did what on the machine and when. Either way, if you access the root account, you must be very careful because small mistakes can have major consequences. Unlike other operating systems, Linux assumes that you know what you are doing if you access the root account and will not stop you from going so far as deleting every file on the hard drive.
Unless you are familiar with Linux system administration, it is far better, safer, and more secure to prefix the sudo command to the package manager call. This will give you the benefit of restricting use of administrator-level authority to a single command. The chances of catastrophic mistakes are therefore mitigated to a great degree.
Tip
More information on any of these commands is available by prefacing either man or info before any of the preceding commands (su, sudo).
Which package manager you use depends on which of the two mainstream package management systems your distribution uses. Users of RedHat or Fedora, SUSE, or Mandriva will use the RPM Package Manager (RPM) system. Users of Debian, Ubuntu, and other Debian-derivatives will use the apt suite of tools available for Debian installations. Each package is discussed in the following:
Using RPMs and yum
If you use SUSE, RedHat, or Fedora, the operating system comes with the yum package manager. You can see if MySQLdb is known to the system by running a search (here using sudo):
sudo yum search mysqldb
If yum returns a hit, you can then install MySQL for Python with the following command:
sudo yum install mysqldb
Using RPMs and urpm
If you use Mandriva, you will need to use the urpm package manager in a similar fashion. To search use urpmq:
sudo urpmq mysqldb
And to install use urpmi:
sudo urpmi mysqldb
Using apt tools on Debian-like systems
Whether you run a version of Ubuntu, Xandros, or Debian, you will have access to aptitude, the default Debian package manager. Using sudo we can search for MySQLdb in the apt sources using the following command:
sudo aptitude search mysqldb
On most Debian-based distributions, MySQL for Python is listed as python-mysqldb.
Once you have found how apt references MySQL for Python, you can install it using the following code:
sudo aptitude install python-mysqldb
Using a package manager automates the entire process so you can move to the section Importing MySQL for Python.
Using an installer for Windows
Windows users will need to use the older 1.2.2 version of MySQL for Python. Using a web browser, go to the following link:
http://sourceforge.net/projects/mysql-python/files/
This page offers a listing of all available files for all platforms. At the end of the file listing, find mysql-python and click on it. The listing will unfold to show folders containing versions of MySQL for Python back to 0.9.1. The version we want is 1.2.2.
Note
Windows binaries do not currently exist for the 1.2.3 version of MySQL for Python. To get them, you would need to install a C compiler on your Windows installation and compile the binary from source.
This is outside the purpose of the present book, but tips for how to do this are contained in the README file that accompanies the 1.2.3 version.
Click on 1.2.2 and unfold the file listing. As you will see, the Windows binaries are differentiated by Python version—both 2.4 and 2.5 are supported. Choose the one that matches your Python installation and download it. Note that all available binaries are for 32-bit Windows installations, not 64-bit.
After downloading the binary, installation is a simple matter of double-clicking the installation EXE file and following the dialogue. Once the installation is complete, the module is ready for use. So go to the section Importing MySQL for Python.
Using an egg file
One of the easiest ways to obtain MySQL for Python is as an egg file, and it is best to use one of those files if you can. Several advantages can be gained from working with egg files such as:
They can include metadata about the package, including its dependencies
They allow for the use of egg-aware software, a helpful level of abstraction
Eggs can, technically, be placed on the Python executable path and used without unpacking
They save the user from installing packages for which they do not have the appropriate version of software
They are so portable that they can be used to extend the functionality of third-party applications
Installing egg handling software
One of the best known egg utilities—Easy Install, is available from the PEAK Developers' Center at http://peak.telecommunity.com/DevCenter/EasyInstall. How you install it depends on your operating system and whether you have package management software available. In the following section, we look at several ways to install Easy Install on the most common systems.
Using a package manager (Linux)
On Ubuntu you can try the following to install the easy_install tool (if not available already):
shell> sudo aptitude install python-setuptools
On RedHat or CentOS you can try using the yum package manager:
shell> sudo yum install python-setuptools
On Mandriva use urpmi:
shell> sudo urpmi python-setuptools
You must have administrator privileges to do the installations just mentioned.
Without a package manager (Mac, Linux)
If you do not have access to a Linux package manager, but nonetheless have a Unix variant as your operating system (for example, Mac OS X), you can install Python's setuptools manually. Go to:
http://pypi.python.org/pypi/setuptools#files
Download the relevant egg file for your Python version.
When the file is downloaded, open a terminal and change to the download directory. From there you can run the egg file as a shell script. For Python 2.5, the command would look like this:
sh setuptools-0.6c11-py2.5.egg
This will install several files, but the most important one for our purposes is easy_install, usually located in /usr/bin.
On Microsoft Windows
On Windows, one can download the setuptools suite from the following URL:
http://pypi.python.org/pypi/setuptools#files
From the list located there, select the most appropriate Windows executable file.
Once the download is completed, double-click the installation file and proceed through the dialogue. The installation process will set up several programs, but the one important for our purposes is easy_install.exe. Where this is located will differ by installation and may require using the search function from the Start Menu.
On 64-bit Windows, for example, it may be in the Program Files (x86) directory. If in doubt, do a search. On Windows XP with Python 2.5, it is located here:
C:\Python25\Scripts\easy_install.exe
Note that you may need administrator privileges to perform this installation. Otherwise, you will need to install the software for your own use. Depending on the setup of your system, this may not always work.
Installing software on Windows for your own use requires the following steps:
Copy the setuptools installation file to your Desktop.
Right-click on it and choose the runas option.
Enter the name of the user who has enough rights to install it (presumably yourself).
After the software has been installed, ensure that you know the location of the easy_install.exe file. You will need it to install MySQL for Python.
Installing MySQL for Python from an egg file
After installing EasyInstall, you still need to install the MySQL for Python egg. The egg files for MySQL for Python can be downloaded from the following URL:
http://sourceforge.net/projects/mysql-python/files/
There you will see a list of all available files relevant to MySQL for Python.
Which one you use depends on your operating system and your installed Python version. Currently, the only egg files available for MySQL for Python version 1.2.3c1 are for Linux running either Python 2.5 or 2.6. Mac users should use a tarball (tar.gz) file as discussed in the next section.
To get an egg file for Windows, click on the MySQL-python directory and select the 1.2.2 version. This is the same directory used for the Windows binaries discussed earlier in this chapter. This time, however, you need to select an egg for Windows that fits either Python 2.4 or 2.5. There is no 2.6 version.
Once you have the egg file for MySQL for Python, you simply need to invoke EasyInstall over the newly-downloaded egg file. How you do that will depend on the permissions you have for your operating system.
With administrator permissions, you can simply call the EasyInstall binary. For Linux, it will look like this:
shell> easy_install
For Windows, you will use a command similar to this one:
C:\Python25\Scripts\easy_install.exe
Note that you must have administrator privileges to do this. Otherwise, Windows users will have to install the software locally. Linux users can use sudo.
EasyInstall will then unpack the archive, install it in your default Python installation folders, and configure it for immediate use.
For Windows users, if you had to install setuptools locally, you may also require a local installation of Python itself in order to install MySQL for Python. See the section On Microsoft Windows under Installing egg-handling software, for help with this. If you need to go through this process, all of your configurations will be local, so you are best to use