2013-04-06 121 views
0

下面的代碼是我的CGI腳本,我試圖做一個插入命令。MYSQL 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>" 
當我運行轉至該網址

- .COM/cgi-bin目錄/ reg.cgi,我沒有找到在MySQL數據庫

+0

是你看到這 打印「」正在打印 – 2013-04-06 07:59:08

+0

我的意思是你的apache配置存在一些問題 – 2013-04-06 08:00:03

+0

你可能會考慮使用web輕量級框架,如[flask](http://flask.pocoo.org)或[bottle](http ://bottlepy.org/)。使用CGI似乎是一個巨大的頭痛。 – 2013-04-06 08:06:55

回答

1

做任何插入操作需要c.execute()後做db.commit()

或者你可以這樣做:

with db: 
    c = db.cursor() 
    c.execute("insert into welcome.registrations values ('test','test',now())") 
+0

非常感謝你Nathan,我做的是一個愚蠢的錯誤 – user2104391 2013-04-06 08:04:29

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.