自行创建一个“Address.xlsx”班级通讯录表格文件,表格中包含学号、姓名、电话、邮箱、地址等信息,并在表格中输入至少5行数据记录。编写Python程序,完成将Excel表格文件的通讯录信息导入到sqlite3数据库中。
编写一个AddressBook类,类中包含selectInfo,insertInfo,deleteInfo,
updateInfo等四个函数用于实现对数据库中通讯录的增删查改操作。
3.实例化AddressBook类的对象,使用本人信息测试完成对通讯录的数据处理操作。
import json
import sqlite3
import xlrd
import xlsxwriter
class AddressBook():
"""该类有selectInfo,insertInfo,deleteInfo,updateInfo等
四个函数用于实现对数据库中通讯录的增删查改操作"""
def selectInfo():
try:
con = sqlite3.connect('rk.db')
cur = con.cursor()
except:
print("数据库连接错误")
#con = sqlite3.connect('rk.db')
#cur = con.cursor()
try:
result = cur.execute('select * from rkTable')
for row in result:
print('{},{},{},{},{}'.format(*row))
except Exception as e:
return False
print("读数据库完成!")
cur.close()
con.close()
def insertInfo(ls1):
try:
con = sqlite3.connect('rk.db')
cur = con.cursor()
except:
print("数据库连接错误")
try:
sqlstr = 'insert into rkTable(学号,姓名,电话,邮箱,地址)values(?,?,?,?,?)'
cur.execute(sqlstr,(ls1[0],ls1[1],ls1[2],ls1[3],ls1[4]))
except Exception as e:
return False
finally:
con.commit()
cur.close()
con.close()
print("增数据库完成!")
def deleteInfo(ls1):
try:
con = sqlite3.connect('rk.db')
cur = con.cursor()
except:
print("数据库连接错误")
try:
sqlstr ='''delete from rkTable where 学号=? '''
cur.execute(sqlstr,[(ls1)])
except Exception as e:
return False
finally:
con.commit()
cur.close()
con.close()
print("删数据库完成!")
def updateInfo(ls1,ls2,ls3):
try:
con = sqlite3.connect('rk.db')
cur = con.cursor()
except:
print("数据库连接错误")
try:
if ls1 == '姓名':
sqlstr ='''update rkTable set 姓名 = ? where 学号 = ? '''
elif ls1 == '电话' :
sqlstr ='''update rkTable set 电话 = ? where 学号 = ? '''
elif ls1 == '地址':
sqlstr ='''update rkTable set 地址 = ? where 学号 = ? '''
elif ls1 == '邮件':
sqlstr ='''update rkTable set 邮件 = ? where 学号 = ? '''
else:
print('失败')
return False
cur.execute(sqlstr,(ls2,ls3))
except Exception as e:
return False
finally:
con.commit()
#cur.execute('update rkTable set 姓名 = ?where 学号 = ? ',(ls2,ls3))
con.commit()
cur.close()
con.close()
print("改数据库完成!")
import json
import sqlite3
import xlrd
import xlsxwriter
from class1 import AddressBook
def readEXCEL():
try:
in_wb = xlrd.open_workbook('Address.xlsx')
except:
print("文件读取错误")
ws = in_wb.sheet_by_index(0)
ls = []
for i in range(ws.nrows):
ls.append(ws.row_values(i))
#print(ls)
print('读EXCEL成功!')
return ls
def writeSQLite(ls):
try:
con = sqlite3.connect('rk.db')
cur = con.cursor()
except:
print("数据库连接错误")
droptable_sql = 'drop table if exists rkTable'
cur.execute(droptable_sql)
cur.execute('''create table rkTable(
'学号' text primary key not null,
'姓名' text not null,
'电话' text not null,
'邮箱' text not null,
'地址' text not null
);''')
for i in range(len(ls) - 1):
sqlstr = 'insert into rkTable(学号,姓名,电话,邮箱,地址)values(?,?,?,?,?)'
cur.execute(sqlstr,(ls[i + 1][0],ls[i + 1][1],ls[i + 1][2],ls[i + 1][3],ls[i + 1][4]))
con.commit()
cur.close()
con.close()
print('创建数据库成功')
def main():
ls = readEXCEL()
writeSQLite(ls)
ls1 = ['137',"cmm",'1***79',"13*****[email protected]","jxsr"]
ls2 = '137'
ls3 = '姓名'
ls4 = '大傻'
ls5 = '136'
AddressBook.selectInfo()
AddressBook.insertInfo(ls1)
AddressBook.selectInfo()
AddressBook.deleteInfo(ls2)
AddressBook.selectInfo()
AddressBook.updateInfo(ls3,ls4,ls5)
AddressBook.selectInfo()
if __name__ == '__main__':
main()