| 
 | 1 | +# Getting started with SQLite  | 
 | 2 | + | 
 | 3 | +This tutorial assumes that the latest version of sqlc is  | 
 | 4 | +[installed](../overview/install.html) and ready to use.  | 
 | 5 | + | 
 | 6 | +Create a new directory called `sqlc-tutorial` and open it up.  | 
 | 7 | + | 
 | 8 | +Initialize a new Go module named `tutorial.sql.dev/app`  | 
 | 9 | + | 
 | 10 | +```shell  | 
 | 11 | +go mod init tutorial.sqlc.dev/app  | 
 | 12 | +```  | 
 | 13 | + | 
 | 14 | +sqlc looks for either a `sqlc.yaml` or `sqlc.json` file in the current  | 
 | 15 | +directory. In our new directory, create a file named `sqlc.yaml` with the  | 
 | 16 | +following contents:  | 
 | 17 | + | 
 | 18 | +```yaml  | 
 | 19 | +version: 2  | 
 | 20 | +sql:  | 
 | 21 | +  - engine: "sqlite"  | 
 | 22 | +    schema: "schema.sql"  | 
 | 23 | +    queries: "query.sql"  | 
 | 24 | +    gen:  | 
 | 25 | +      go:  | 
 | 26 | +        package: "tutorial"  | 
 | 27 | +        out: "tutorial"  | 
 | 28 | +```  | 
 | 29 | +
  | 
 | 30 | +sqlc needs to know your database schema and queries. In the same directory,  | 
 | 31 | +create a file named `schema.sql` with the following contents:  | 
 | 32 | + | 
 | 33 | +```sql  | 
 | 34 | +CREATE TABLE authors (  | 
 | 35 | +  id   INTEGER PRIMARY KEY AUTOINCREMENT,  | 
 | 36 | +  name text    NOT NULL,  | 
 | 37 | +  bio  text  | 
 | 38 | +);  | 
 | 39 | +```  | 
 | 40 | + | 
 | 41 | +Next, create a `query.sql` file with the following four queries:  | 
 | 42 | + | 
 | 43 | +```sql  | 
 | 44 | +-- name: GetAuthor :one  | 
 | 45 | +SELECT * FROM authors  | 
 | 46 | +WHERE id = ? LIMIT 1;  | 
 | 47 | +
  | 
 | 48 | +-- name: ListAuthors :many  | 
 | 49 | +SELECT * FROM authors  | 
 | 50 | +ORDER BY name;  | 
 | 51 | +
  | 
 | 52 | +-- name: CreateAuthor :one  | 
 | 53 | +INSERT INTO authors (  | 
 | 54 | +  name, bio  | 
 | 55 | +) VALUES (  | 
 | 56 | +  ?, ?  | 
 | 57 | +)  | 
 | 58 | +RETURNING *;  | 
 | 59 | +
  | 
 | 60 | +-- name: DeleteAuthor :exec  | 
 | 61 | +DELETE FROM authors  | 
 | 62 | +WHERE id = ?;  | 
 | 63 | +```  | 
 | 64 | + | 
 | 65 | +For SQL UPDATE if you do not want to return the updated record to the user, add this to the `query.sql` file:  | 
 | 66 | + | 
 | 67 | +```sql  | 
 | 68 | +-- name: UpdateAuthor :exec  | 
 | 69 | +UPDATE authors  | 
 | 70 | +set name = ?,  | 
 | 71 | +bio = ?  | 
 | 72 | +WHERE id = ?;  | 
 | 73 | +```  | 
 | 74 | + | 
 | 75 | +Otherwise, to return the updated record back to the user, add this to the `query.sql` file:  | 
 | 76 | + | 
 | 77 | +```sql  | 
 | 78 | +-- name: UpdateAuthor :one  | 
 | 79 | +UPDATE authors  | 
 | 80 | +set name = ?,  | 
 | 81 | +bio = ?  | 
 | 82 | +WHERE id = ?  | 
 | 83 | +RETURNING *;  | 
 | 84 | +```  | 
 | 85 | + | 
 | 86 | +You are now ready to generate code. Run the `generate` command. You shouldn't see any errors or output.  | 
 | 87 | + | 
 | 88 | +```shell  | 
 | 89 | +sqlc generate  | 
 | 90 | +```  | 
 | 91 | + | 
 | 92 | +You should now have a `tutorial` package containing three files.  | 
 | 93 | + | 
 | 94 | +```  | 
 | 95 | +├── go.mod  | 
 | 96 | +├── query.sql  | 
 | 97 | +├── schema.sql  | 
 | 98 | +├── sqlc.yaml  | 
 | 99 | +└── tutorial  | 
 | 100 | +    ├── db.go  | 
 | 101 | +    ├── models.go  | 
 | 102 | +    └── query.sql.go  | 
 | 103 | +```  | 
 | 104 | +
  | 
 | 105 | +You can use your newly generated queries in `app.go`.  | 
 | 106 | +
  | 
 | 107 | +```go  | 
 | 108 | +package main  | 
 | 109 | +
  | 
 | 110 | +import (  | 
 | 111 | +	"context"  | 
 | 112 | +	"database/sql"  | 
 | 113 | +	"log"  | 
 | 114 | +	"reflect"  | 
 | 115 | +
  | 
 | 116 | +	"tutorial.sqlc.dev/app/tutorial"  | 
 | 117 | +
  | 
 | 118 | +	_ "embed"  | 
 | 119 | +
  | 
 | 120 | +	_ "github.com/mattn/go-sqlite3"  | 
 | 121 | +)  | 
 | 122 | +
  | 
 | 123 | +//go:embed schema.sql  | 
 | 124 | +var ddl string  | 
 | 125 | +
  | 
 | 126 | +func run() error {  | 
 | 127 | +	ctx := context.Background()  | 
 | 128 | +
  | 
 | 129 | +	db, err := sql.Open("sqlite3", ":memory:")  | 
 | 130 | +	if err != nil {  | 
 | 131 | +		return err  | 
 | 132 | +	}  | 
 | 133 | +
  | 
 | 134 | +	// create tables  | 
 | 135 | +	if _, err := db.ExecContext(ctx, ddl); err != nil {  | 
 | 136 | +		return err  | 
 | 137 | +	}  | 
 | 138 | +
  | 
 | 139 | +	queries := tutorial.New(db)  | 
 | 140 | +
  | 
 | 141 | +	// list all authors  | 
 | 142 | +	authors, err := queries.ListAuthors(ctx)  | 
 | 143 | +	if err != nil {  | 
 | 144 | +		return err  | 
 | 145 | +	}  | 
 | 146 | +	log.Println(authors)  | 
 | 147 | +
  | 
 | 148 | +	// create an author  | 
 | 149 | +	insertedAuthor, err := queries.CreateAuthor(ctx, tutorial.CreateAuthorParams{  | 
 | 150 | +		Name: "Brian Kernighan",  | 
 | 151 | +		Bio:  sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},  | 
 | 152 | +	})  | 
 | 153 | +	if err != nil {  | 
 | 154 | +		return err  | 
 | 155 | +	}  | 
 | 156 | +	log.Println(insertedAuthor)  | 
 | 157 | +
  | 
 | 158 | +	// get the author we just inserted  | 
 | 159 | +	fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthor.ID)  | 
 | 160 | +	if err != nil {  | 
 | 161 | +		return err  | 
 | 162 | +	}  | 
 | 163 | +
  | 
 | 164 | +	// prints true  | 
 | 165 | +	log.Println(reflect.DeepEqual(insertedAuthor, fetchedAuthor))  | 
 | 166 | +	return nil  | 
 | 167 | +}  | 
 | 168 | +
  | 
 | 169 | +func main() {  | 
 | 170 | +	if err := run(); err != nil {  | 
 | 171 | +		log.Fatal(err)  | 
 | 172 | +	}  | 
 | 173 | +}  | 
 | 174 | +```  | 
 | 175 | + | 
 | 176 | +Before the code will compile, you'll need to add the Go SQLite driver.  | 
 | 177 | + | 
 | 178 | +```  | 
 | 179 | +go mod tidy  | 
 | 180 | +go build ./...  | 
 | 181 | +```  | 
 | 182 | + | 
 | 183 | +To make that possible, sqlc generates readable, **idiomatic** Go code that you  | 
 | 184 | +otherwise would have had to write yourself. Take a look in `tutorial/query.sql.go`.  | 
0 commit comments