export to csv

main
Matthew Huntington 2 years ago
parent db0ad49996
commit fccdad9bc0

@ -141,7 +141,7 @@ sqlite3 mydb.db
## Migrating data to a SQLite ## Migrating data to a SQLite
```python ```python
#PostgreSQL # PostgreSQL
import psycopg2 import psycopg2
conn = psycopg2.connect( conn = psycopg2.connect(
@ -202,3 +202,28 @@ with open('writedb.csv', 'w') as csvfile:
## Exporting data to CSV ## Exporting data to CSV
```python
# PostgreSQL
import psycopg2
conn = psycopg2.connect(
database="my_db"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM people")
people = cursor.fetchall()
cursor.close()
conn.close()
# CSV
import csv
with open('writedb.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for person in people:
writer.writerow(person)
```

Loading…
Cancel
Save