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