0

下面的代码是我的 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 中找到任何插入操作

4

2 回答 2

1

你需要做db.commit()之后c.execute()

或者你可以这样做:

with db:
    c = db.cursor()
    c.execute("insert into welcome.registrations values ('test','test',now())")
于 2013-04-06T08:01:21.723 回答
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. 
于 2013-04-06T09:08:36.427 回答