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", "[email protected]", "9999999999", "ABC")
insertDate("B", "[email protected]", "9999999998", "DEF")
insertDate("C", "[email protected]", "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
3
  • Did you try the solutions provided here? Commented Mar 11, 2017 at 11:48
  • add your project structure with path to db please Commented Mar 11, 2017 at 16:45
  • db and program both are in same directory Commented Mar 11, 2017 at 19:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.