VA Session 5 Python vs SQL Flashcards
SQL vs Python
SQL
- to store data & initial processing
- for operations on large datasets (many rows & columns)
- for heavy duty tasks
- limited numbers of built-in functions
- often more efficient (python every time code executed, connects to database again)
Python
- for exploratory & further data analysis
- many built-in functions
Python connecting to database
- supports connecting to common commercial & open-source databases
- SQL commands
- Why? -> data often stored in database (not file e.g. csv)
Connecting Python & database - code
inport sqlite3
db = sqlite3.connect(“path…”)
Querying database with Python - code
db.execute(“SELECT … FROM …”).fetchone()
Adding a record to database with Python - code
db.execute(“INSERT INTO table(column) VALUES (“…”)”)
Return output - code
- fetchall(): retrieves result of query as list where each returned row = tuple
- fetchone(): retrieves first row of results of query as tuble
Parameters
- allow feeding values into query
- e.g. name = input(“Product name: )
- db.execute(“SELECT ProductPrice FROM Product WHERE ProductName=?”,[name]).fetchone()
Error Handling
- prevent if query fails, script continues running
- One approach: try & except
SQL Injections: Danger
- Threat when application connected to database (e.g. web interfaces: webpages input communicates with a database)
- unauthorized view or edit data (or whole database)
- unauthorized inputs (e.g. DROP TABLE Users)
approaches of calculating most expensive product
- SQL: db.execute(“SELECT Max(ProductPrice) FROM Product”).fetchone()
- SQL & Python: prices = db.executre(…).fetchall(); mostExpensive = max(prices)
-1> 2 (2. brings potentially millions of unnecessary data