如何使用sqlalchemy获取在sqlite中创建的数据库的sql转储 [英] how to get sql dump of a database created in sqlite using sqlalchemy

查看:110
本文介绍了如何使用sqlalchemy获取在sqlite中创建的数据库的sql转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从json读取的数据,并使用python中的sql-alchemy以适当的模式输入到sqlite数据库中. (为此使用pd.dataframe.to_sql()).我想要此数据库的数据库转储(.sql文件). sql alchemy中是否有任何API可以做到这一点?

I have data read from json and fed into an sqlite database with proper schema using sql-alchemy in python. (used pd.dataframe.to_sql() for this). I want the database dump (.sql file) of this database. is there any api in sql alchemy that would do this?

推荐答案

假设您有一个与SQLite数据库关联的SQLAlchemy engine对象,则可以简单地使用Python的

Assuming that you have a SQLAlchemy engine object associated with your SQLite database you can simply use Python's iterdump method like so:

con = engine.raw_connection()
with open('C:/Users/Gord/Desktop/dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)

这篇关于如何使用sqlalchemy获取在sqlite中创建的数据库的sql转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆