0

Below Code is my CGI Script, where am trying to do a insert Command.

#! C:\Python27\python.exe -u

import cgi
import MySQLdb
import xml.sax.saxutils


query = cgi.parse()
db = MySQLdb.connect(


host = "127.0.0.1",
user = "root",
passwd = "mysql123",
db = "welcome")

print 'Content-type: text/plain\n\n<?xml version="1.0" encoding="utf-8"?>\n<result>'

try:
c = db.cursor()
c.execute("insert into welcome.registrations values ('test','test',now())")
print '\t<update>true</update>'
except:
print '\t<update>false</update>'


print "</result>"

when i run the go to the url - .com/cgi-bin/reg.cgi, am not finding any insert operation done in mysql DB

3
  • Are you seeing this print "</result>" getting printed Commented Apr 6, 2013 at 7:59
  • I meant there is some problem with your apache configuration Commented Apr 6, 2013 at 8:00
  • You might look into using a web lightweight framework such as flask or bottle. Using cgi seems like a massive headache. Commented Apr 6, 2013 at 8:06

2 Answers 2

1

You need to do db.commit() after c.execute().

Or you could do:

with db:
    c = db.cursor()
    c.execute("insert into welcome.registrations values ('test','test',now())")
Sign up to request clarification or add additional context in comments.

Comments

0

make sure you do a db.commit() after every insert (or in the end, any will work ):

try:
c = db.cursor()
c.execute("insert into welcome.registrations values ('test','test',now())")
db.commit()
db.close() # Make sure you close the connection else multiple connections will result in hanging of mysql server. 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.