美文网首页
python打开未知的DB文件

python打开未知的DB文件

作者: 丙吉 | 来源:发表于2023-05-25 08:09 被阅读0次
问题: 接手一个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)
结果如下:
image.png

相关文章

网友评论

      本文标题:python打开未知的DB文件

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