Most web applications read and write data in a database. Python connects to databases through small libraries, and learning this step is where a Python learner starts building real applications.
The basic steps
- Open a connection to the database.
- Create a cursor, which runs SQL statements.
- Run a query, passing values as parameters.
- Fetch the results, or commit the changes.
- Close the connection.
Always use parameters
Never build SQL by joining strings with user input. Pass values as parameters, so the library handles them safely. This prevents SQL injection, one of the most common security problems.
Start with SQLite
SQLite is built into Python and needs no server, so it is ideal for practice. The same ideas carry over to MySQL and PostgreSQL.
With a framework
Django and other frameworks use an ORM, which maps Python classes to tables. Learn plain SQL first, so you understand what the ORM does for you.
Next step: see our Python Full Stack course at ScholarLogic, or book a free demo class. A counsellor will share the fee and the next batch dates.
