Skip to content

jim3/go-sqlite-crud

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

SQL Database CRUD with Go

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.

Overview

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)

What It Covers

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

Project Structure

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)

Running It

Requires Go and a C compiler (for the SQLite CGo bindings):

go run main.go

Expected 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}

Why This Matters

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.

Notes

  • Parameterized queries (? placeholders) are used throughout to prevent SQL injection.
  • The ProductStore struct keeps the *sql.DB scoped and makes the code easier to extend — for example, adding a ListProducts method 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.

About

SQL Database CRUD with Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages