0

I tried using Flask and SQLite. The python code is given below. When the code is executed. It accepts one request and successfully inserts it and throws error for the rest of the requests.

I tried the SQLite code like this

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")

and it successfully inserted 3 records but same using flask is not woking.

This is the actual code for flask

Python Code

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)

Error

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
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46

2 Answers2

0

You need to close connection object not cursor object, didn't close connection object that's why database is locked. conn.close() instead of cursor.close()

0

what if just use sqlalchemy? ORM seems easier and clearer to see but also safer db.session.add(Table(info = info )) db.session.commit() i found it easier than using cursor and all that

lycheelichi
  • 185
  • 1
  • 2
  • 9
  • 3
    Your post has more questions than statements and no explanation of the code found in between. Are you actually trying to answer the question at the top of this page or trying to ask a new one? As it is your post risks being considered "not an answer", which could get it deleted. – Yunnosch May 03 '21 at 14:17