dump csv from sqlalchemy

前端 未结 8 454
时光取名叫无心
时光取名叫无心 2020-12-02 17:48

For some reason, I want to dump a table from a database (sqlite3) in the form of a csv file. I\'m using a python script with elixir (based on sqlalchemy) to modify the datab

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 18:14

    I know this is old, but i just had this problem and this is how i solved it

    from sqlalchemy import create_engine
    
    basedir = os.path.abspath(os.path.dirname(__file__))
    sql_engine = create_engine(os.path.join('sqlite:///' + os.path.join(basedir, 'single_file_app.db')), echo=False)
    results = pd.read_sql_query('select * from users',sql_engine)
    results.to_csv(os.path.join(basedir, 'mydump2.csv'),index=False,sep=";")
    

提交回复
热议问题