Python读取sqlite3数据库中的数据

查看 127|回复 9
作者:我真的爱发明   
[toc]
1. 简介
  • 从Python3.x版本开始,在标准库中已经内置了SQLlite3模块,它可以支持SQLite3数据库的访问和相关的数据库操作。在需要操作SQLite3数据库数据时,只须在程序中导入SQLite3模块即可。

    1.1. 使用
    1.1.1. 创建
    代码
    #导入sqllite3模块
    import sqlite3
    # 1.硬盘上创建连接
    con = sqlite3.connect('first.db')
    # 获取cursor对象
    cur = con.cursor()
    # 执行sql创建表
    sql = 'create table t_person(pno INTEGER PRIMARY KEY  AUTOINCREMENT ,pname varchar(30) NOT NULL ,age INTEGER)'
    try:
        cur.execute(sql)
    except Exception as e:
        print(e)
        print('创建表失败')
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()




  • 1.1.2. 插入
    1.1.2.1. 插入一条数据
    #导入sqllite3模块
    import sqlite3
    # 1.硬盘上创建连接
    con = sqlite3.connect('first.db')
    # 获取cursor对象
    cur = con.cursor()
    # 执行sql创建表
    sql = 'insert into t_person(pname,age) values(?,?)'
    try:
        cur.execute(sql,('张三',23))
        #提交事务
        con.commit()
        print('插入成功')
    except Exception as e:
        print(e)
        print('插入失败')
        con.rollback()
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()
    1.1.3. 查询
    1.1.3.1. 查询所有数据
  • fetchall()查询所有数据

    #导入sqllite3模块
    import sqlite3
    # 1.硬盘上创建连接
    con = sqlite3.connect('first.db')
    # 获取cursor对象
    cur = con.cursor()
    # 执行sql创建表
    sql = 'select * from t_person'
    try:
        cur.execute(sql)
        # 获取所有数据
        person_all = cur.fetchall()
        # print(person_all)
        # 遍历
        for p in person_all:
            print(p)
    except Exception as e:
        print(e)
        print('查询失败')
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()



  • 查询一条数据

  • fetchone()查询一条数据
  • 按顺序进行读取


    #导入sqllite3模块
    import sqlite3
    # 1.硬盘上创建连接
    con = sqlite3.connect('first.db')
    # 获取cursor对象
    cur = con.cursor()
    # 执行sql创建表
    sql = 'select * from t_person'
    try:
        cur.execute(sql)
        # 获取一条数据
        person = cur.fetchone()
        print(person)
        person = cur.fetchone()
        print(person)
        person = cur.fetchone()
        print(person)
        person = cur.fetchone()
        print(person)
    except Exception as e:
        print(e)
        print('查询失败')
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()




  • 1.1.3.2. 读取特定位置的数据
  • 需要把读取的信息放到sql语句中
    获得最新一条数据
  • 代码


    def sqlite3_get_last_data(db_path,sql):
        # 导入sqllite3模块
        import sqlite3
        # 1.硬盘上创建连接
        con = sqlite3.connect(db_path)
        # 获取cursor对象
        cur = con.cursor()
        # 执行sql创建表
        try:
            cur.execute(sql)
            # 获取所有数据
            person_all = cur.fetchall()
            last_data = person_all[-1]
            # print(last_data)
            # print("type(last_data):", type(last_data))
            # print("last_data:", )
            last_text = last_data[6]
            return last_text
        except Exception as e:
            print(e)
            print('查询失败')
        finally:
            # 关闭游标
            cur.close()
            # 关闭连接
            con.close()
    db_path = 'D:\MailMasterData\[email protected]_1414\search.db'
    sql = 'select * from Search_content'
    last_text = sqlite3_get_last_data(db_path,sql)
    print("last_text:", last_text)




  • 1.1.4. 修改数据
    代码
    #导入sqllite3模块
    import sqlite3
    #1.硬盘上创建连接
    con=sqlite3.connect('first.db')
    #获取cursor对象
    cur=con.cursor()
    try:
        #执行sql创建表
        update_sql = 'update t_person set pname=? where pno=?'
        cur.execute(update_sql, ('小明', 1))
        #提交事务
        con.commit()
        print('修改成功')
    except Exception as e:
        print(e)
        print('修改失败')
        con.rollback()
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()




  • 1.1.5. 删除数据
    代码
    #导入sqllite3模块
    import sqlite3
    #1.硬盘上创建连接
    con=sqlite3.connect('first.db')
    #获取cursor对象
    cur=con.cursor()
    #执行sql创建表
    delete_sql = 'delete from t_person where pno=?'
    try:
        cur.execute(delete_sql, (2,))
        #提交事务
        con.commit()
        print('删除成功')
    except Exception as e:
        print(e)
        print('删除失败')
        con.rollback()
    finally:
        # 关闭游标
        cur.close()
        # 关闭连接
        con.close()



  • 数据, 游标

  • ChineseCabbage   

    前排支持
    tiantou   

    谢谢锟斤拷锟斤拷
    imcuer   

    Python读取sqlite3
    navis1mple   

    6666666666666666666
    GMCN   

    支持加密吗
    89684828   

    感谢楼主分享,支持一下!
    vethenc   

    感谢分享,非常实用的库
    xxl1039   

    感谢分享。
    sjw166029   

    还不错,支持!!!
    您需要登录后才可以回帖 登录 | 立即注册

    返回顶部