ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 14408 and this is thread id 2776

Hi @Elijah_Nkuah - A few issues I think:

  1. Youโ€™re reusing the cursor across separate database calls.
  2. Iโ€™d run execute() on conn, not c. conn.execute() returns a c to fetch results.
  3. Always commit after a logically complete set of update operations to make them atomic.
  4. Create the conn object as a singleton so it survives Streamlit reruns (can use session state or wrap it in st.cache with a custom hash function for sqlite3.Connection).

As a quick fix try:

conn = sqlite3.connect("usersdata.db", check_same_thread=False)

(Warning, your user signup implementation may trip up if two users try to create the same login credentials simultaneously, and your db scheme allows duplicate users. Hashing the password isnโ€™t secure. It needs to be encrypted with a secret key.)

You could use my simple auth solution instead of dealing with all these issues yourself. :slight_smile:

HTH,
Arvindra

1 Like