Python
  Home arrow Python arrow Page 3 - MySQL Connectivity With Python
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

MySQL Connectivity With Python
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 142
    2002-08-21

    Table of Contents:
  • MySQL Connectivity With Python
  • Getting Started
  • Animal Antics
  • One By One
  • A Different Species
  • Be Prepared
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    MySQL Connectivity With Python - Animal Antics


    (Page 3 of 7 )

    With that out of the way, here's a simple example that demonstrates some of the functionality of the DBI. Consider the following database table,

    mysql> SELECT * FROM animals; +---------+----------+ | name | species | +---------+----------+ | Wallace | Walrus | | Polly | Parrot | | Freddie | Frog | | Tusker | Elephant | | Sammy | Skunk | +---------+----------+ 5 rows in set (0.01 sec)
    and then consider this short Python script, which connects to the database and prints out the data within the table.

    #!/usr/bin/python # import MySQL module import MySQLdb # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret", db="db56a") # create a cursor cursor = db.cursor() # execute SQL statement cursor.execute("SELECT * FROM animals") # get the resultset as a tuple result = cursor.fetchall() # iterate through resultset for record in result: print record[0] , "-->", record[1]
    Most of this is self-explanatory, but let me go through it with you briefly anyway.

    The first step is to import the MySQLdb module, via Python's "import" function.

    # import MySQL module import MySQLdb
    Once that's done, you can open up a connection to the MySQL database server, by passing the module's connect() method a series of connection parameters - the server name, the database user name and password, and the database name.

    # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret", db="db56a")
    A successful connection returns a Connection object, which you can use to create a cursor.

    # create a cursor cursor = db.cursor()
    This cursor is needed to execute an SQL statement, and to retrieve the generated resultset.

    # execute SQL statement cursor.execute("SELECT * FROM animals") # get the resultset as a tuple result = cursor.fetchall()
    A number of methods are available to retrieve the SQL resultset - the one used here is the fetchall() method, which returns a tuple of tuples, each inner tuple representing a row of the resultset. This tuple can then be iterated over with a regular "for" loop, and its elements printed to the standard output.

    # iterate through resultset for record in result: print record[0] , "-->", record[1]

    More Python Articles
    More By icarus, (c) Melonfire


       · This tutorial is very helpful to me.Thank you very much.
       · Thank you very much, a concise and accurate tutorial on how to get Python working...
       · I am a noob in Python, and loved this article. Helped me a lot in designing my...
       · I have tried your tutorial but I have a problem when I try save data to a data base....
       · i am also having the same problem, please help
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway