- 从Python3.x版本开始,在标准库中已经 内置了SQLlite3模块,它可以支持SQLite3数据库的访问和相关的数据库操作。在需要操作SQLite3数据库数据时,只须在程序中导入SQLite3模块即可。
#导入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()
#导入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()
- 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()
- 需要把读取的信息放到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)
#导入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()
#导入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()
:
cur.execute(delete_sql, (2,))
提交事务
con.commit()
print(‘删除成功’)
except Exception as e:
print(e)
print(‘删除失败’)
con.rollback()
finally:
关闭游标
cur.close()
关闭连接
con.close()
![Python读取sqlite3数据库中的数据](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230809/7fb8efddb56baf993b17041d70b8e908.png)
Original: https://blog.csdn.net/u014723479/article/details/128474877
Author: 我真的爱发明
Title: Python读取sqlite3数据库中的数据
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815696/
转载文章受原作者版权保护。转载请注明原作者出处!