export sqlite database into CSV file

438 views
Skip to first unread message

lin

unread,
Oct 9, 2014, 3:00:28 AM10/9/14
to peewe...@googlegroups.com
I have check the document have CSV loader.
Does it have functions to export table or data into CSV file?
Any suggestion?

Charles Leifer

unread,
Oct 9, 2014, 10:21:40 PM10/9/14
to peewe...@googlegroups.com
Well, you can always use the sqlite shell:

sqlite> .mode csv
sqlite> .header on
sqlite> .out my_csv_dump.csv

Now any queries you run subsequently will be dumped to your CSV file. I'm not sure how to accomplish the same using SQLite cursors, it may just be a feature of the sqlite shell. If anyone knows, that'd be great.

You can also use the tuples query result wrapper and just use python's CSV utility which is super easy to use.

query = MyModel.select().tuples()
for row in query:
    csv_writer.writerow(row)

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lin

unread,
Oct 31, 2014, 4:44:37 AM10/31/14
to peewe...@googlegroups.com

I saw the latest peewee already provide dump_csv() function. Many thanks!
But I want to request few feature for dump_csv(). Actually I am using these features right now.
Maybe the first two is also useful for others.

1. Change title function. It is call writerow() in csv package.
   Or should I create a new csv_writer instance by my own. Then using writerow and pass it into dump_csv()?
2. Can choose which column doesn't want to export.
3. re-translate text. For example, in database using number 1~N instead of level or priority.
   But if export to files. Users want to see the description instead of numbers. Maybe High, medium, low.

Charles Leifer

unread,
Oct 31, 2014, 10:39:37 AM10/31/14
to peewe...@googlegroups.com
I think those goals are a little beyond the scope of the peewee utility, which is a thin wrapper over the standard library csv.

--
Reply all
Reply to author
Forward
0 new messages