473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

mysql - python cgi connection

Can anyone help me to connect the mysql with python-CGI.

I tried it. but it doesn't work.

please help me.
Jan 18 '07 #1
18 4194
ghostdog74
511 Expert 256MB
Can anyone help me to connect the mysql with python-CGI.

I tried it. but it doesn't work.

please help me.
as usual, help us to help you by providing as much info as you can, like when you said cannot work, what exactly cannot work. Errors you encountered, provide the code that you have done and describe your expected results, etc etc
Jan 18 '07 #2
bartonc
6,596 Expert 4TB
Can anyone help me to connect the mysql with python-CGI.

I tried it. but it doesn't work.

please help me.
Ghostdog is right. There are posting guidelines which state that you must describe what your trouble is AND give examples of what you have done to get to this point. "I tried it. but it doesn't work." doesn't give us anything to go in in terms of helping you solve your problem.
Jan 19 '07 #3
cuties
8
i myself working on mysql and cgi-python for a project assigned to me. if you don't mind i'll post my codes here and you can try to see it.
Jan 19 '07 #4
bartonc
6,596 Expert 4TB
i myself working on mysql and cgi-python for a project assigned to me. if you don't mind i'll post my codes here and you can try to see it.
Yes. Please post you code.
Jan 19 '07 #5
Apologize!!!!!!!!!!!!!!!!!!

Here is my code...

It is not working...but when i connect mysql in standalone python code, it's working...

Please check my code and reply...

thanks..

Expand|Select|Wrap|Line Numbers
  1. #!C:\Python25\python.exe
  2.  
  3. print "Content-type: text/html\n\n"
  4. import sys
  5. import MySQLdb
  6.  
  7. try:
  8.     conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
  9.     print "Connected<br/>"
  10. except:
  11.     print "Cannot connect to server.</br>"
  12.     sys.exit(1)
  13.  
  14. try:
  15.                 cursor = conn.cursor()
  16.     cursor.execute("SELECT * FROM sudha");
  17.     rows = cursor.fetchall()
  18.     for row in rows:
  19.         print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
  20.         print "%d rows were returned<br/>" % cursor.rowcount
  21.         cursor.close()
  22. except MySQLdb.Error, e:
  23.     print "query failed<br/>"
  24.     print e
  25.  
  26. conn.close()
  27. print "Disconnected<br/>"
Jan 19 '07 #6
sorry, now it's in code tag:-

Expand|Select|Wrap|Line Numbers
  1. #!C:\Python25\python.exe
  2.  
  3. print "Content-type: text/html\n\n"
  4. import sys
  5. import MySQLdb
  6.  
  7. try:
  8.     conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
  9.     print "Connected<br/>"
  10. except:
  11.     print "Cannot connect to server.</br>"
  12.     sys.exit(1)
  13.  
  14. try:
  15.         cursor = conn.cursor()
  16.     cursor.execute("SELECT * FROM sudha");
  17.     rows = cursor.fetchall()
  18.     for row in rows:
  19.         print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
  20.         print "%d rows were returned<br/>" % cursor.rowcount
  21.         cursor.close()
  22. except MySQLdb.Error, e:
  23.     print "query failed<br/>"
  24.     print e
  25.  
  26. conn.close()
  27. print "Disconnected<br/>"
  28. sys.exit(0)
Jan 19 '07 #7
bartonc
6,596 Expert 4TB
Apologize!!!!!!!!!!!!!!!!!!

Here is my code...

It is not working...but when i connect mysql in standalone python code, it's working...

Please check my code and reply...

thanks..

Expand|Select|Wrap|Line Numbers
  1. #!C:\Python25\python.exe
  2.  
  3. print "Content-type: text/html\n\n"
  4. import sys
  5. import MySQLdb
  6.  
  7. try:
  8.     conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
  9.     print "Connected<br/>"
  10. except:
  11.     print "Cannot connect to server.</br>"
  12.     sys.exit(1)
  13.  
  14. try:
  15.                 cursor = conn.cursor()
  16.     cursor.execute("SELECT * FROM sudha");
  17.     rows = cursor.fetchall()
  18.     for row in rows:
  19.         print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
  20.         print "%d rows were returned<br/>" % cursor.rowcount
  21.         cursor.close()
  22. except MySQLdb.Error, e:
  23.     print "query failed<br/>"
  24.     print e
  25.  
  26. conn.close()
  27. print "Disconnected<br/>"
After I added code tags (which you will learn to do as we go along), I see a nasty indentation error at the top of the second try block. Are you getting something like syntax or indentation error? Will you paste the error here?
Jan 19 '07 #8
Hi barton..

the line cursor = conn.cursor() is in right position only.. there is no identation error... i pasted it correctly..but that line moved one tab. I dont know why? But in my code it is in right position only..

I am not getting any thing in the browser page, when i run this program...
It's plain....

Expand|Select|Wrap|Line Numbers
  1. try:
  2.                 cursor = conn.cursor()
  3.     cursor.execute("SELECT * FROM sudha");
  4.     rows = cursor.fetchall()
  5.     for row in rows:
  6.         print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
  7.         print "%d rows were returned<br/>" % cursor.rowcount
  8.         cursor.close()
  9. except MySQLdb.Error, e:
  10.     print "query failed<br/>"
  11.     print e
Jan 19 '07 #9
bartonc
6,596 Expert 4TB
Hi barton..

I am not getting any thing in the browser page, when i run this program...
It's plain....
Well, I see a database connection and some printing stdout, but I don't see any browser connection here. Do you know if your CGI redirects stdout?
Jan 19 '07 #10
Actually,I am using apache 2.2 server. so i will store the programs in apache-cgi-bin. then i will run that program in browser using the cgi-bin path.

In is simple cgi program...
we r not giving any specified browser path.

Expand|Select|Wrap|Line Numbers
  1. #!C:\Python25\python.exe
  2. print "Content-Type: text/html\n\n"
  3. print "hello"
like that..i am testing to connect the mysql...
Jan 19 '07 #11
bartonc
6,596 Expert 4TB
Hi barton..

the line cursor = conn.cursor() is in right position only.. there is no identation error... i pasted it correctly..but that line moved one tab. I dont know why? But in my code it is in right position only..

I am not getting any thing in the browser page, when i run this program...
It's plain....

Expand|Select|Wrap|Line Numbers
  1. try:
  2.                 cursor = conn.cursor()
  3.     cursor.execute("SELECT * FROM sudha");
  4.     rows = cursor.fetchall()
  5.     for row in rows:
  6.         print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
  7.         print "%d rows were returned<br/>" % cursor.rowcount
  8.         cursor.close()
  9. except MySQLdb.Error, e:
  10.     print "query failed<br/>"
  11.     print e
Except for the strange indentation, this looks perfectly fine to me. Have you tried running this under python using an IDE or otherwise where the window will stay open long enough to read the messages?
Jan 19 '07 #12
bartonc
6,596 Expert 4TB
Actually,I am using apache 2.2 server. so i will store the programs in apache-cgi-bin. then i will run that program in browser using the cgi-bin path.

In is simple cgi program...
we r not giving any specified browser path.

Expand|Select|Wrap|Line Numbers
  1. #!C:\Python25\python.exe
  2. print "Content-Type: text/html\n\n"
  3. print "hello"
like that..i am testing to connect the mysql...
Did you see this thread?
Jan 19 '07 #13
I don't know whether my python-cgi code is correct or not...
you people have to say!!!!!!!!!!

Because i am beginner, just now started to program in cgi..

This is the output, i am getting when i run it in IDLE.

Expand|Select|Wrap|Line Numbers
  1. Connected<br/>
  2. sudhakar: srf<br/>
  3. 4 rows were returned<br/>
  4. anand: scientist<br/>
  5. 4 rows were returned<br/>
  6. ramesh: jrf<br/>
  7. 4 rows were returned<br/>
  8. ren: trainee<br/>
  9. 4 rows were returned<br/>
  10. Disconnected<br/>
Jan 19 '07 #14
bartonc
6,596 Expert 4TB
I don't know whether my python-cgi code is correct or not...
you people have to say!!!!!!!!!!

Because i am beginner, just now started to program in cgi..

This is the output, i am getting when i run it in IDLE.

Expand|Select|Wrap|Line Numbers
  1. Connected<br/>
  2. sudhakar: srf<br/>
  3. 4 rows were returned<br/>
  4. anand: scientist<br/>
  5. 4 rows were returned<br/>
  6. ramesh: jrf<br/>
  7. 4 rows were returned<br/>
  8. ren: trainee<br/>
  9. 4 rows were returned<br/>
  10. Disconnected<br/>
So your MySQL connection is working correctly and the problem is in your CGI code. There must be some way to tell it where to direct the output that is being sent to stdout, but I don't know cgi.
Jan 19 '07 #15
Ok barton..No problem....
Jan 19 '07 #16
cuties
8
in apache there is a folder called "www"....for me i save my html page there and under this html page there should be a form such as like this:

Expand|Select|Wrap|Line Numbers
  1. <form action='http://username/cgi-bin/cgi-script' method ='post/get'>
  2.  
where the cgi-script is the cgi you wrote for your html.... did you configured your apache server? cgi script will be stored in the cgi-bin folder.

with this it should work since your database has no prob.... try thing.
Jan 22 '07 #17
cuties
8
sorry there was an error in the previous wan...i hv corrected it here....

in apache there is a folder called "www"....for me i save my html page there and under this html page there should be a form such as like this:

Expand|Select|Wrap|Line Numbers
  1. <form action='http://host/cgi-bin/cgi-script' method ='post/get'>
  2.  
where the cgi-script is the cgi you wrote for your html.... did you configured your apache server? cgi script will be stored in the cgi-bin folder.

with this it should work since your database has no prob.... try thing.
Jan 22 '07 #18
Did you configure Apache yourself as well? Out of the box it probably doesn't recognize a .py as cgi script unless you got some special build.

I only ask because you mentioned the output was "just plain" in a previous post. This thread is rather old so I will wait before explaining >.<
Feb 14 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Bill Eldridge | last post by:
I'm trying to grab a document off the Web and toss it into a MySQL database, but I keep running into the various encoding problems with Unicode (that aren't a problem for me with GB2312, BIG 5,...
2
by: Josh | last post by:
Hi, First off, I am a newbie to Python and so far I must say that we're getting along pretty well. My schooling was is C/C++, but have been writing code in, dare I say it, VB, for the past 4...
8
by: Alec Wysoker | last post by:
I need to be able to access mySQL 4.0 and 4.1 databases from python. I was hoping to find mysql-python 1.2.0 already built for Sparc, but no such luck. I've been struggling trying to get it...
11
by: grumfish | last post by:
I'm trying to add a row to a MySQL table using insert. Here is the code: connection = MySQLdb.connect(host="localhost", user="root", passwd="pw", db="japanese") cursor = connection.cursor()...
2
by: francescomoi | last post by:
Hi. I'm trying to build 'MySQL-python-1.2.0' on my Linux FC2: ---------------------------------- # export PATH=$PATH:/usr/local/mysql/bin/ # export mysqlclient=mysqlclient_r # python setup.py...
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
2
by: Roopesh | last post by:
Hi, In my mod_python project I am using mysql as the database. There is table card in which unique cards are stored. When a user request comes he has to get a unique card. In this situation I want...
4
by: coldpizza | last post by:
Hi, I want to run a database query and then display the first 10 records on a web page. Then I want to be able to click the 'Next' link on the page to show the next 10 records, and so on. My...
3
by: Andrey | last post by:
Hi just a quick question about using MySQL module... are there any api / class available to give a higher level in working with Mysql in python? such as db.fetch_array(), db.fetch_rows(),...
3
by: marexposed | last post by:
I've got this error (see the path in last line) db=MySQLdb.connect(host='localhost',use_unicode = True, charset = "Windows-1251",user='root',passwd='12',db='articulos') File...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.