How to store Python functions in a Sqlite table?
Last Updated :
01 Oct, 2020
SQLite is a relational database system contained in a C library that works over syntax very much similar to SQL. It can be fused with Python with the help of the sqlite3 module. The Python Standard Library sqlite3 was developed by Gerhard Häring. It delivers an SQL interface compliant with the DB-API 2.0 specification described by PEP 249.
To use this module and establish the connection with database:-
1. Import sqlite3 module and create the object representing the database.
import sqlite3
conn = sqlite3.connect('function.db')
2. To execute SQL commands, construct a cursor object and call the execute() method.
c = conn.cursor()
c.execute('''CREATE TABLE student
(Number REAL,stud_name TEXT)''')
Note: In Order to track the operations on a database level we need to download the SQLite browser database.
Steps to store Python function in SQLite table:
- Step 1) Import sqlite3 and create the object of database .
Python3
import sqlite3
conn = sqlite3.connect( 'function.db' )
|
Here we have imported sqlite3 module, and We have initialized the database function.db. This instruction will create the database if the database doesn’t exist and if the database having the same name as defined exist than it will move further.
- Step 2) Create a Cursor object and call its execute() method to create table.
Python3
c = conn.cursor()
c.execute(
)
|
After initializing the database we have to create the cursor object using conn.cursor(). The cursor allows us to execute the SQL commands.
- Step 3) Write Function in below syntax.
Since we have to store the function definition inside the table, we have to write function in the above syntax.
- Step 4) Insert function into pyfunction table.
Python3
c.execute( "INSERT INTO pyfuction (func_defination) VALUES (?)" ,
(code,))
conn.commit()
|
The SQL command “INSERT INTO pyfuction (func_defination) VALUES (?)”, (code) inserts the value of variable code inside the table pyfunction in the form of text. Then use the commit() method to save the changes.
Below are some examples store Python functions in a Sqlite table?
Example 1: Storing gfg() function in sqlite table named pyfunction.
Python3
import sqlite3
conn = sqlite3.connect( 'function.db' )
c = conn.cursor()
c.execute(
)
code =
c.execute( "INSERT INTO pyfuction (func_defination) VALUES (?)" ,
(code))
conn.commit()
|
Output:
Example 2: Here is another example of storing a function named add() in the pyfunction table.
Python3
import sqlite3
conn = sqlite3.connect( 'function.db' )
c = conn.cursor()
c.execute(
)
code =
c.execute( "INSERT INTO pyfuction (func_defination) VALUES (?)" ,
(code,))
c.execute( "SELECT * FROM pyfuction;" )
print (c.fetchall())
conn.commit()
|
Output:
Example 3: Storing two functions, maximum() and gfg() in pyfunction table.
Python3
import sqlite3
conn = sqlite3.connect( 'function.db' )
c = conn.cursor()
c.execute(
)
def1 =
c.execute( "INSERT INTO pyfuction (func_defination) VALUES (?)" ,
(def1,))
def2 =
c.execute( "INSERT INTO pyfuction (func_defination) VALUES (?)" ,
(def2,))
c.execute( "SELECT * FROM pyfuction;" )
print (c.fetchall())
conn.commit()
|
Output:
Share your thoughts in the comments
Please Login to comment...