我使用Streamlit作為我的數(shù)據(jù)處理和可視化框架。按照關(guān)于使用 Streamlit 進行高級緩存的文檔,關(guān)于如何創(chuàng)建數(shù)據(jù)庫連接并將它們傳遞給其他函數(shù),我正在使用sqlite3并嘗試在運行時創(chuàng)建一個連接并傳遞“光標”,方法是:import streamlit as st# create database connection@st.cache(allow_output_mutation=True)def get_database_connection(): conn = sqlite3.connect('collect/Data/fpl.db') c = conn.cursor() return c在這里,我嘗試通過 傳遞我的連接 ID hash_funcs,返回創(chuàng)建的游標:# pass connections around@st.cache(allow_output_mutation=True, hash_funcs={sqlite3.Cursor:id})def get_teams_basic(c): df_teams = sql('SELECT * FROM TeamsBasic', c) return df_teams@st.cache(allow_output_mutation=True, hash_funcs={sqlite3.Cursor:id})def get_players_basic(c): df_player_basic = sql('SELECT * FROM PlayersBasic', c) return df_player_basic# and so on...這是我的sql():def sql(query, cursor): ''' Takes an SQL query string, and outputs a dataframe representation of the query result. ''' # Execute the sql query cursor.execute(query) # Get the query into a dataframe and set columns df_temp = pd.DataFrame(cursor.fetchall()) df_temp.columns = [x[0] for x in cursor.description] # Set the sql id as the dataframe index index_column = df_temp.columns[0] df_temp.set_index(index_column, drop=True, inplace=True) return df_temp然后,在 處main(),我實例化數(shù)據(jù)庫連接并將光標傳遞給我的函數(shù):def main(): c = get_database_connection() teams(c) players(c)if __name__ == '__main__': main()但是當(dāng)代碼到達第二個連接時,players()出現(xiàn)以下錯誤:ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 123145444151296 and this is thread id 123145454661632.我錯過了什么?
1 回答

心有法竹
TA貢獻1866條經(jīng)驗 獲得超5個贊
我不確定您是否仍在解決這個問題,但如果您還在,我發(fā)現(xiàn)了一個可能有用的關(guān)于該主題的討論線程。我很快就會在我的一個項目中試用它。
https://discuss.streamlit.io/t/prediction-analysis-and-creating-a-database/3504
添加回答
舉報
0/150
提交
取消