问题: 接手一个DB文件,不知道里面都有些什么表,表里的数据长什么样子,按装了navicat后也没打开,后来想着用PYTHON打开看看。
python 代码如下:
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)
# save to excel
df.to_excel('C:\\Users\\Desktop\\db\\'+tab[0]+'.xlsx')
cursor.close()
conn.close()
return
# createing file path
path = 'C:\\Users\\Desktop\\test.db'
ReadDB(path)
结果如下:
![](https://img.haomeiwen.com/i7468301/649f02397c62fadf.png)
image.png
网友评论