ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Using the MySQLdb interface
Submitter: Mark Nenadov (other recipes)
Last Updated: 2002/05/12
Version no: 1.4
Category: Databases

 

3 stars 13 vote(s)


Description:

A simple example showing how to use the MySQLdb interface to function with your MySQL database.

Source: Text Source

import MySQLdb

# Create a connection object and create a cursor
Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="joe", passwd="egf42" db="tst")
Cursor = Con.cursor()

# Make SQL string and execute it
sql = "SELECT * FROM Users"
Cursor.execute(sql)

# Fetch all results from the cursor into a sequence and close the connection
Results = Cursor.fetchall()
Con.close()

Discussion:

This is pretty self explanitory. You can get the MySQLdb module from http://sourceforge.net/projects/mysql-python



Add comment

Number of comments: 4

New Link for Downloads, Steve Holden, 2002/05/02
Note that for up-to-date distributions of MySQLdb you need to go to http://sourceforge.net/projects/mysql-python

Add comment

Thank you!, Mark Nenadov, 2002/05/12
I will update the url so nobody gets mislead.
Add comment

Typo, Patrick Paysant, 2002/11/23
In order to have the script working, I have to add a comma between passwd and db parameters.

As here :

Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="joe", passwd="egf42", db="tst")

Anyway, thanks for this recipe, it help me to start with mysql-python.

Add comment

UNIX Sockets, Reed O'Brien, 2006/04/28

con = MySQLdb.Connect(host='localhost', port=3306, user='user', passwd='secret', db='dbname', unix_socket='/var/lib/mysql/mysql.sock') 


sockets are faster on localhost and some distributions of linux keep things in funny places... You can just add the unix_socket parameter like above for Fedora Core 3.

Maybe named_pipe on windows is similar; I can't say though.
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.