Run Parameterized Queries from Python
For Project Part 3, you will run SQLite queries from Python and use the result.
Use Python Console Mode to Read from Database
Please re-type and run the following code in Python Console Mode. Take note of what each line appears to be doing and what data types are being used.
import sqlite3
conn = sqlite3.connect('company.db')
cur = conn.cursor()
cur.execute('SELECT name FROM professionals')
rows = cur.fetchall()
rows
Mess with the variable rows (a list of tuples) until you are comfortable with it. Print it, change parts of it, run loops to get its data. An example of loops you might run is as follows:
for row in rows: print(row)
for row in rows:
for col in row: print(col)
Convert rows to dictionary type
Many programmers prefer a list of dict data types (which have the field names) over a list of tuples (which do not have the field names) when getting database results, and project part 3 requires you to do this because this makes it so you can change your model file (which loads JSON into a list of dict types) without changing your views.
One easy way to get this is: Before running cur = conn.cursor(),
run conn.row_factory = sqlite3.Row.
This setting makes the sqlite3 library return rows as a list of objects instead of a list of tuples.
Then, replace rows = cur.fetchall() with this convenient list comprehension that translates each row to a dict data structure:
rows = [dict(row) for row in cur.fetchall()].
The rows variable will then contain a list of dict objects.
Use Python Console Mode to Write to Database (without parameters)
If needed, run these three lines to get connected to the database:
import sqlite3
conn = sqlite3.connect('company.db')
cur = conn.cursor()
Insert a professional into professionals (then check whether it worked), edit it (then check whether it worked), then delete it (then check whether it worked). The following example code should do, but you may do these actions any way that works for you.
# Insert a Professional
cur.execute("INSERT INTO professionals (name, image_ext) VALUES ('Test Name','jpg')")
conn.commit()
# Get the id of the inserted professional:
id = cur.lastrowid
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
cur.execute(f"UPDATE professionals SET name='Test Name 2' WHERE id={id}")
conn.commit()
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
cur.execute(f"DELETE FROM professionals WHERE id={id}")
conn.commit()
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
Use Python Console Mode to Write to Database with Parameters
Do the same INSERT, UPDATE, and DELETE operations, but this time use parameterized data in your UPDATE and DELETE queries instead of strings. The following example code should do, but you may do these actions any way that works for you.
# Insert a Professional
cur.execute("INSERT INTO professionals (name, image_ext) VALUES (?, ?, ?)", ["Test Name", "jpg"])
conn.commit()
# Get the id of the inserted professional:
id = cur.lastrowid
id
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
cur.execute("UPDATE professionals SET name=? WHERE id=?",["Test Name 2", id])
conn.commit()
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
cur.execute("DELETE FROM professionals WHERE id=?", [id])
conn.commit()
# Check whether it worked:
cur.execute("SELECT * FROM professionals").fetchall()
Close File
When done, close the database file using:
conn.close()