美文网首页
python读取.db文件

python读取.db文件

作者: 丙吉 | 来源:发表于2023-12-03 13:19 被阅读0次

    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

    相关文章

      网友评论

          本文标题:python读取.db文件

          本文链接:https://www.haomeiwen.com/subject/lqukgdtx.html