|
| 1 | +--- |
| 2 | +title: Gin Quick Start Guide |
| 3 | +description: Get started with SQLite Cloud using Gin. |
| 4 | +category: getting-started |
| 5 | +status: publish |
| 6 | +slug: quick-start-gin |
| 7 | +--- |
| 8 | + |
| 9 | +In this quickstart, we will show you how to get started with SQLite Cloud and Go by building a simple Gin application that connects to and reads from a SQLite Cloud database. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +1. **Set up a SQLite Cloud account** |
| 14 | + - If you haven't already, [sign up for a SQLite Cloud account](https://sqlitecloud.io/register) and create a new database. |
| 15 | + - In this guide, we will use the sample datasets that come pre-loaded with SQLite Cloud. |
| 16 | + |
| 17 | +2. **Create a Gin app** |
| 18 | + - You should have [Go installed](https://go.dev/doc/install) locally. |
| 19 | + - Set up your Go workspace. |
| 20 | +```bash |
| 21 | +mkdir sqlc-quickstart |
| 22 | +cd sqlc-quickstart |
| 23 | + |
| 24 | +go mod init example.com/sqlc-quickstart |
| 25 | +``` |
| 26 | + - Create a file called `app.go`. |
| 27 | + - Add the following code to your `app.go` file. |
| 28 | +```go |
| 29 | +package main |
| 30 | + |
| 31 | +import "fmt" |
| 32 | +``` |
| 33 | + - Import the Gin package in your Go source code. |
| 34 | +```go |
| 35 | +import "github.com/gin-gonic/gin" |
| 36 | +``` |
| 37 | + - Run the `go mod tidy` command to synchronize your module's dependencies. |
| 38 | +```bash |
| 39 | +$ go mod tidy |
| 40 | +go: finding module for package github.com/gin-gonic/gin |
| 41 | +go: found github.com/gin-gonic/gin in github.com/gin-gonic/gin v1.10.0 |
| 42 | +go: downloading github.com/google/go-cmp v0.5.5 |
| 43 | +``` |
| 44 | + |
| 45 | +3. **Install the SQLite Cloud package** |
| 46 | +- Import the package in your Go source code. |
| 47 | +```go |
| 48 | +import sqlitecloud "github.com/sqlitecloud/sqlitecloud-go" |
| 49 | +``` |
| 50 | +- Download the package, and run the `go mod tidy` command to synchronize your module’s dependencies. |
| 51 | +```bash |
| 52 | +$ go mod tidy |
| 53 | +go: downloading github.com/sqlitecloud/sqlitecloud-go v1.0.0 |
| 54 | +``` |
| 55 | + |
| 56 | +4. **Connect with a valid SQLite Cloud connection string** |
| 57 | +```go |
| 58 | +sqlitecloud://{username}:{password}@{host}.sqlite.cloud:8860/{database} |
| 59 | +``` |
| 60 | +- To get your admin username, go to your SQLite Cloud account dashboard. In the left nav, open Security and select Users. Your admin username has already been created. Replace `{username}` in the connection string. |
| 61 | +- To set your admin user’s password, click the row’s down chevron and select Edit. Enter a new Password and click Save. Replace `{password}` in the connection string. |
| 62 | +- To get the host, see under your Project name `{host}.sqlite.cloud`. |
| 63 | +- To get the database name, in the left nav, open Databases and select Tables. All of your databases are listed in the Select Database dropdown. |
| 64 | + |
| 65 | + |
| 66 | +5. **Query data** |
| 67 | + - Copy the following code into the `app.go` file. |
| 68 | + - Replace `<your-connection-string>`. |
| 69 | + |
| 70 | +```go |
| 71 | +type Artist struct { |
| 72 | + ArtistID int64 `json:"artist id"` |
| 73 | + Name string `json:"name"` |
| 74 | +} |
| 75 | + |
| 76 | +func readArtists(resultSet *sqlitecloud.Result) ([]Artist, error) { |
| 77 | + var artists []Artist |
| 78 | + |
| 79 | + for r := uint64(0); r < resultSet.GetNumberOfRows(); r++ { |
| 80 | + id, err := resultSet.GetInt64Value(r, 0) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + name, err := resultSet.GetStringValue(r, 1) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + artists = append(artists, Artist{ |
| 91 | + ArtistID: id, |
| 92 | + Name: name, |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + return artists, nil |
| 97 | +} |
| 98 | + |
| 99 | +func main() { |
| 100 | + r := gin.Default() |
| 101 | + |
| 102 | + r.GET("/artists", func(c *gin.Context) { |
| 103 | + const connectionString = "<your-connection-string>" |
| 104 | + |
| 105 | + db, err := sqlitecloud.Connect(connectionString) |
| 106 | + if err != nil { |
| 107 | + fmt.Println("Connect error: ", err) |
| 108 | + panic("Connect error") |
| 109 | + } |
| 110 | + |
| 111 | + dbResult, err := db.Select("SELECT * FROM artists LIMIT 10;") |
| 112 | + if err != nil { |
| 113 | + fmt.Println("Select error: ", err) |
| 114 | + panic("Select error") |
| 115 | + } |
| 116 | + |
| 117 | + artists, err := readArtists(dbResult) |
| 118 | + if err != nil { |
| 119 | + fmt.Println("Read artists error: ", err) |
| 120 | + panic("Read artists error") |
| 121 | + } |
| 122 | + |
| 123 | + c.JSON(200, artists) |
| 124 | + |
| 125 | + }) |
| 126 | + |
| 127 | + r.Run() // listen and serve on 0.0.0.0:8080 |
| 128 | +} |
| 129 | + |
| 130 | +``` |
| 131 | + |
| 132 | +6. **Run your app** |
| 133 | + |
| 134 | +```bash |
| 135 | +$ go run app.go |
| 136 | +``` |
| 137 | + |
| 138 | +7. **View your app** |
| 139 | + - Open your browser and navigate to `localhost:8080/artists`. |
| 140 | + |
| 141 | +And that's it! You've successfully built a Gin app that reads data from a SQLite Cloud database. |
0 commit comments