Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error for cursor.connection(return not async connection) #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ArtemIsmagilov
Copy link

@ArtemIsmagilov ArtemIsmagilov commented May 2, 2024

await cur.connection.commit()
          ^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 126329731884608 and this is thread id 126329755602944.

Description

method connection for cursor return not async connection

Fixes: # replace on async connection self._conn

```text
await cur.connection.commit()
          ^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 126329731884608 and this is thread id 126329755602944.
```
@ArtemIsmagilov
Copy link
Author

ArtemIsmagilov commented May 2, 2024

my usecase, where was error.

from contextlib import asynccontextmanager

import aiosqlite
from aiosqlite import Cursor

from settings import conf


@asynccontextmanager
async def get_db() -> Cursor:
    async with aiosqlite.connect(conf.DATABASE_URL) as conn:
        conn.row_factory = aiosqlite.Row
        if conf.SQLITE3_ECHO:
            await conn.set_trace_callback(print)
        async with conn.cursor() as cur:
            yield cur
import asyncio
from sql_app.database import get_db


async def main():

    async with get_db() as cur:
        with open("sql_app/sql_files/create_table_cities.sql") as f:
            data = f.read()
        await cur.executescript(data)
        await cur.connection.commit()


if __name__ == "__main__":
    asyncio.run(main())

result:

await cur.connection.commit()

          ^^^^^^^^^^^^^^^^^^^^^^^

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant