无法使用Flask将数据插入数据库

时间:2017-03-11 08:47:30

标签: python sqlite flask

我尝试使用Flask和SQLite。 python代码如下。代码执行时。它接受一个请求并成功插入它,并为其余请求抛出错误。

我尝试了像这样的SQLite代码

def insertData(name, email, phone, college):
    conn = sqlite3.connect('connect.sqlite')
    cursor = conn.cursor()
    cursor.execute('''INSERT INTO user ('Name','Email','MobileNumber','College') VALUES(?,?,?,?)''', (name, email, phone, college))
    conn.commit()
    cursor.close()

insertDate("A", "a@gmail.com", "9999999999", "ABC")
insertDate("B", "b@gmail.com", "9999999998", "DEF")
insertDate("C", "c@gmail.com", "9999999997", "GHI")

并成功插入了3条记录,但使用烧瓶的情况并非如此。

这是烧瓶的实际代码

Python代码

from flask import Flask
from flask import request
import sqlite3, json
import wsgiserver

app = Flask(__name__)

@app.route('/connect/register', methods=['POST'])
def register():
    name = request.form.get('name')
    college = request.form.get('college')
    email = request.form.get('email')
    phone = request.form.get('phone')
    conn = sqlite3.connect('connect.sqlite')
    cursor = conn.cursor()
    cursor.execute('''INSERT INTO user ('Name','Email','MobileNumber','College') VALUES(?,?,?,?)''', (name, email, phone, college))
    conn.commit()
    cursor.close()
    return json.dumps(request.form)

if __name__ == '__main__':
    app.run(host='192.168.0.2',port=8080,debug=True)

错误

Traceback (most recent call last):
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/debugger24/anaconda/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/debugger24/Development/ConnectAPI/main.py", line 26, in register
    insertDate(name, email, phone, college)
  File "/Users/debugger24/Development/ConnectAPI/main.py", line 15, in insertDate
    cursor.execute('''INSERT INTO user ('Name','Email','MobileNumber','College') VALUES(?,?,?,?)''', (name, email, phone, college))
sqlite3.OperationalError: database is locked

2 个答案:

答案 0 :(得分:0)

您需要关闭连接对象而不是游标对象,没有关闭连接对象,这就是数据库被锁定的原因。 conn.close()而不是cursor.close()

答案 1 :(得分:0)

如果只使用 sqlalchemy 呢? ORM 看起来更容易、更清晰,但也更安全 db.session.add(表(信息=信息)) db.session.commit() 我发现它比使用游标更容易