mysql - python cgi connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudhakaranr
    New Member
    • Dec 2006
    • 24

    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.
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by sudhakaranr
    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

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by sudhakaranr
      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.

      Comment

      • cuties
        New Member
        • Jan 2007
        • 8

        #4
        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.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by cuties
          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.

          Comment

          • sudhakaranr
            New Member
            • Dec 2006
            • 24

            #6
            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..

            Code:
            #!C:\Python25\python.exe
            
            print "Content-type: text/html\n\n"
            import sys
            import MySQLdb
            
            try:
            	conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
            	print "Connected<br/>"
            except:
            	print "Cannot connect to server.</br>"
            	sys.exit(1)
            
            try:
                            cursor = conn.cursor()
            	cursor.execute("SELECT * FROM sudha");
            	rows = cursor.fetchall()
            	for row in rows:
            		print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
            		print "%d rows were returned<br/>" % cursor.rowcount
            		cursor.close()
            except MySQLdb.Error, e:
            	print "query failed<br/>"
            	print e
            
            conn.close()
            print "Disconnected<br/>"
            Last edited by bartonc; Jan 19 '07, 05:16 AM. Reason: added [code][/code] tags

            Comment

            • sudhakaranr
              New Member
              • Dec 2006
              • 24

              #7
              sorry, now it's in code tag:-

              Code:
              #!C:\Python25\python.exe
              
              print "Content-type: text/html\n\n"
              import sys
              import MySQLdb
              
              try:
              	conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
              	print "Connected<br/>"
              except:
              	print "Cannot connect to server.</br>"
              	sys.exit(1)
              
              try:
                      cursor = conn.cursor()
              	cursor.execute("SELECT * FROM sudha");
              	rows = cursor.fetchall()
              	for row in rows:
              		print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
              		print "%d rows were returned<br/>" % cursor.rowcount
              		cursor.close()
              except MySQLdb.Error, e:
              	print "query failed<br/>"
              	print e
              
              conn.close()
              print "Disconnected<br/>"
              sys.exit(0)

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by sudhakaranr
                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..

                Code:
                #!C:\Python25\python.exe
                
                print "Content-type: text/html\n\n"
                import sys
                import MySQLdb
                
                try:
                	conn = MySQLdb.connect(host="localhost",user="root",passwd="sudhakar",db="test");
                	print "Connected<br/>"
                except:
                	print "Cannot connect to server.</br>"
                	sys.exit(1)
                
                try:
                                cursor = conn.cursor()
                	cursor.execute("SELECT * FROM sudha");
                	rows = cursor.fetchall()
                	for row in rows:
                		print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
                		print "%d rows were returned<br/>" % cursor.rowcount
                		cursor.close()
                except MySQLdb.Error, e:
                	print "query failed<br/>"
                	print e
                
                conn.close()
                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?

                Comment

                • sudhakaranr
                  New Member
                  • Dec 2006
                  • 24

                  #9
                  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....

                  Code:
                  try:
                                  cursor = conn.cursor()
                  	cursor.execute("SELECT * FROM sudha");
                  	rows = cursor.fetchall()
                  	for row in rows:
                  		print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
                  		print "%d rows were returned<br/>" % cursor.rowcount
                  		cursor.close()
                  except MySQLdb.Error, e:
                  	print "query failed<br/>"
                  	print e

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by sudhakaranr
                    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?

                    Comment

                    • sudhakaranr
                      New Member
                      • Dec 2006
                      • 24

                      #11
                      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.

                      Code:
                      #!C:\Python25\python.exe
                      print "Content-Type: text/html\n\n"
                      print "hello"
                      like that..i am testing to connect the mysql...

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by sudhakaranr
                        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....

                        Code:
                        try:
                                        cursor = conn.cursor()
                        	cursor.execute("SELECT * FROM sudha");
                        	rows = cursor.fetchall()
                        	for row in rows:
                        		print "%s: %s, %s<br/>" % (row[0],row[1],row[2])
                        		print "%d rows were returned<br/>" % cursor.rowcount
                        		cursor.close()
                        except MySQLdb.Error, e:
                        	print "query failed<br/>"
                        	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?

                        Comment

                        • bartonc
                          Recognized Expert Expert
                          • Sep 2006
                          • 6478

                          #13
                          Originally posted by sudhakaranr
                          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.

                          Code:
                          #!C:\Python25\python.exe
                          print "Content-Type: text/html\n\n"
                          print "hello"
                          like that..i am testing to connect the mysql...
                          Did you see this thread?

                          Comment

                          • sudhakaranr
                            New Member
                            • Dec 2006
                            • 24

                            #14
                            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.

                            Code:
                            Connected<br/>
                            sudhakar: srf<br/>
                            4 rows were returned<br/>
                            anand: scientist<br/>
                            4 rows were returned<br/>
                            ramesh: jrf<br/>
                            4 rows were returned<br/>
                            ren: trainee<br/>
                            4 rows were returned<br/>
                            Disconnected<br/>

                            Comment

                            • bartonc
                              Recognized Expert Expert
                              • Sep 2006
                              • 6478

                              #15
                              Originally posted by sudhakaranr
                              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.

                              Code:
                              Connected<br/>
                              sudhakar: srf<br/>
                              4 rows were returned<br/>
                              anand: scientist<br/>
                              4 rows were returned<br/>
                              ramesh: jrf<br/>
                              4 rows were returned<br/>
                              ren: trainee<br/>
                              4 rows were returned<br/>
                              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.

                              Comment

                              Working...