Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Python functions in a SQLite table?

Tags:

python

sqlite

I am creating a simple network monitoring web application in Python.

Given:

  • I want to manage everything from the GUI. No requirement post-install to touch files on the host server.
  • I want to be able to define custom checks in Python, and store those functions/checks in a SQLite database.

Questions:

  • How should I store that textarea (python function) in the SQLite database?

  • How do I call that function from another python script after I've pulled it out of the SQLite database?

like image 290
Luke has no name Avatar asked Jan 27 '26 03:01

Luke has no name


1 Answers

SQLite TEXT field is fine for that. And you can call function from the code like this:

>>> code = """
... def hello():
...     print 'Hello world'
... """
>>> exec code
>>> hello()
Hello world

But I suggest you to save the code on disk with random filename. Then filename can be saved in database and code can be used like a normal module.

like image 128
Igonato Avatar answered Jan 28 '26 17:01

Igonato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!