import csv
import sqlite3

dbpath = 'csvtest.db'
conn = sqlite3.connect(dbpath)
cursor = conn.cursor()
sql = '''SELECT * FROM titanic'''
cursor.execute(sql)
data = cursor.fetchall()
# csvファイルを開きdataをcsvに書き込み
with open('output.csv', 'w') as f:
    output = csv.writer(f)
    output.writerow([
        'PassengerId', 'Survived', 'Pclass',
        'Name', 'Sex', 'Age', 'SibSp', 'Parch',
        'Ticket', 'Fare', 'Cabin', 'Embarked'
    ])
    output.writerows(data)
cursor.close()
conn.close()
