I am writing a simple app to learn Flask and SQLAlchemy basics. Currently, I have defined my database, and without the SQLAlchemy component (no data storage), the app works fine.
The app simply takes an int in the URL and returns the doubled value, while storing the value passed by the user along with a timestamp in the database.
Currently, when I make the query to update the database and return the new value, I get a 500 error on the db.session.commit()
line.
Full code:
from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class Call(db.Model):
id = db.Column(db.Integer, primary_key=True)
call_value = db.Column(db.Integer)
call_time = db.Column(db.Integer)
def __init__(self,user_input):
self.call_value = user_input
self.call_time = datetime.datetime.now()
def __repr__(self):
return '<Doubled %r>' % self.call_value
@app.route('/doubler/<int:val>', methods=['GET'])
def show_double(val):
query = Call(val)
db.session.add(query)
db.session.commit()
return "Value: " + str(2*val)
if __name__ == "__main__":
app.run()
I have two hunches on why this might be happening: either some issue with getting and formatting the datetime, or the primary key generation needs to be done manually. However, everything I've seen would indicate that neither of these are issues. What needs to be done to commit to my database? Should I be doing all of the database work in a separate file?
Edit:
Error from debugging is:
OperationalError: (sqlite3.OperationalError) unable to open database file