python读取.db文件

python 读取.db文件并保存为xlsx文件

import sqlite3
import pandas as pd




def ReadDB(path:str):
    # create a sql connection to our sqlite database
    try:
        conn = sqlite3.connect(path)
    except Error as e:
        print(e)
    # creating cursor
    cursor = conn.cursor()
    # reading all table names
    table_list = [a for a in cursor.execute("SELECT name FROM sqlite_master WHERE type = 'table'")]
    # here is your table list
    print('table_list is :\n', table_list)
    for tab in table_list:
        df = pd.read_sql_query('SELECT * FROM {}'.format(tab[0]), conn)
        conn.commit()
        print('table ', tab[0], 'head is :\n', df.head())
        print('table ', tab[0], 'shape is :\n', df.shape)
        df.to_excel('./'+tab[0]+'.xlsx')
    cursor.close()
    conn.close()
    return


# createing file path
path = './test.db'
ReadDB(path)
结果如下:
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容