A simple example showing how to use the MySQLdb interface to function with your MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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()
|
This is pretty self explanitory. You can get the MySQLdb module from http://sourceforge.net/projects/mysql-python
Tags: database
New Link for Downloads. Note that for up-to-date distributions of MySQLdb you need to go to http://sourceforge.net/projects/mysql-python
Thank you! I will update the url so nobody gets mislead.
Typo. In order to have the script working, I have to add a comma between passwd and db parameters.
As here :
Anyway, thanks for this recipe, it help me to start with mysql-python.
UNIX Sockets.
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.