如何将Python函数存储在Sqlite表中?

如何将Python函数存储在Sqlite表中?

在下面的代码中,我们导入了sqlite3模块并建立了数据库连接。接着我们创建了一个表,并插入了数据,从sqlite3数据库中获取了信息并最终关闭了连接。

更多Python相关文章,请阅读:Python 教程

示例

#sqlitedemo.py
import sqlite3
from employee import employee
conn = sqlite3.connect('employee.db')
c=conn.cursor()
c.execute(‘’’CREATE TABLE employee(first text, last text, pay integer)’’’)
emp_1 = employee('John', 'Doe', 50000 )
emp_2 = employee('Jane', 'Doe', 60000)
emp_3 = employee('James', 'Dell', 80000)
c.execute(‘’’INSERT INTO employee VALUES(:first, :last,  
:pay)’’’,{'first':emp_1.first, 'last':emp_1.last,
'pay':emp_1.pay})
c.execute(‘’’INSERT INTO employee VALUES(:first, :last,
:pay)’’’,{'first':emp_2.first, 'last':emp_2.last,
'pay':emp_2.pay})
c.execute(‘’’INSERT INTO employee VALUES(:first, :last,
:pay)’’’,{'first':emp_3.first, 'last':emp_3.last,
'pay':emp_3.pay})
c.execute("SELECT * FROM employee WHERE last ='Doe'")
print(c.fetchone())
print(c.fetchmany(2))
conn.commit()
conn.close()

输出结果

(u'James', u'Dell', 80000)
[(u'James', u'Dell', 80000), (u'James', u'Dell', 80000)]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程