Good Practice With Database Drivers in Your First App
A driver is the library your code uses to talk to the database. A few good habits with it prevent most beginner bugs and security problems. Each practice below comes with the reason and the anti-pattern it replaces.
1. Use placeholders for every value
Do:
conn.execute("SELECT * FROM users WHERE email = ?", (email,))
Instead of: "... WHERE email = '" + email + "'".
Why: placeholders keep values separate from the SQL, preventing SQL injection and quoting errors.
2. Close what you open
Do: use with blocks, try-with-resources or using so connections and cursors close automatically.
Instead of: opening a new connection in a loop and never closing it.
Why: databases allow a limited number of connections. Leaked ones eventually cause "too many connections" errors.
3. Reuse connections sensibly
Do: open a connection once for a script, or use your framework's connection handling or pool in a web app.
Instead of: connecting and disconnecting for every single query.
Why: opening a connection takes time, especially to a server over the network.
4. Commit deliberately
Do: commit after a group of related changes, or use a block that commits on success.
Instead of: forgetting to commit and wondering where your data went, or committing after every row in a large import.
Why: commits mark finished work. Too few lose changes; too many make imports slow.
5. Catch specific errors
Do:
try:
...
except sqlite3.IntegrityError:
print("That email is already registered.")
Instead of: a bare except: that hides every problem, or no handling at all.
Why: specific errors let you respond helpfully and still notice real bugs.
6. Keep secrets out of code
Do: read connection strings and passwords from environment variables.
Instead of: typing the password into the source file.
Why: code is shared, copied and uploaded; passwords in it leak.
7. Use the driver your stack recommends
Do: follow your database's or framework's documentation.
Instead of: picking an obscure, unmaintained library from a search result.
Why: well-maintained drivers get security fixes and have better documentation.
8. Know your placeholder style
Do: check whether your driver uses ?, %s, $1 or @name.
Instead of: copying examples from a different driver and getting confusing errors.
How to check your own project
Open the code of a practice project and look for each habit in turn. Search for SQL strings built with + or formatting, for connections opened without a matching close, and for any password typed into a file. Fix one kind of problem at a time and run the program after each change. Most small projects can be brought up to these practices in an afternoon, and the exercise teaches you more about your driver than reading its documentation from start to finish.
Summary
| Practice | Prevents |
|---|---|
| Placeholders | SQL injection, quoting bugs |
| Closing | Connection leaks |
| Reusing connections | Slow apps |
| Deliberate commits | Lost or slow writes |
| Specific errors | Hidden bugs |
| Secrets outside code | Leaked passwords |
For side-by-side examples in four languages, see the thorough guide to drivers.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.