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

It is not possible to use register_converter #240

Open
Pankovea opened this issue May 24, 2023 · 3 comments
Open

It is not possible to use register_converter #240

Pankovea opened this issue May 24, 2023 · 3 comments

Comments

@Pankovea
Copy link

Description

In sqlite3 I can register converters to get correct data types.

import sqlite3
sqlite3.register_adapter(bool, int) # <-------
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))  # <-------

db = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES)
db.execute('CREATE TABLE foo (bar BOOLEAN)')
db.execute('INSERT INTO foo VALUES (?)', (True,))
db.execute('INSERT INTO foo VALUES (?)', (False,))
for row in db.execute('SELECT * FROM foo'):
    print(row)
Output:
(True,)
(False,)

I can't use register_converter

aiosqlite.register_adapter(bool, int)
aiosqlite.register_converter("BOOLEAN", lambda v: bool(int(v)))

Thats take no effect

Output:
(1,)
(0,)

Details

  • OS: Win10
  • Python version: 3.11
  • aiosqlite version: 0.19.0
@wreimers
Copy link

I'm still hitting this. Exact same scenario.

  • OS: macOS 14.5
  • Python version: 3.12.4
  • aiosqlite version: 0.20.0

@wreimers
Copy link

wreimers commented Jul 17, 2024

I was able to work around this:

sqlite3.register_adapter(bool, int)
sqlite3.register_converter("BOOLEAN", lambda v: bool(int(v)))

connection = await aiosqlite.connect(DATABASE, detect_types=sqlite3.PARSE_DECLTYPES)

Note that I had to use sqlite3 to set up the converter, and to specify the detect_types argument.

@makarr
Copy link

makarr commented Oct 30, 2024

@wreimers Thanks for sharing the workaround. It's odd that aiosqlite.register_adapter seems to work but the converter does not.

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

No branches or pull requests

3 participants