博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python操作sqlite3
阅读量:7233 次
发布时间:2019-06-29

本文共 5083 字,大约阅读时间需要 16 分钟。

  hot3.png

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按任意键继续......")

转载于:https://my.oschina.net/jk409/blog/290249

你可能感兴趣的文章
不需要远程kernel就能在浏览器上运行 为数据科学实验和沟通打造的工具
查看>>
iOS - XML解析
查看>>
数据与前端
查看>>
react-copy-write 基于新 Context 和 immer 的 React 状态管理库
查看>>
RE|GoF的23种设计模式-4
查看>>
探究JavaScript中的继承
查看>>
python中list详解
查看>>
那时为了金三银四,我所整理的java面试题汇总
查看>>
[给创业公司的原生云] ①原生云是什么
查看>>
初识web-components 并且快速实现todolist
查看>>
51信用卡 Android 自动埋点实践
查看>>
activiti web流程设计器 整合视频 教程 SSM和独立部署的方式
查看>>
Android Menu
查看>>
担心被淘汰?请看这份财会人员晋升指南!
查看>>
JB的小程序之旅-小程序基础(登录授权、请求数据)
查看>>
(七)微服务分布式云架构spring cloud - common-service 项目构建过程
查看>>
简谈socket在直播软件开发上的应用
查看>>
JavaScript数组增删改查知识梳理
查看>>
日常抄书之React中Diff算法思路
查看>>
(二)大型互联网分布式企业微服务云架构
查看>>