|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
I am a master student in Australia and trying to do a project - website - about accessing MySQL database in Linux platform with Python/CGI script through webpage, but the python codes didn't work.
Do you know how to do it? Can you give us some sample codes? I have two python files as follows, can you check them what are wrong? 1) #!/usr/bin/python import cgi import mysql database = 'bestuni' query = 'SELECT * FROM uni' dbh = MySQL.connect('localhost', 'root', 'mypassword') dbh.selectdb(database) result = dbh.do(query) print "Content-type: text/htmlrnrn" print "<html><head><title>Python Bestuni</title></head><body>n" for row in result: print "Best Universities =", row[0], ", desc=", row[1], ", widgetid=", row[2], "<p>" print "</body></html>n" dbh.close() 2) #!/usr/bin/python import cgi import pickle import MySQL def uni(): print "Content-type:text/htmln" print '<html>' print "<head><title> Harley's uni cgi</title></head>" print '<body>' print '<form method="POST" action="http://ip54200.scu.edu.au/harley-cgi/uni.py">' print '<p><center><h1>The best universities around the world</h1></center></p>' print '<p><center><h2>Do you know the best universities over the world?</h2></center></p>' print '<p><center><input type="submit"name="yes" value="Yes"></center></p>' print '<p><center><input type="submit"name="no" value="No"></center></p>' print '</form>' form=cgi.FieldStorage() f=open("bestuni.mysql","r") data=pickle.load(f) f.close() if form.has_key("new"): print "<b>The best universities around the world are as follows:</b>" print data[rank],data[name],data[country],data[e_mail] elif form.has_key("no"): print "<p>Wow, man! You don't want to those things? mmm...</p>" f.close() print "</body>" print "</html>" uni() E-mail me: goodgoodtom@yahoo.com |
|
#2
|
|||
|
|||
|
I'm not sure how the Mysql library for python works but your browser header should look exactly like this:
print "Content-type:text/htmlnn" ------------------ PB |
|
#3
|
|||
|
|||
|
MySQL is correct mysql module name?
I use MySQLdb module. in That case, Examples likes) import MySQLdb connection = MySQLdb.connect(user='hyun',db='hyun') cursor = connection.cursor() query="select count(*) from pybbs" cursor.execute(query) RowNum=cursor.fetchone() Then, RowNum[0] has the result. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to access MySQL with Python?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|