One Query, Five Drivers: A Showcase of Database Code
Seeing the same task in several languages shows how similar database code really is. Each example below runs one parameterised query, "find books by an author", using a common driver. These are short teaching examples, not production code.
The query
SELECT title FROM books WHERE author = ?;
The ? is a placeholder. Some drivers use $1, %s or @author instead.
Python with sqlite3
import sqlite3
conn = sqlite3.connect("library.db")
rows = conn.execute("SELECT title FROM books WHERE author = ?", ("Ann",)).fetchall()
for (title,) in rows:
print(title)
conn.close()
Notice: the value travels as a tuple after the SQL. sqlite3 is built into Python.
Node.js with pg (PostgreSQL)
const { Client } = require("pg");
const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
const res = await client.query("SELECT title FROM books WHERE author = $1", ["Ann"]);
console.log(res.rows.map(r => r.title));
await client.end();
Notice: placeholders are numbered ($1), and the connection string comes from an environment variable.
Java with JDBC
try (Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement("SELECT title FROM books WHERE author = ?")) {
ps.setString(1, "Ann");
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) System.out.println(rs.getString("title"));
}
}
Notice: try (...) closes the connection automatically. Parameters are numbered from 1.
C# with Microsoft.Data.Sqlite
using var conn = new SqliteConnection("Data Source=library.db");
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT title FROM books WHERE author = @author";
cmd.Parameters.AddWithValue("@author", "Ann");
using var reader = cmd.ExecuteReader();
while (reader.Read()) Console.WriteLine(reader.GetString(0));
Notice: named parameters with @, and using to clean up.
PHP with PDO
$pdo = new PDO("sqlite:library.db");
$stmt = $pdo->prepare("SELECT title FROM books WHERE author = ?");
$stmt->execute(["Ann"]);
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $title) echo $title, "\n";
Notice: prepare then execute with an array of values.
What all five have in common
| Step | Every example does it |
|---|---|
| Open a connection | yes |
| Send SQL with a placeholder | yes |
| Pass the value separately | yes |
| Read rows back | yes |
| Close or clean up | yes |
Things to try
- Pick the language you know and run the example against a tiny
bookstable. - Change the author to one that does not exist. What comes back? (An empty result, not an error.)
- Try a value containing a quote mark, such as
O'Brien. Because of the placeholder, it just works.
For more on what drivers do behind the scenes, see Database Drivers at a Glance.
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.
Illustrative example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.