Closed alexneufeld closed 1 year ago
Nothing to do with TypesConverterCursorFactory
which you can see if you comment out setting it as cursor_factory
.
Virtual tables are a SQLite thing which means they only operate on SQLite supported data types. I've changed the error message, so now you get this as the last line of the exception:
TypeError: Value from Python is not supported by SQLite. It should be one of None, int, float, str, or bytes. Received complex.
There is a repr_invalid
parameter to make_virtual_table which would have turned the complex number into the string '(3+4j)'
but that is repr output and not something the types converter would understand. The parameter is off by default because it is expensive checking every value in every column of every row returned.
You could change the yield line to this which would convert the value into something SQLite supports.
yield {"col1": registrar.adapt_value(table_data[c])}
But then on running you will get this where the values are the adapted ones (strings).
┌──────────┐
│ col1 │
│ 3.0+4.0 │
│ 8.0+-2.0 │
│ 1.0+-5.0 │
└──────────┘
The reason for that is the virtual table at the SQLite level does not have declared column types. Using apsw.ext.query_info will include this where the None
is the declared column type:
description=(('col1', None),),
In short, this is trying really hard to pretend SQLite supports COMPLEX but that has to be done everywhere. If the yield
line was changed and make_virtual_table
did declare the column at the SQLite level to have type COMPLEX
then it would work. But the latter is not something that will be changed in apsw because the library code is applicable to everyone.
You can however do it yourself. You can copy make_virtual_table
, and where it gets the function signature you have to look at the annotation and turn that into whatever string you want in the SQL that creates the virtual table.
Thinking about it a little more, I'd recommend you avoid make_virtual_table
and implement your own virtual tables. That will provide precise control over exactly what values are provided to SQLite, as well as the table and column declarations for the virtual table which are necessary for TypesConverterCursorFactory
to work as expected.
Will do. Thanks for your help!
I have this small script based on examples in the Example/Tour section of the documentation (1, 2)
it fails with the following error on my system:
Is there a simple solution to this error, or do I need to look further into the documentation for virtual tables?