吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2540|回复: 17
收起左侧

[Python 原创] Python读取sqlite3数据库中的数据

[复制链接]
我真的爱发明 发表于 2022-12-28 22:24

[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\hengzhe19711121@163.com_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()

免费评分

参与人数 3吾爱币 +9 热心值 +3 收起 理由
5151diy + 1 + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
孺子韫 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

unpy 发表于 2023-10-25 17:04

默认的没有

需要安装 sqlcipher , 然后就可以了

from pysqlcipher3 import dbapi2 as sqlite3
....
cursor.execute("PRAGMA key='mypassword'")
sghades 发表于 2023-11-1 20:39
python处理sqlite数据,大概150万行数据,读取到内存里会爆,只能用sql将处理放在sqlite内处理,但是效率低下,请问有没有办法加载在内存里处理?
ChineseCabbage 发表于 2022-12-29 00:28
tiantou 发表于 2022-12-29 08:28
谢谢锟斤拷锟斤拷
imcuer 发表于 2022-12-29 09:05
Python读取sqlite3
navis1mple 发表于 2022-12-29 09:39
6666666666666666666
GMCN 发表于 2022-12-29 10:11
支持加密吗
89684828 发表于 2022-12-29 10:23
感谢楼主分享,支持一下!
vethenc 发表于 2022-12-29 10:27
感谢分享,非常实用的库
xxl1039 发表于 2022-12-29 10:29
感谢分享。
sjw166029 发表于 2022-12-29 10:34
还不错,支持!!!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 22:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表