Lesson 01
Database Communication
Learn why a Python program uses a database table, and the six steps from import and connect through the cursor, the query, and close.
Learn why a Python program uses a database table, and the six steps from import and connect through the cursor, the query, and close.
Example
import sqlite3
con = sqlite3.connect(":memory:")
cur = con.cursor()
print(type(con).__name__)
print(type(cur).__name__)
con.close()The prints are Connection and Cursor. A table stores rows under column names, so a query can ask for one column instead of scanning a raw file. Oracle and MySQL need their own database software plus a third-party package such as oracledb or mysql-connector-python. SQLite is the database the examples run, and sqlite3 is already in desktop Python. This browser build does not include sqlite3, so the playground says that and still prints the type names Connection and Cursor. :memory: keeps the table for one connection only. The same six steps apply on a server database: import, connect, cursor, execute, process the result, then close. These lessons do not send a password. A server database can require one; this in-memory database does not.
Key takeaways
-
A table has column names and rows, which makes a query simpler than a raw file
-
The six steps are import, connect, cursor, execute, process, close
-
sqlite3is in the Python standard library -
oracledbandmysql-connector-pythonare third-party packages for those servers -
:memory:lasts for this connection only -
Next: DDL Queries — create a table, add a column, and drop it
-
Previous: Client Program
Loading playground…
Check your understanding
Database Communication Quiz
What does a table add that a raw file usually lacks?
1 / 10