简体   繁体   中英

Flask-SQLAlchemy Gives Unknown Error

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

确保运行Flask脚本的用户有权在/tmp/test.db读写,并且'sqlite:////tmp/test.db'似乎没有多余'sqlite:////tmp/test.db'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM