You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.5 KiB
2.5 KiB
Python + SQL
Lessons
- Install a Python virtual environment
- Install psychopg2-binary
- Connect to Postgres via Python
- Running Queries with Python
- SQLite Basics
- Migrating data to a SQLite
- Exporting data to CSV
Install a Python virtual environment
python3 -m venv ~/my-env
source ~/my-env/bin/activate
Install psychopg2-binary
python -m pip install psycopg2-binary
Connect to Postgres via Python
import psycopg2
conn = psycopg2.connect(
database="my_db"
)
# close connection
conn.close()
Running Queries with Python
Select many
cursor = conn.cursor()
cursor.execute("SELECT * FROM people")
print(cursor.fetchall())
# close connection
cursor.close()
Select one
cursor.execute("SELECT * FROM people WHERE id = %s", [24])
print(cursor.fetchone())
Insert
cursor.execute("INSERT INTO people (name, age) VALUES (%s, %s)", ['Matt', 43])
conn.commit()
Delete
cursor.execute("DELETE FROM people WHERE id = %s", [24]);
conn.commit()
Update
cursor.execute("UPDATE people SET name = %s, age = %s WHERE id = %s", ['Matt', 43, 20])
conn.commit()
SQLite Basics
Connect:
import sqlite3
con = sqlite3.connect("mydb.db")
# close connection
con.close()
Create table
cur = con.cursor()
cur.execute("CREATE TABLE people (name, age)")
Select Many
res = cur.execute("SELECT * FROM people")
print(res.fetchall())
Select One
res = cur.execute("SELECT * FROM people WHERE name = 'Matt'")
print(res.fetchone())
Insert
cur.execute("INSERT INTO people (name, age) VALUES (?, ?)", ['Zagthorp', 543])
con.commit()
Delete
cur.execute("DELETE FROM people WHERE name = ?", ['Bilbo'])
con.commit()
Update
cur.execute("UPDATE people SET name = ? WHERE name = ?", ['Bilbo', 'Matthew'])
con.commit()
Check via sqlite3 in the terminal
sqlite3 mydb.db
Migrating data to a SQLite
#PostgreSQL
import psycopg2
conn = psycopg2.connect(
database="my_db"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM people")
people = cursor.fetchall()
cursor.close()
conn.close()
# SQLite
import sqlite3
con = sqlite3.connect("mydb.db")
cur = con.cursor()
cur.execute("CREATE TABLE people (id, name, age)")
for person in people:
cur.execute("INSERT INTO people (id, name, age) VALUES (?, ?, ?)", [person[0], person[1], person[2]])
con.commit()
con.close()