I'm working on a website powered by Python at the back end. The following script receives values from JavaScript and should write the same to a database.
import cgi
import sqlite3
form = cgi.FieldStorage()
brand = form['brand'].value
model = form['model'].value
db = sqlite3.connect("dbase.db")
query = "INSERT INTO requests (brand, model) VALUES (?, ?)"
db.execute(query, (brand, model,))
db.commit()
db.close()
But invoking the script returns 500 Internal Server Error
. To my surprise, the following code, run independently on terminal works perfect.
import sqlite3
brand = 'nokia'
model = 'lumia 625'
db = sqlite3.connect("dbase.db")
query = "INSERT INTO requests (brand, model) VALUES (?, ?)"
db.execute(query, (brand, model,))
db.commit()
db.close()
I'm using Python 2.7.5 and running on Lighttpd server. Also, the db.execute()
portion is where the error occurs. How can I correct the problem?
This might happen for not having database dbase
and table requests
.You need a database and table for inserting fields.You can create database with this query
sqlite3 dbase.db
And next you need to create a table for this like
create table requests(brand varchar(10), model varchar2(10));
Then your file will execute
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.