Store Python Functions in a SQLite Table



In Python, you can store a function as a text string in a SQLite database using the sqlite3 module. You cannot run the function directly from the database, but you can retrieve the code later and execute it using Python's exec() or eval() functions.

This is helpful when you want to store or update function logic, like in code editors, plugins, or apps that run user-created scripts.

Using sqlite3 and exec() Function

The sqlite3 module is used to create and work with SQLite databases in Python. The exec() function can run Python code that is stored as a string, including function definitions.

Syntax

Following is a basic syntax for creating a table and inserting a function's code into it -

cursor.execute("CREATE TABLE IF NOT EXISTS functions (name TEXT, code TEXT)")
cursor.execute("INSERT INTO functions (name, code) VALUES (?, ?)", (func_name, func_code))

Return Value

The exec() function doesn't return anything (it returns None), but it runs the function code inside the string. If the string contains a function definition, that function becomes usable in your Python program after exec() is called.

Example

This example shows how to store a Python function as a string in a SQLite database and later retrieve and run it using the exec() function -

import sqlite3

# Connect to SQLite database
conn = sqlite3.connect("functions.db")
cursor = conn.cursor()

# Create a table for the functions
cursor.execute("CREATE TABLE IF NOT EXISTS functions (name TEXT, code TEXT)")

# Define a function as a string
func_name = "greet"
func_code = """
def greet(name):
   return f"Hello, {name}!"
"""

# Store the function in the database
cursor.execute("INSERT INTO functions (name, code) VALUES (?, ?)", (func_name, func_code))
conn.commit()

# Retrieve the function
cursor.execute("SELECT code FROM functions WHERE name = ?", (func_name,))
fetched_code = cursor.fetchone()[0]

# Execute the retrieved function
exec(fetched_code)
print(greet("Rohan"))

conn.close()

We get the following output -

Hello, Rohan

Handling exec() Errors in SQLite

When you use the exec() function to run code stored in a database, you may come across errors if the code is incorrect or missing. Here are some common errors found -

  • SyntaxError: This happens if the string you are trying to execute has invalid Python code. For example, a missing colon or indentation error will cause this.
  • TypeError: This can occur if you try to call the function before it is properly defined or if the database returns None instead of a string.
  • AttributeError: You might see this error if you assume a function exists but it was never defined due to incorrect code or failed execution.

To avoid these errors, always validate your function string before executing it, and use try...except blocks to catch possible errors during execution.

Example

In the following example, we are using the exec() function to run a string containing Python code with incorrect indentation inside a try-except block. Since the return statement is not properly indented, it raises a SyntaxError -

bad_code = "def greet(name):\nreturn 'Hi ' + name"  # Improper indentation

try:
   exec(bad_code)
except Exception as e:
   print("Error:", e)

Following is the error obtained -

Error: expected an indented block after function definition on line 1 (<string>, line 2)

Storing Multiple Functions

You can store more than one function in a single SQLite table by assigning each function a unique name along with its code. This makes it easy to organize and reuse different pieces of logic. To use a specific function later, simply search for it by name, fetch its code, and use the exec() function to define it in your program.

Example

In the example below, we are storing multiple function definitions as strings in a database and later retrieving them by their respective names. The retrieved function code is executed using exec() to dynamically define and use the cube() function -

import sqlite3

# Connect to in-memory database
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()

# Create table
cursor.execute("CREATE TABLE functions (name TEXT, code TEXT)")

# List of functions as (name, code) tuples
functions = [
   ("square", "def square(n):\n   return n * n"),
   ("cube", "def cube(n):\n   return n * n * n")
]

# Insert multiple functions
cursor.executemany("INSERT INTO functions (name, code) VALUES (?, ?)", functions)
conn.commit()

# Fetch and use cube function
cursor.execute("SELECT code FROM functions WHERE name = 'cube'")
exec(cursor.fetchone()[0])
print(cube(3)) 

We get the result as shown below -

27

Using the "dill" Module to Store Functions

If you want to store the entire function object, including its behavior, variables, and even decorators, you can use the dill module. The dill module allows you to serialize (convert) the whole function into binary data that can be saved and later restored exactly as it was.

Example

This example shows how to use the "dill" module to serialize a Python function, store it in a SQLite database, and later retrieve and execute it -

import dill
import sqlite3

conn = sqlite3.connect("functions.db")
cursor = conn.cursor()

cursor.execute("CREATE TABLE IF NOT EXISTS serialized_functions (name TEXT, code BLOB)")

def add(a, b):
   return a + b

# Serialize the function
serialized_code = dill.dumps(add)
cursor.execute("INSERT INTO serialized_functions (name, code) VALUES (?, ?)", ("add", serialized_code))
conn.commit()

# Retrieve and deserialize
cursor.execute("SELECT code FROM serialized_functions WHERE name = 'add'")
retrieved_func = dill.loads(cursor.fetchone()[0])
print(retrieved_func(5, 3))

We get the output as shown below -

8
Updated on: 2025-05-07T16:33:25+05:30

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements