我试图将数据从html表单写入Sqlite数据库(d1)。但是代码无法连接/创建数据库。我检查了Apache日志,没有错误。谁能帮助我为什么会这样?
Python代码(sc1.cgi):
#!/usr/bin/python
print ( "/nContent-Type: text/html\n\n" )
import cgi
import sqlite3
form = cgi.FieldStorage()
fn = form.getvalue('first_name')
ln = form.getvalue('last_name')
conn = sqlite3.connect('d1.db')
c = conn.cursor()
q = "CREATE TABLE IF NOT EXISTS t1 (first_name, last_name)"
c.execute(q)
q = "INSERT INTO t1 VALUES ('%s', '%s')" % (fn, ln)
c.execute(q)
conn.commit()
conn.close()
表格:
<body>
<form action = /path/to/sc1.cgi>
First Name: <input type = "text" name = "first_name"> <br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>