欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > 【sqlite】python操作sqlite3(含测试)

【sqlite】python操作sqlite3(含测试)

2025/2/11 7:39:47 来源:https://blog.csdn.net/u013080870/article/details/145518284  浏览:    关键词:【sqlite】python操作sqlite3(含测试)

个人小项目或者小团队,sqllite很适用,数据库封装操作如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025-02-08 13:57
# @Author  : duxiaowei
# @File    : connect_sqllite.py
# @Software: PyCharm
"""
sqllite操作,
"""
import sqlite3class DBlite():def __init__(self):self.conn = sqlite3.connect(r'D:\Tools\git\data\gitea.db')self.cursor = self.conn.cursor()# print("成功连接到数据库")# 查询def select(self, *args, **kwargs):try:self.cursor.execute(*args, **kwargs)# 获取所有查询结果results = self.cursor.fetchall()return resultsexcept sqlite3.Error as e:print("===>sqllite==【查询】==异常!", e)finally:self.cursor.close()self.conn.close()def change(self, *args, **kwargs):try:# 插入多行数据self.cursor.execute(*args, **kwargs)self.conn.commit()# print("===>sqllite==【操作】==成功!")except sqlite3.Error as e:print("===>sqllite==【操作】==异常!", e)finally:self.cursor.close()self.conn.close()# 查询所有用户
# select_query = "select count(1) from pytest_result where def_name=?"
# arg = ['test_1_shengou_shougong']
# result = DBlite().select(select_query, arg)
# print(result)
# # --------------------------------------------------------
# 插入数据
# students = [
#     ('D:\\code\\python\\test_登录5.py', 'test_aaa_class','test_aaa_func', "pass", "0.011",''),
#     ('D:\\code\\python\\test_登录.py', 'test_aaab_class','test_abbc_func', "failed", "0.012",''),
# ]
# for i in students:
#     sql = "insert into pytest_result(file_name, class_name, def_name, result, duration, message) values (?,?,?,?,?,?)"
#     DBlite().change(sql, i)# # ---------------删除-----------------------------------------
# sql = "delete FROM pytest_result where file_name=?"
# arg = ['D:\\code\\python\\test_登录6.py']
# DBlite().change(sql, arg)
# # # --------------------------------------------------------
# sql = "UPDATE pytest_result SET status = 0 WHERE def_name = ?"
#
# arg = ['test_aac']
# DBlite().change(sql, arg)
# # --------------------------------------------------------

以下为sqllit3 创建pytest运行结果表的,建表语句,我的方案

create table pytest_result
(id INTEGER not nullconstraint pytest_result_pkprimary key autoincrement,file_name1 varchar(100),class_name varchar(50),def_name varchar(60),result varchar(10),result_time TIMESTAMP default datetime('now', 'localtime') not null,status INT default 0,create_time TIMESTAMP default datetime('now', 'localtime') not null,duration varchar(30),message TEXT
);-- insert into pytest_result(id, file_name1, class_name, def_name, result, result_time, status, create_time, duration, message)values......;drop table pytest_result;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com