import sqlite3,os,time #python3.4,,,,jk409于2014-05-08用以练习sqlit3编写的人员信息脚本 #data=[[1,"jk409",'25',"1988-05-02","IT",'66666666'], # [2,"kkk",'20'"1988-10-07","IT",'88888888'] # ] def init(): f=open('./test.db','wb') f.close() cx=sqlite3.connect('./test.db') cu=cx.cursor() cu.execute('create table kkk(pid integer primary key,\ name varchar(16),\ age integer,\ sr text NULL,\ bm text NULL,\ tel text NULL)') cx.commit() #cu.close() cx.close() def add(): data_tj=[] ID=input("请输入您的[ID]:") xm=input("请输入您的[姓名]:") ss=input("请输入您的[年龄]:") sr=input("请输入您的[出生年月日]:") bm=input("请输入您的[所在部门]:") tel=input("请输入您的[联系电话]:") data_tj.append([ID,xm,ss,sr,bm,tel]) cx=sqlite3.connect('./test.db') cu=cx.cursor() for i in data_tj: print(i) cx.execute("insert into kkk values (?,?,?,?,?,?)", i) print(xm,'个人信息添加成功!') cx.commit() cx.close() def cx(): cx=sqlite3.connect('./test.db') cu=cx.cursor() xz=input("1.查询所有的信息 2.个人模糊信息查询 3.个人精确信息查询:") if xz == "1": print ("\n查询的信息如下:") data_cx=cu.execute('select * from kkk;') for i in data_cx.fetchall(): print ('ID:',i[0],"\t"+i[1],"\t",i[2],"\t"+i[3],"\t"+i[4],"\t"+i[5]) if xz == "2": cx_name=input("请输入您的[姓名]:") print ("\n查询的信息如下:") data_cx=cu.execute("select * from kkk where name = '%s';" % cx_name) for i in data_cx.fetchall(): if cx_name == i[1]: print ('ID:',i[0],"\t"+i[1],"\t",i[2],"\t"+i[3],"\t"+i[4],"\t"+i[5]) #if xz == "3": # cx_name=input("请输入您的[姓名]:") # cx_tel=input("请输入您的[电话]:") # print ("\n查询的信息如下:") # data_cx=cu.execute('select * from kkk;') # for i in data_cx.fetchall(): # if cx_name == i[1] and cx_tel ==i[5]: # print ("姓名:"+i[1],"\t年龄:",i[2],"\t生日:"+i[3],"\t部门:"+i[4],"\t电话:"+i[5]) # break if xz == "3": cx_name=input("请输入您的[姓名]:") cx_tel=input("请输入您的[电话]:") print ("\n查询的信息如下:") data_cx=cu.execute("select * from kkk where name = '%s' and tel = '%s';" %(cx_name,cx_tel)) for i in data_cx.fetchall(): if cx_name == i[1] and cx_tel ==i[5]: print ("姓名:"+i[1],"\t年龄:",i[2],"\t生日:"+i[3],"\t部门:"+i[4],"\t电话:"+i[5]) cx.commit() #cu.close() cx.close() def sc(): sc_name=input("请输入需要删除的信息的[姓名]:") cx=sqlite3.connect('./test.db') cu=cx.cursor() cx.execute("delete from kkk where name = '%s'" % sc_name) #cx.execute("insert into kkk values (?,?,?,?,?,?)", i) print(sc_name,'个人信息[删除]成功!') cx.commit() cx.close() def xg(): #建议使用唯一的id号修改比较好,以防把同名字的信息也修改了 xg_name=input("请输入需要删除的信息的[姓名]:") cx=sqlite3.connect('./test.db') cu=cx.cursor() data_xg=cu.execute('select * from kkk ;') for i in data_xg.fetchall(): if i[1] == xg_name: print('\nID:',i[0]) print ("姓名:"+i[1],"\t年龄:",i[2],"\t生日:"+i[3],"\t部门:"+i[4],"\t电话:"+i[5]) print("\n","1.姓名 2.年龄 3.生日 4.部门 5.电话") bh_mod=input("请输入你修改项目的[编号1~5]:") if bh_mod == "1": n_mod=input("请输入您的新姓名:") cu.execute("update kkk set name='%s' where name = '%s' "% (n_mod,xg_name)) if bh_mod == "2": l_mod=input("请输入您的新年龄:") cu.execute("update kkk set age ='%s' where name = '%s' "% (l_mod,xg_name)) if bh_mod == "3": s_mod=input("请输入您的新生日:") cu.execute("update kkk set sr ='%s' where name = '%s' "% (s_mod,xg_name)) if bh_mod == "4": b_mod=input("请输入您的新部门:") cu.execute("update kkk set bm ='%s' where name = '%s' "% (b_mod,xg_name)) if bh_mod == "5": t_mod=input("请输入您的新电话:") cu.execute("update kkk set tel ='%s' where name = '%s' "% (t_mod,xg_name)) #if bh_mod == "" or bh_mod == " ": print('输入有误!') cx.commit() #cu.close() cx.close() def show(): print("---------------------------------------") print("1.查询 2.添加 3.修改 4.删除 5.退出 ") def pl2(): #批量插入1百万行数据,使用时间30秒, a=time.time() cx=sqlite3.connect('./test.db') cu=cx.cursor() for i in range(1,1000000): cx.execute("insert into kkk values (%s,'yyy',25,'1985-10-7','IT','%s')"% (i,i)) cx.commit() #cu.close() cx.close() b=time.time() print(b-a) if __name__=='__main__': while 1: os.system('cls') show() xz=input("请输入您的需要选项的编号[1/2/3/4/5]:") if xz == "9": pl() if xz == "1": cx() if xz == "2": add() if xz == "3": xg() if xz == "4": sc() if xz == "5": break if xz == "" or xz == " ": print("空") if xz == "0": init=input('改操作会格式所有数据,请确实是否操作[yes|no]:') if init == "yes": print("格式化中......") init() print("格式化完成!") xyz=input("\n按任意键继续......")