SQLite插入语句使用Flask无声地失败

com*_*ted 3 python sqlite flask

我正在关注Flask网站上的Flask/SQLite教程:http: //flask.pocoo.org/docs/patterns/sqlite3/

我所有的选择查询都运行正常,但是当我尝试插入语句时没有任何反应.我没有错误,但我也没有在我的数据库中插入一行.我不确定如何在没有错误发生时调试它.有人有想法吗?

这是我的设置代码:

def connect_db():
    return sqlite3.connect(DATABASE)

@application.before_request
def before_request():
    g.db = connect_db()

@application.teardown_request
def teardown_request(exception):
    if hasattr(g, 'db'):
        g.db.close()

def query_db(query, args=(), one=False):
    cur = g.db.execute(query, args)
    rv = [dict((cur.description[idx][0], value)
               for idx, value in enumerate(row)) for row in cur.fetchall()]
    return (rv[0] if rv else None) if one else rv
Run Code Online (Sandbox Code Playgroud)

以下是一个有效的查询示例:

@application.route('/api/galleries')
def get_galleries():
    query = query_db('select id, title, thumbnail from galleries')
    return json.dumps(query)
Run Code Online (Sandbox Code Playgroud)

这是我的插入声明.这个没有做任何事情,没有错误:

g.db.execute('insert into photos (is_favorite, galleries_id, filename) values (?, ?, ?)', [is_favorite, galleries_id, filename])
Run Code Online (Sandbox Code Playgroud)

小智 11

你是在INSERT之后提交的吗?

g.db.execute('insert into photos (is_favorite, galleries_id, filename) values (?, ?, ?)', [is_favorite, galleries_id, filename])
g.db.commit()
Run Code Online (Sandbox Code Playgroud)

  • 我怀疑可能是这种情况,但我找不到它的文档.我会放手一搏. (2认同)