利用 Python 实现针对 Sqlite3 数据库的操作,实现以下函数:
(1)初始化数据库:创建数据库文件、数据表
函数原型: def create_db(path)
参数 path:字符串,指明了数据库文件生成的位置。
在指定路径新建 Sqlite3 数据库,如果已经存在,则应首先删除原文件再创建。然后,
建立两张数据表,即 Person 表与 Position 表。
返回值:创建成功,返回 0;失败返回-1。
人员信息表 Person:
序号 | 字段名称 | 字段类型 | 取值范围 |
1 | 姓名 NAME | 字符串 | 32 字符 |
2 | 性别 GENDER | 字符串 | 2 字符 |
3 | 生日 BIRTH | 日期 | 2000 年 10 月 20 日 |
4 | 身份证号 ID | 字符串 | 18 位身份证号,全局唯一,作为主键 |
5 | 岗位 POSITIONID | 字符串 | 与岗位表关联 |
岗位表 Position:
序号 | 字段名称 | 字段类型 | 取值范围 |
1 | 岗位名称 POSITIONID | 字符串 | A、 B、 C、 D;全局唯一,作为主键 |
2 | 薪水 SALARY | 数字 | 10000, 6000, 3000, 1000;每月的薪水 |
(2)新进人员:
函数原型: def new_employee(person, level)
参数 person:四元组, (姓名,性别,生日,身份证号)。
参数 level:字符串,岗位。
返回值:人员插入成功,返回 0;失败返回-1。
(3)删除人员:
函数原型: def delete_employee(person)
参数 person:字符串,被删除人员的身份证号。
返回值:删除成功,返回 0;失败返回-1。
(4)设置岗位薪水:
函数原型: def set_level_salary(level, salary)
参数 level:字符串,岗位级别,即 A、 B、 C、 D 四个等级之一。
参数 level:整数,薪水。
返回值:设置成功,返回 0;失败返回-1。
(5)统计薪水开支:
函数原型: def get_total_salary()
返回值:整数,返回当前所有人员每月开支的薪水总和;失败返回-1。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sqlite3
import os
#11.实现数据库的操作
#(1)初始化数据库
def create_db(path):
for filename in os.listdir (path):
if filename=='Person.db' or filename=='Position.db':
os.remove(filename)
'''创建数据库表测试'''
print ('人员信息表Person创建数据库表测试...')
sql = '''CREATE TABLE Person (
`NAME` VARCHAR NOT NULL,
`GENDER` varchar NOT NULL,
`BIRTH` DATE DEFAULT NULL,
`ID` varchar DEFAULT NULL,
'POSTTION' VARCHAR DEFAULT NULL ,
PRIMARY KEY (`ID`)
)'''
conn = sqlite3.connect (path+'Person.db')
cu=conn.cursor ()
'''创建数据库表:student'''
if sql is not None and sql != '':
print ('执行sql:[{}]'.format (sql))
cu.execute (sql)
conn.commit ()
print ('创建数据库表[student]成功!')
flag=1
cu.close()
else:
# print ('the [{}] is empty or equal None!'.format (sql))
flag=0
'''创建数据库表测试'''
print ('岗位表Position创建数据库表测试...')
sql = '''CREATE TABLE Position(
'POSITION' VARCHAR NOT NULL,
`SALARY` INT NOT NULL,
PRIMARY KEY ('POSITION')
)'''
conn = sqlite3.connect (path + 'Position.db')
cu = conn.cursor ()
'''创建数据库表:student'''
if sql is not None and sql != '':
print ('执行sql:[{}]'.format (sql))
cu.execute (sql)
conn.commit ()
print ('创建数据库表[student]成功!')
flag=1
cu.close ()
else:
# print ('the [{}] is empty or equal None!'.format (sql))
flag=0
if flag==1:
return 0
else:
return -1
#(2)新进人员
def new_employee(person,level):
# 参数
# person:四元组, (姓名,性别,生日,身份证号)。
# 参数
# level:字符串,岗位。
# 返回值:人员插入成功,返回
# 0;失败返回 - 1。
conn=sqlite3.connect('Person.db')
cursor=conn.cursor()
name=person[0]
gender=person[1]
birth=person[2]
id=person[3]
sql='''insert into Person
('NAME','GENDER','BIRTH','ID','POSTTION')
VALUES (:st_name,:st_gender,:st_birth,:st_id,:st_position)
'''
cursor.execute (sql,{'st_name':name,'st_gender':gender,'st_birth':birth,'st_id':id,'st_position':level})
conn.commit ()
conn.close()
return 0
#(3)删除人员
def delete_employee(person):
conn=sqlite3.connect('Person.db')
c=conn.cursor()
c.execute('DELETE FROM PERSON WHERE ID='+person)
conn.commit ()
conn.close ()
return 0
#(4)设置岗位薪水
def set_level_salary(level,salary):
conn=sqlite3.connect('Position.db')
c=conn.cursor()
c.execute('INSERT INTO Position VALUES (?,?)',(level,salary))
conn.commit()
conn.close()
return 0
#(5)统计薪水开支
def get_total_salary():
conn=sqlite3.connect('Person.db' )
c=conn.cursor()
c.execute('SELECT POSTTION FROM Person')
# a=c.fetchone()
b=c.fetchall()
money=0
for i in b:
if i[0]=='A':
money+=10000
if i[0]=='B':
money+=6000
if i[0]=='C':
money+=3000
if i[0]=='D':
money+=1000
print(money)
return money
if __name__ == '__main__':
pass
path='./'
create_db (path)
person=('张三','男','1997年2月3日','123456789012345678')
level='A'
new_employee (person, level)
person = ('李四', '女', '1999年2月3日', '188456799012345678')
level = 'B'
new_employee (person, level)
person = ('王五', '女', '1999年2月3日', '188456789012345678')
level = 'C'
new_employee (person, level)
person = ('吗刘', '男', '1899年2月3日', '008456789012345678')
level = 'D'
new_employee (person, level)
person='188456789012345678'
delete_employee (person)
for i in [('A',10000),('B',6000),('C',3000),('D',1000)]:
set_level_salary (i[0], i[1])
get_total_salary()
如果对您有帮助,请打赏!谢谢!