Connecting MySQL and Python

Similar documents
Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

How To Use The Listerv Maestro With A Different Database On A Different System (Oracle) On A New Computer (Orora)

Introduction to Python

FileMaker 14. ODBC and JDBC Guide

PROGRESS DATADIRECT QA AND PERFORMANCE TESTING EXTENSIVE TESTING ENSURES DATA CONNECTIVITY THAT WORKS

FileMaker 13. ODBC and JDBC Guide

inforouter V8.0 Server & Client Requirements

MySQL Connector/Python Developer Guide

FileMaker 12. ODBC and JDBC Guide

Elastic Scalability in MySQL Fabric using OpenStack

Release Bulletin Sybase ETL Small Business Edition 4.2

This guide specifies the required and supported system elements for the application.

Version Overview. Business value

FileMaker 11. ODBC and JDBC Guide

MySQL for Beginners Ed 3

PYTHON IN A NUTSHELL. O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo. Alex Martelli. Second Edition

SIMIAN systems. Setting up a Sitellite development environment on Windows. Sitellite Content Management System

Failover Support. DataDirect Connect for ODBC Drivers. Introduction. Connection Failover

Suite. How to Use GrandMaster Suite. Exporting with ODBC

unipaas V1.9g Release Notes

Microsoft SQL Server Connector for Apache Hadoop Version 1.0. User Guide

OpenScape Business V2

Getting Started using the SQuirreL SQL Client

Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Database Toolkit: Portable and Cost Effective Software

ODBC Client Driver Help Kepware, Inc.

Writing Scripts with PHP s PEAR DB Module

AWS Schema Conversion Tool. User Guide Version 1.0

KonyOne Server Installer - Linux Release Notes

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Hive Interview Questions

Tivoli Endpoint Manager for Remote Control Version 8 Release 2. User s Guide

CDH installation & Application Test Report

JDBC (Java / SQL Programming) CS 377: Database Systems

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

Windows Authentication on Microsoft SQL Server

Monitoring MySQL. Geert Vanderkelen MySQL Senior Support Engineer Sun Microsystems

Getting Started with RES ONE Automation 2015

Users Guide. Ribo 3.0

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

Guide to the MySQL Workbench Migration Wizard: From Microsoft SQL Server to MySQL

PUBLIC Installation: SAP Mobile Platform Server for Linux

Witango Application Server 6. Installation Guide for OS X

System Requirements - Table of Contents

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

RTI Real-Time Connect. Release Notes

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Kognitio Technote Kognitio v8.x Hadoop Connector Setup

Getting Started with Attunity CloudBeam for Azure SQL Data Warehouse BYOL

OWB Users, Enter The New ODI World

Data Access Guide. BusinessObjects 11. Windows and UNIX

DBA Tutorial Kai Voigt Senior MySQL Instructor Sun Microsystems Santa Clara, April 12, 2010

SQL Server. SQL Server 100 Most Asked Questions: Best Practices guide to managing, mining, building and developing SQL Server databases

New Features in MySQL 5.0, 5.1, and Beyond

Setting Up ALERE with Client/Server Data

MD Link Integration MDI Solutions Limited

Brekeke SIP Server Version 3 Using a Third-Party Database Tutorial Brekeke Software, Inc.

Writing MySQL Scripts With Python's DB-API Interface

LDAPCON Sébastien Bahloul

Oracle Essbase Integration Services. Readme. Release

Oracle Tools and Bindings with languages

EOP ASSIST: A Software Application for K 12 Schools and School Districts Installation Manual

SCALABLE DATA SERVICES

Configuring and Integrating Oracle

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Integrating NLTK with the Hadoop Map Reduce Framework Human Language Technology Project

VO-Dance Installation Guide

RemoteTM LAN Server User Guide

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

Performance Testing Web 2.0

Zend Server 4.0 Beta 2 Release Announcement What s new in Zend Server 4.0 Beta 2 Updates and Improvements Resolved Issues Installation Issues

Table of Contents. FleetSoft Installation Guide

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

REDCap General Security Overview

Accessing Oracle 11g from SAS on Linux Using DataDirect Connect for ODBC

IBM Lotus Enterprise Integrator (LEI) for Domino. Version August 17, 2010

Products and Solutions

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Witango Application Server 6. Installation Guide for Windows

Upgrading MySQL from 32-bit to 64-bit

Sidebar Dashboard User Guide. Modified: June, 2013 Version 8.2

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Python for Series 60 Platform

Architecting the Future of Big Data

Fiery E100 Color Server. Welcome

Benjamin Carlson Node.js SQL Server for Data Analytics and Multiplayer Games Excerpt from upcoming MQP-1404 Project Report Advisor: Brian Moriarty

LoadRunner and Performance Center v11.52 Technical Awareness Webinar Training

Greenplum Database 4.0 Connectivity Tools for Windows

Waspmote IDE. User Guide

MySQL and Virtualization Guide

How-To: MySQL as a linked server in MS SQL Server

Microsoft SQL Server Staging

Laptop Backup - Administrator Guide (Windows)

4D v1x ODBC Driver INSTALLATION GUIDE

Install and Configure SQL Server Database Software Interview Questions and Answers

SQL and Java. Database Systems Lecture 19 Natasha Alechina

MySQL Administration and Management Essentials

IceWarp Server Windows Installation Guide

Webapps Vulnerability Report

Transcription:

Connecting MySQL and Python OʼReilly MySQL Conference & Expo 2010 - Tue, 13 Apr 2010 Geert Vanderkelen geert.vanderkelen@sun.com 1. Introduction This handout is supporting the talk Connecting MySQL and Python which tries to give an overview of how to connect your Python application with MySQL. It introduces Python, goes over some MySQL features and dives into the possibilities to setup the connection between the two using drivers and popular frameworks. 2.About Python Python is an object-oriented programming language which is easy to learn and easy to read. It comes with a large collection of build-in functionality but can easily be extended. Python is used for scripting and web development, but it is also embed it into application or can for creation graphical games for example. There should be one-- and preferably only one-- obvious way to do it. (Part of The Zen of Python) Software quality Small example, following C code: int i = 0; while(i < 1000) { if(i < 10) foo(); else { bar(); foo(); } spam(); i++; } Lets take a look at how you would do the same in Python. Try to write the following differently (of course, you can add spaces): i = 0 while i < 1000: if i < 10: foo() else: bar() foo() spam() i += 1 Portable Your Python scripts or applications will run on virtually all major platforms where Python is installed. Simply copying it over from a Linux distribution to a Windows machine should work. Object-Oriented Python is an object-oriented language, but you donʼt have too. Just like C++, if you want to develop in more procedural or structural way: you can. Copyright 2010, Oracle and/or its affiliates. All rights reserved. - 1/5

All included, and extendible The distribution of Python comes with a large collection of functionality known as the standard library. It includes most tools you need to get you going right away. There are also lots of 3rd party modules available. The database interfaces for connecting to MySQL are examples. Alternative implementations There are currently three primary implementations of the Python language: CPython, Jython and IronPython. CPython is the most common and standard implementation of Python written using the ANSI C programming language. When you run CPython source it will translate the code to byte code and run it using the Python Virtual Machine (PVM). CPython is the Python you would download from http://python.org and find on most major platforms pre-installed. Script Java using the Python language. Jython is completely written in and integrates with Java. It allows you to code Java using the Python language and run it through the Java Virtual Machine. is a library and tool called 2to3 which makes easier. 3.Database Interfaces Python defines the portable Database API v2.0 in PEP-249 (Python Enhancement Proposal). This protocol should be implemented by all modules which offer an interface to DBMS. A script which works using MySQL should also work using Oracle by just replacing the database interface. MySQLdb The most used database interface for MySQL is without doubt MySQL for Python, i.e. MySQLdb. It was developed and is maintained by Andy Dustman. MySQLdb uses the C MySQL Client Libraries and usually needs MySQL installed on the box on which you are running your applications. Binaries are downloadable for most platforms like Windows and Mac OS X. Most Linux and BSD distributions also make it available through their packaging system. oursql oursql is a recent development by the Python community. Just like MySQLdb it uses the C MySQL Client Libraries. Jython makes it possible to script Java. Similar to Jython, you have IronPython which is integrates with Microsoftʼs.Net framework as well as Mono. MySQL Connector/Python: written in pure Python and works with Python v3.1 Python v3.x The next generation of Python, v3, is already available, but the adaption is a bit slow. Python v2 and v3 are not compatible and most, if not all code, has to be rewritten. This is quite some work for all 3rd party module developers. Luckily there 4.MySQL Connector/ Python MySQL Connector/Python is developed at Sun and implements the MySQL Client/ Server Protocol completely in Python. This makes it extremely easy to install and start Copyright 2010, Oracle and/or its affiliates. All rights reserved. - 2/5

coding right away. You donʼt even need MySQL installed on the machine unless of course you need a local test database. Releases want warnings, but instead would like an error: configure MySQL to use the TRADITIONAL SQL Mode. SET @@session.sql_mode = TRADITIONAL There is no ʻstableʼ or ʻGAʼ release yet, but we try to make regular development packages to make it easy to try it out. MySQL Connector/Python is released under GPLv2 and can be downloaded from Launchpad (see Links). Packaging Python v2.5 and later, and v3.1 are supported. Both editions come in the same package. and installation takes care to install the correct one. Make sure you use the correct examples for your Python version: the v3.1 youʼll find in the py3k/ subdirectory. 5.Things about MySQL What is important in MySQL for the Connectors in general, and for Python in particular. Character set The world is going unicode, and it might give you less headaches to do the same when youʼre coding in Python. Most drivers support the use_unicode parameters and can set the character set. MySQL Connector/Python and oursql are for example defaulting to UTF-8. Default Storage Engine It might be good to set the default storage engine to make sure tables are created correctly by frameworks. For example, you can set it to InnoDB so all your tables are transactional. SET @@global.storage_engine = InnoDB SQL Mode If you donʼt like MySQL to insert to long strings in to small columns; if you donʼt like invalid dates to be accepted; if you donʼt You can set it per session, or globally for each connection to the server. 6.Frameworks and ORMs Django Django is a web application framework which makes creating database-driven websites a piece of cake. It comes with everything included and works great with MySQL. It currently comes only with support for MySQLdb, but attempts to make it work with MySQL Connector/Python has been successful. TurboGears Another great web application framework, TurboGears consist of several other projects such as Pylons, Genshi and SQLAlchemy. SQLAlchemy SQLAlchemy is an SQL Toolkit and Object- Relational Mapper. The latest v0.6 should work out of the box with MySQLdb, oursql and MySQL Connector/Python. Twisted Twisted is a event-driven networking framework. It comes with 'adbapi' to elevate the problem of your application blocking when waiting for a result from the database. It offers an asynchronous mapping to the underlaying database interfaces using Python's DB-API v2.0. Using this, you can define connection pools, send a query and let a callback handle the results when it is ready. Copyright 2010, Oracle and/or its affiliates. All rights reserved. - 3/5

7.Examples Simply getting current date & time #import MySQLdb as db import mysql.connector as db #import oursql as db cur.execute("select NOW()") print(cur.fetchall()) cur.close() A Warning turns into an Error import mysql.connector as db cur.execute("set SQL_MODE = 'TRADITIONAL'") cur.execute("create TABLE t1 (c1 CHAR(5))") s = "INSERT INTO t1 (c1) VALUES (%s)" cur.execute(s, ('wrong length',)) except db.error, e: print "Data is to long!" cur.close() Use format codes using execute() ins = "INSERT INTO t1 (dayofbirth) VALUES (%s)" cur.execute(ins, (datetime(1977,6,14),)) Jython using MySQL Connector/J import java from java.lang import Class from java.sql import DriverManager class PlayMySQL(java.lang.Object): def init (self): cs = "jdbc:mysql://%s/%s?user=%s" % ( 'localhost','test','root') c = DriverManager.getConnection(cs) self.conn = c def show_engines(self): stmt = self.conn.createstatement() rs = stmt.executequery("show ENGINES") while rs.next(): print rs.getstring("engine") if name == ' main ': drv = "com.mysql.jdbc.driver" Class.forName(drv).newInstance(); p = PlayMySQL() 8.Some links Cython http://cython.org Django http://djangoproject.com IronPython http://ironpython.com Jython http://jython.org MySQL http://dev.mysql.com MySQL Connector/Python http://launchpad.net/myconnpy MySQLdb http://mysql-python.sourceforge.net/ oursql https://launchpad.net/oursql Python http://python.org SQLAlchemy http://sqlalchemy.org/ Copyright 2010, Oracle and/or its affiliates. All rights reserved. - 4/5

TurboGears http://turbogears.com/ Twisted http://twistedmatrix.com 9.About The Author Geert Vanderkelen is a member of the MySQL Support Team at Sun Microsystems. 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. He is also the maintainer of Sun's MySQL Connector/ Python. Contact Geert Vanderkelen geert.vanderkelen@sun.com Copyright 2010, Oracle and/or its affiliates. All rights reserved. - 5/5