A foundational project demonstrating SQL database operations using Go and SQLite. Built as part of working through structured Go challenges, this project covers the full CRUD lifecycle — Create, Read, Update, and Delete — using Go's standard database/sql package.
The project models a simple product inventory system. It defines a Product struct and a ProductStore type that wraps a *sql.DB connection, keeping database logic organized and reusable.
Stack:
- Language: Go
- Database: SQLite (via
mattn/go-sqlite3) - Driver:
database/sql(standard library)
| Operation | Method | Description |
|---|---|---|
| Create | CreateProduct |
Inserts a new product row and captures the generated ID |
| Read | GetProduct |
Retrieves a single product by ID using QueryRow and Scan |
| Update | UpdateProduct |
Updates all fields for a given product; checks rows affected |
| Delete | DeleteProduct |
Removes a product by ID; returns an error if not found |
sql-database-crud/
├── main.go # All database logic and entry point
├── go.mod # Module definition and dependencies
└── storedb.db # SQLite database file (created at runtime)
Requires Go and a C compiler (for the SQLite CGo bindings):
go run main.goExpected output:
DB initialised and products table ready
Inserted product with ID: 1
Got product: &{1 Keyboard 49.99 10 Electronics}
Updated product: &{1 Updated Name 19.99 50 Electronics}
Deleted product: &{1 Updated Name 19.99 50 Electronics}
This project is a deliberate step in building toward more practical Go applications. The patterns here — wrapping a *sql.DB in a store type, using parameterized queries, handling sql.ErrNoRows, and checking RowsAffected — are the same ones that appear in production backends.
The immediate goal is to carry these patterns forward into real projects, including integrating database persistence into mazey, an early-stage CLI reconnaissance tool, and other Go projects down the line.
- Parameterized queries (
?placeholders) are used throughout to prevent SQL injection. - The
ProductStorestruct keeps the*sql.DBscoped and makes the code easier to extend — for example, adding aListProductsmethod or swapping in PostgreSQL later. - This is an early-stage, learning-in-public project. The goal is to get the fundamentals right before building on top of them.