Last active
August 29, 2016 02:58
-
-
Save kurozumi/f9632d83b9ed5da1d568a7585c5e2de0 to your computer and use it in GitHub Desktop.
【Python】peeweeを使ってデータベースのデータをCSVでエクスポートする方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from peewee import * | |
# dump_csvを使えるようにする | |
from playhouse.csv_loader import * | |
db = MySQLDatabase("database", **{"password": "password", "user": "user", "host": "localhost"}) | |
# ベースモデル。すべてのモデルはコレを継承することでデータベースが自動で指定される | |
class BaseModel(Model): | |
class Meta: | |
database = db | |
# 適当に作ったユーザーテーブル | |
class Users(BaseModel): | |
username = CharField(unigue=True) | |
password = CharFiled() | |
email = CharField() | |
created_at = DateTimeFiled() | |
class Meta: | |
db_table = "users" | |
# すべてのユーザーデータを読み込んでdump_csvでエクスポートする | |
with open('users.csv', 'w') as fh: | |
query = Users.select() | |
dump_csv(query, fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment