首页 > 数据库 >python操作sqlite

python操作sqlite

时间:2023-09-03 11:55:38浏览次数:35  
标签:sqlite name python fields self sql table 操作 data

import json
import sqlite3
import pandas as pd


class SqliteTool:
    def __init__(self, db_path):
        self.db_path = db_path
        self.conn = sqlite3.connect(self.db_path)
        self.conn.row_factory = sqlite3.Row
        self.cursor = self.conn.cursor()

    def is_exist_table(self, table_name):
        '''
        判断表是否存在,存在为1,不存在为0
        '''
        sql = f"select count(*)  from sqlite_master where type='table' and name='{table_name}'"
        data = self.cursor.execute(sql)
        count = 0
        for d in data:
            count = dict(d).get("count(*)")
            break
        if count == 0:
            print(f"table_name={table_name}不存在!")
        return count

    def get_tables(self):
        '''
        获取数据库中所有表
        '''
        table_list = self.cursor.execute("select name from sqlite_master where type='table' order by name;")
        tables = []
        for table in table_list:
            tables.append(dict(table).get("name"))
        print(f"数据库中总共有表:{len(tables)}张,tables={tables}")
        return tables

    def get_one_table_data(self, table_name, fields=[]):
        if len(fields) == 0:
            sql = f"select * from {table_name}"
            fields = self.get_fields(table_name)
        else:
            fields_str = ",".join(fields)
            sql = f"select {fields_str} from {table_name}"
        print(f"需要获取的字段有:{fields}")
        data_lst = self.cursor.execute(sql)
        data_list = []
        for data in data_lst:
            dic = dict(data)
            one_line = []
            for field in fields:
                field_value = dic.get(field)
                one_line.append(field_value)
            data_list.append(one_line)
        result = {
            "fields": fields,
            "data_list": data_list
        }
        return result

    def delete_table(self, table_name):
        '''删除表'''
        try:
            sql = f"drop table if exists {table_name}"
            self.cursor.execute(sql)
        except Exception as e:
            return f"删除表失败,error={e}"

    def execute_sql(self, sql):
        '''执行sql'''
        try:
            self.cursor.execute(sql)
        except Exception as e:
            return f"execute_sql fail error={e}"

    def get_fields(self, table_name):
        '''
        获取表中所有字段
        '''
        sql = f"PRAGMA table_info([{table_name}])"
        data_lst = self.cursor.execute(sql)
        fields = []
        for data in data_lst:
            name = dict(data).get("name")
            fields.append(name)
        return fields

    def data2json(self, table_name, json_file, fields=[]):
        try:
            if len(fields) == 0:
                sql = f"select * from {table_name}"
                fields = self.get_fields(table_name)
            else:
                fields_str = ",".join(fields)
                sql = f"select {fields_str} from {table_name}"
            print(f"需要获取的字段有:{fields}")
            data_lst = self.cursor.execute(sql)
            data_list = []
            f = open(json_file, "w", encoding="utf-8")
            for data in data_lst:
                dic = dict(data)
                data_list.append(dic)
                f.write(json.dumps(dic, ensure_ascii=False) + "\n")
                f.flush()
            f.close()
        except Exception as e:
            print(f"数据存入json文件失败!,e={e}")

    def data2excel(self, table_name, excel_path, fields=[]):
        '''
        @table_name:表名
        @excel_path:保存数据的excel地址
        @fields:需要获取的字段
        '''
        try:
            is_exist = self.is_exist_table(table_name)
            if is_exist == 1:
                result = self.get_one_table_data(table_name, fields)
                if result:
                    headers = result.get("fields")
                    data_list = result.get("data_list")
                    data_list.insert(0, fields)
                    df = pd.DataFrame(data_list, columns=headers)
                    df.to_excel(excel_path)
        except Exception as e:
            print(f"数据存入excel失败!,e={e}")

    def run(self):
        # self.get_tables()
        table_name = "douyin_juliang_suggestions"
        # ['id', 'createtime', 'updatetime', 'suggestions', 'keyword']
        # datas = self.get_one_table_data(table_name, ["keyword", "suggestions"])
        # print(datas)
        # is_exist = self.is_exist_table(table_name)
        # print(is_exist)
        # excel_path= "1.xlsx"
        # self.data2excel(table_name, excel_path, fields=[])
        pass

    def close(self):
        self.cursor.close()
        self.conn.close()

if __name__ == '__main__':
    db_path = "D:\gk\kuaishou.db"
    s = SqliteTool(db_path=db_path)
    table_name="kuaishou_data"
    excel_path="1.xlsx"
    s.get_tables()
    s.data2excel(table_name,excel_path)
    s.run()
    s.close()

 

标签:sqlite,name,python,fields,self,sql,table,操作,data
From: https://www.cnblogs.com/knighterrant/p/17674823.html

相关文章

  • Python安装
    Python3编译安装1.安装编译相关工具yum-ygroupinstall"Developmenttools"yum-yinstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgdbm-develdb4-devellibpcap-develxz-develyuminstalllibffi-devel-y2.下载安......
  • python+selenium自动化测试
    自动化测试工具selenium使用指南python+selenium环境安装:直接pipinstallselenium 安装webdriver打开/关闭浏览器:importtimefromseleniumimportwebdriverbrowser=webdriver.Edge()browser.get("http://www.baidu.com/")time.sleep(5)browser.get("https://ma......
  • python学习
    python学习正则表达式的使用正则表达式以下是替换指定文件夹下文本中的内容对图片形式的pdf提取目录,可以用以下程序叠加多个正则表达式来去除重复项。importosimportredefreplace_timestamp(directory):#遍历目录下的所有文件和文件夹forroot,dirs,fil......
  • 【Python】90%开发者未注意到的Python特性
    1.引言如果你在日常工作中经常使用Python进行编码,那么毫无疑问你会非常喜欢这个简单的Python功能。闲话少说,我们直接开始吧!2.举个栗子例如,要求大家打印列表中元素的值。我想大部分人可以立即使用for循环来执行此操作。li=[10,20,30,40,50]foriinli:print(i)结......
  • MySQL基础篇:掌握数据表操作的基础知识
    表(table)是一种结构化的文件,可以用来存储特定类型的数据,如:学生信息,课程信息,都可以放到表中。另外表都有特定的名称,而且不能重复。表中具有几个概念:列、行、主键。列叫做字段(Column),行叫做表中的记录,每一个字段都有:字段名称/字段数据类型/字段约束/字段长度创建表在MySQL中,......
  • 浅析常用的Python Web的几大框架
    在各种语言平台中,python涌现的web框架恐怕是最多的,是一个百花齐放的世界,各种micro-framework、framework不可胜数;猜想原因应该是在python中构造框架十分简单,使得轮子不断被发明。所 以在Python社区总有关于Python框架孰优孰劣的话题。下面就给大家介绍一下python的几大框架: Djan......
  • 使用synchronized关键字来同步多个线程操作同一个文件
    使用synchronized关键字来同步多个线程操作同一个文件importjava.io.FileWriter;importjava.io.IOException;publicclassFileSyncExample{privatestaticObjectfile=newObject();publicstaticvoidmain(String[]args)throwsInterruptedException{......
  • Python练习:嵌套列表解析,讲3*4的矩阵转换成4*3的矩阵
      1#嵌套列表解析,讲3*4的矩阵转换成4*3的矩阵23matrix=[[1,2,3,4],4[5,6,7,8],5[9,10,11,12]]678forrowinmatrix:9print("遍历每一行:",row)101112print("\n")1314s=[[row[i]forrow......
  • python: excel 换行符(_x000D_)
     data4=dataframe1.loc[4:34]foridx,datavalueindata4.iterrows():#strnum=Common.Utils.Utils.getAnnualLeave(data)print("[{}]:{}".format(idx,datavalue))slist=datavalue.tolist()hbll=BLL.EmpLoyeeHo......
  • 如何使用C++11原子操作实现自旋锁
    什么是自旋锁?C++自旋锁是一种低层次的同步原语,用于保护共享资源的访问。自旋锁是一种轻量级的锁,适用于短时间的资源锁定。自旋锁的特点:当一个线程尝试获取已经被另一个线程占有的自旋锁时,这个线程会进入一个循环(自旋),在这个循环中它不断地检查锁是否已经被释放。如果锁已经被释放,那......