0
我做功課的數據庫:解析URL並插入
http://zetcode.com/db/sqlitepythontutorial/
http://www.kitebird.com/articles/pydbapi.html (及以上)
目標:
當do_POST
方法被調用時,我想解析URL並將其存儲到數據庫中。
代碼:
import SimpleHTTPServer
import SocketServer
import urlparse
import sqlite3 as lite
# Otherwise, we get some weird threading issue.
con = lite.connect('storage.db',check_same_thread = False)
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Events")
cur.execute("CREATE TABLE Events(wtype TEXT, wtId INT, actiong TEXT)")
#
def do_PUT(self):
s = urlparse.urlparse(self.path)
#print s
a = s.path.split('/')
print a[1]
print a[2]
print a[3]
##(this didn-t work, so I tried putting it in a try-except block and adding connect statements:
##cur.execute("INSERT INTO Events VALUES(?, ?, ?)",(a[1],a[2],a[3]))
try:
con = lite.connect('storage.db',check_same_thread = False) cur = con.cursor()
cur.execute("INSERT INTO Events VALUES(?, ?, ?)",("HELLO", 5, "friend")
con.commit()
except lite.Error, e:
print "error"
finally:
print "done"
錯誤:
cun-80-147:newclone curtisservaas1$ python mainserver.py
File "mainserver.py", line 48
con.commit()
^
SyntaxError: invalid syntax
cun-80-147:newclone curtisservaas1$ "
展望未來:
我想能夠自動遞增我做的條目但首先,我需要能夠實際輸入數據庫。
Meta: 我在這裏沒有問太多的問題,所以對如何編寫更好的問題的建議表示讚賞。 Crocker's rules applies。我認爲標題可以改進(這也將幫助我找出如何找到類似的問題)。