MySQLdb for Python

Status
Not open for further replies.

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Just haven't had a chance, we've been quite busy with some upcoming changes to x10. When things slow down a bit I'd be happy to look into installing extra modules. Just keep reminding me :)

-Corey
 

Dannz

New Member
Messages
43
Reaction score
0
Points
0
It was me that made the request. I'm kind of hanging on having the MySQLdb as the glue between the database, Python and the Flash swf front-end. I can live without the other modules and work around not having XML if need be, but MySQLdb is fairly fundamental. I understand its been busy with PHP etc., but when you do have a chance it would be appreciated. Thanks *Face for raising this - I was giving up hope and thinking I might have to find a new home. :(
Edit:
I just saw the post from *Face asking for help on Python.

The following simple script might help. Just put in in the cgi-bin (with 711) and either name it with a .cgi extension. You just have to pass the parameters - database name, user name, password, and a SQL statement, and it will return the results of the SQL statement.

WARNING - I would not use this 'live' - modify it to suit you, but it isn't a good idea to allow access to full SQL query - I usually hardcode the "SELECT xx FROM xx WHERE" and have the remainder passed from the client side - you can join the two bits together in the Python cgi script - easy ->

prefix = "SELECT * FROM myDatabase WHERE "
stmt = prefix + incomingSnippet
cursor.execute(stmt)


<- Python is nice and easy (and very powerful) - its worth the effort of learning.

Here's the simple version of the cgi script - all the modules are part of the standard Python package except the MySQLdb module (which has to be installed by administrator). :) ->


#!/usr/bin/env python

from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
import MySQLdb
import string

class Pyro:

def pydb(self, mydb, mypw, myusr, stmt, data):
db=MySQLdb.Connect(db=mydb, host='localhost', user=myusr, passwd=mypw)
cursor=db.cursor()
cursor.execute(stmt)
data=cursor.fetchall()
cursor.close()
db.close()
return data

else:
return "no command given"

pdb = Pyro() #create an instance of the class
handler = CGIXMLRPCRequestHandler() #create the handler
handler.register_instance(pdb) #register the instance as a web service
handler.handle_request() #start accepting requests
Edit:
PS - some of the indentation has been lost when posting this - it matters! If you 're not sure about Python indentation, email me on dan()law()nz()gmail()com.
Edit:
Corey, my apologies - I see you asked for some information linking to the MySQLdb module -


MySQLdb can be got from http://sourceforge.net/project/showfiles.php?group_id=22307

It is the 'standard' MySQL API for Python - lots more info from Google.
 
Last edited:

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Okay, just keep reminding me I will get this installed once things settle down a bit. 890 total posts yesteday, bleh!

-Corey
 

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Can you verify if this works now?
 

*Face

New Member
Messages
62
Reaction score
0
Points
0
I tried out a modified version of Dannz's script, and it seems to be working:
Code:
#!/usr/bin/python

import MySQLdb
import string
print "Content-Type: text/plain\n"
//connect to mysql here 
cursor=db.cursor()
cursor.execute('SELECT version()')
data=cursor.fetchall()
cursor.close()
db.close()
print data
 
Status
Not open for further replies.
Top