I
I
Ingvar Von Bjork2019-12-13 13:00:27
Python
Ingvar Von Bjork, 2019-12-13 13:00:27

How to fix error when using OR and custom function?

Function

def lower_string(_str):
    return _str.lower()

I add to the database and execute the request
con.create_function("LOWER", 1, lower_string)
SELECT * FROM incoming WHERE lower("index") LIKE lower("%иванов%")

The function works fine, but as soon as I add OR
SELECT * FROM incoming WHERE lower("index") LIKE lower("%иванов%") OR
lower("num_incoming") LIKE lower("%иванов%")

then an error occurs immediately.
sqlite3.OperationalError: user-defined function raised exception

Why does the error occur only if there is an OR, and how can it be fixed?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ingvar Von Björk, 2019-12-13
@DeboshiR

Everything turned out to be quite obvious: Since not all fields in the table were of string type, the lower() function could not process them. To fix the error, I simply added a conversion to the string:

def lower_string(_str):
    return str(_str).lower()

V
Vadim Shatalov, 2019-12-13
@netpastor

Why write your own function when there is a built-in one?
https://www.w3resource.com/sqlite/core-functions-l...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question