dump csv from sqlalchemy

前端 未结 8 432
时光取名叫无心
时光取名叫无心 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 17:57

    Modifying Peter Hansen's answer here a bit, to use SQLAlchemy instead of raw db access

    import csv
    outfile = open('mydump.csv', 'wb')
    outcsv = csv.writer(outfile)
    records = session.query(MyModel).all()
    [outcsv.writerow([getattr(curr, column.name) for column in MyTable.__mapper__.columns]) for curr in records]
    # or maybe use outcsv.writerows(records)
    
    outfile.close()
    
    0 讨论(0)
  • 2020-12-02 17:59

    There are numerous ways to achieve this, including a simple os.system() call to the sqlite3 utility if you have that installed, but here's roughly what I'd do from Python:

    import sqlite3
    import csv
    
    con = sqlite3.connect('mydatabase.db')
    outfile = open('mydump.csv', 'wb')
    outcsv = csv.writer(outfile)
    
    cursor = con.execute('select * from mytable')
    
    # dump column titles (optional)
    outcsv.writerow(x[0] for x in cursor.description)
    # dump rows
    outcsv.writerows(cursor.fetchall())
    
    outfile.close()
    
    0 讨论(0)
  • 2020-12-02 18:09
    with open('dump.csv', 'wb') as f:
        out = csv.writer(f)
        out.writerow(['id', 'description'])
    
        for item in session.query(Queue).all():
            out.writerow([item.id, item.description])
    

    I found this to be useful if you don't mind hand-crafting your column labels.

    0 讨论(0)
  • 2020-12-02 18:10
    import csv
    
    f = open('ratings.csv', 'w')
    out = csv.writer(f)
    out.writerow(['id', 'user_id', 'movie_id', 'rating'])
    
    for item in db.query.all():
        out.writerow([item.username, item.username, item.movie_name, item.rating])
    f.close()
    
    0 讨论(0)
  • 2020-12-02 18:10

    I spent a lot of time searching for a solution to this problem and finally created something like this:

    from sqlalchemy import inspect
    
    with open(file_to_write, 'w') as file:
        out_csv = csv.writer(file, lineterminator='\n')
    
        columns = [column.name for column in inspect(Movies).columns][1:]
        out_csv.writerow(columns)
    
        session_3 = session_maker()
    
        extract_query = [getattr(Movies, col) for col in columns]
        for mov in session_3.query(*extract_query):
            out_csv.writerow(mov)
    
        session_3.close()
    

    It creates a CSV file with column names and a dump of the entire "movies" table without "id" primary column.

    0 讨论(0)
  • 2020-12-02 18:12

    I adapted the above examples to my sqlalchemy based code like this:

    import csv
    import sqlalchemy as sqAl
    
    metadata = sqAl.MetaData()
    engine = sqAl.create_engine('sqlite:///%s' % 'data.db')
    metadata.bind = engine
    
    mytable = sqAl.Table('sometable', metadata, autoload=True)
    db_connection = engine.connect()
    
    select = sqAl.sql.select([mytable])
    result = db_connection.execute(select)
    
    fh = open('data.csv', 'wb')
    outcsv = csv.writer(fh)
    
    outcsv.writerow(result.keys())
    outcsv.writerows(result)
    
    fh.close
    

    This works for me with sqlalchemy 0.7.9. I suppose that this would work with all sqlalchemy table and result objects.

    0 讨论(0)
提交回复
热议问题