Trying to get the md5sum of a table field in sqlite3 using a User-defined function.
[Edit] The code shown below was tried after reading documentation at https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.create_function
On python 3.8, The following code throws sqlite3.OperationalError: user-defined function raised exception
How do I fix this ?
import sqlite3
import hashlib
def md5sum(text):
return hashlib.md5(text).hexdigest()
with sqlite3.connect(":memory:") as conn:
conn.executescript("""
CREATE TABLE project(name);
INSERT INTO project(name) VALUES ('test');
""")
conn.create_function("md5", 1, md5sum)
cursor = conn.cursor()
# cursor.execute("SELECT name from project")
cursor.execute("SELECT md5(name) from project") # Throws error
print(cursor.fetchone()[0])