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.

@@ -106,3 +106,4 @@ ENV/

# Editors
.vscode/
.idea/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.idea/

@@ -108,8 +108,8 @@ def row_factory(self, factory: Optional[Type]) -> None:
self._cursor.row_factory = factory

@property
def connection(self) -> sqlite3.Connection:
return self._cursor.connection

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_cursor.connection

@@ -108,8 +108,8 @@ def row_factory(self, factory: Optional[Type]) -> None:
self._cursor.row_factory = factory

@property
def connection(self) -> sqlite3.Connection:
return self._cursor.connection
def connection(self) -> "Connection":

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" "

def connection(self) -> sqlite3.Connection:
return self._cursor.connection
def connection(self) -> "Connection":
return self._conn

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_conn

@@ -108,8 +108,8 @@ def row_factory(self, factory: Optional[Type]) -> None:
self._cursor.row_factory = factory

@property
def connection(self) -> sqlite3.Connection:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlite3.

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.

2 participants