0

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])
nio
  • 3
  • 3

1 Answers1

0

Notice in the example from the doc that it is sending b"foo" to the function (see this post for more info). Try casting name as BLOB in the SELECT and see if that changes the result. ie md5(CAST (name as BLOB))

DinoCoderSaurus
  • 6,110
  • 2
  • 10
  • 15