sqlite 更新值时无法打开数据库

hfsqlsce  于 2023-08-06  发布在  SQLite
关注(0)|答案(1)|浏览(162)

我在Dash中展示了一个数据表,其中的内容是使用SQLAlchemy从SQLite数据库中检索的。我在数据表上设置了复选框和一个更新该行的按钮(例如,一个名为status的列,我将其中的值从“RESOURCE”更改为“FINISHED”)。但是,要更新的回调函数无法打开数据库:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file [SQL: UPDATE users SET status = ? where eventid = ?] [Parameters: ('FINISHED', 8)]

字符串
布局元素:

html.Button('Set Event Complete', id="complete-event"),
html.Div(id='output-message'),
html.Div(dash_table.DataTable(  # Datatable that includes the data from the data gathering method
    id='datatable-paging-filtering',    
    columns=[
        {"name": i, "id": i} for i in df.columns],    
    data=df.to_dict("records"),    
    page_size=PAGE_SIZE,    
    page_current=0,
    filter_action='native',
    sort_action='native',
    sort_mode='multi',
    column_selectable="single",
    row_deletable=True,
    row_selectable="multi",
    selected_columns=[],
    selected_rows=[]
))


回调函数:

@app.callback(
    Output('output-message', 'children'),
    [Input('complete-event', 'n_clicks')],
    [State('datatable-paging-filtering', 'data'),
     State('datatable-paging-filtering', 'selected_rows')]
)
def update_database(n_clicks, rows, selected_rows):
    if n_clicks is None:
        return json.dumps('')
    else:
        for i in selected_rows:
            rows[i]['status'] = 'FINISHED'
                
            conn = engine.connect()
            stmt = text("UPDATE users SET status = :status WHERE eventid = :eventid")
            conn.execute(stmt, {"status": 'FINISHED', "eventid": rows[i]['eventid']})
            conn.commit()
            conn.close()
        return json.dumps('Event status updated successfully!')


数据库配置:

server = Flask(__name__)
app = dash.Dash(__name__, server=server)
PAGE_SIZE = 15    

server.config.update(
    SECRET_KEY=os.urandom(12),
    SQLALCHEMY_DATABASE_URI='sqlite:///C:\\Users\\XXXXXXX\\OneDrive - XXX XXXX XXX\\Documents\\Dashtesting\\db2.sqlite',
    SQLALCHEMY_TRACK_MODIFICATIONS=False
)

engine = create_engine('sqlite:///C:\\Users\\XXXXXXX\\OneDrive - XXX XXX XXX\\Documents\\Dashtesting\\db2.sqlite', echo=True)


目标是实际更新数据库。我能够从数据库中检索数据,但在尝试更新数据库时出现问题。

mzmfm0qo

mzmfm0qo1#

我会尝试将数据库移动到其他地方,以查看路径是否导致问题。问题可能是文件夹名包含空格,或者flask/python没有对父文件夹的写访问权限。See if these tips help的数据。

相关问题