首页 > 解决方案 > 如何通过python cgi将表形式的值保存到sqlite db中?

问题描述

我有一个如下表:

<form action = "get.py" method = "get">
First Name: <input type = "text" name = "first_name">  <br />

Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>

我试图通过 python cgi 传递值,(centos + apache + cgi)

#!/usr/bin/python                                                         

# Import modules for CGI handling                                         
import cgi, cgitb                                                         

# Create instance of FieldStorage                                         
form = cgi.FieldStorage()                                                 

# Get data from fields                                                    
tt = form.getvalue('first_name')                                           
cc = form.getvalue('last_name')                                            

import sqlite3                                                            
conn = sqlite3.connect('personal.db')                                     
c = conn.cursor()                                                         
sql = "INSERT INTO PQ VALUES ('%s', '%s', Datetime('now'), '0')" % (tt, cc) 
c.execute(sql)                                                            
c.close()      

分贝如下:

sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE PQ (Title TEXT, Description TEXT, Time TEXT, Deleted TEXT);
INSERT INTO PQ VALUES('biochem','how to do learning path','2018-05-16 03:55:54','')
COMMIT;
sqlite> .q

当我尝试提交数据时,cgi 返回如下错误:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

我应该怎么做才能解决这个问题?是否缺少任何配置?

标签: pythonhtmlapachegetcgi

解决方案


推荐阅读