Code下面是我的CGI脚本,我试图插入命令。
#! 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>"
当我运行转到url - .com / cgi-bin / reg.cgi时,我没有在mysql DB中找到任何插入操作
答案 0 :(得分:1)
您需要在db.commit()
后执行c.execute()
。
或者你可以这样做:
with db:
c = db.cursor()
c.execute("insert into welcome.registrations values ('test','test',now())")
答案 1 :(得分:0)
确保在每次插入后执行db.commit()(或者最后,任何都可以):
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.