How an App Talks to a Database, in Annotated Pictures
When your program "gets data from the database", several pieces work together. This tutorial draws them as a series of simple pictures. Sketch each one on paper as you read; the drawing is what makes it stick.
Picture 1: two boxes
Draw two boxes. Label the left one Your app and the right one Database server. Draw a line between them and label it network connection.
Caption: the app and the database are separate programs. Even on one laptop, they talk through a connection, much like a browser talks to a website.
Picture 2: the translator
Inside the app box, near its right edge, draw a small box labelled Driver.
Caption: a driver is a library that knows how to speak one database's language over the network. Python's psycopg for PostgreSQL, Java's JDBC drivers and the Node.js mysql2 package are all drivers. Your code talks to the driver; the driver talks to the database.
Picture 3: opening the connection
Draw an arrow from the driver to the database, labelled with a connection string such as:
postgresql://app_user@localhost:5432/shop
Caption: the connection string tells the driver which kind of database, where it is, which account to use and which database to open. The server checks the password and says yes or no. See reading a connection string piece by piece.
Picture 4: sending a question
Draw a second arrow carrying a piece of SQL:
SELECT name, price FROM products WHERE price < $1
and next to it a separate small label: $1 = 20.
Caption: the SQL and the values travel separately. That separation, called using placeholders or parameters, is what keeps your app safe from SQL injection.
Picture 5: the database does the work
Inside the database box, draw a small table of products and circle the rows priced under 20.
Caption: the database finds the matching rows itself. Your app does not download the whole table and search it; it only receives the answer.
Picture 6: results come back
Draw an arrow back to the app carrying a little grid of results. Inside the app box, draw that grid turning into a list your code can loop over.
Caption: the driver turns the database's reply into something your language understands, such as a list of rows or objects.
Picture 7: closing or reusing
Finally, draw a small box inside the app labelled Pool, holding three short lines.
Caption: opening a connection takes time, so real apps keep a few open in a connection pool and reuse them for each request instead of connecting every time.
Put it together
| Step | Piece involved |
|---|---|
| Find the database | Connection string |
| Speak its language | Driver |
| Ask the question | SQL with placeholders |
| Do the searching | Database server |
| Use the answer | Driver turning results into rows |
| Save time next request | Connection pool |
Try it
Run a tiny script that connects, selects one row and prints it. Then point to each picture and say which line of your code belongs to it. Our six-part beginner course has a version of this as its fifth part.
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.