添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱喝酒的双杠  ·  Dynamics Community ...·  1 月前    · 
严肃的豌豆  ·  WPF ICommand In MVVM·  1 年前    · 
Introducing Earthly Cloud. Consistent, Fast Builds, Any CI. Get 6,000 build min/mth free! Learn more .

This article explains how to integrate SQLite with Golang. Earthly simplifies the automation and containerization of Golang SQLite builds. Learn more about Earthly .

Welcome back. I’m an experienced developer, learning Golang by building an activity tracker. Last time I made a command-line client to connect to the JSON Service , but today is all about database persistence using database/sql .

If you’re curious about the basics of storing persistent data into a SQL database using Golang, this tutorial will be helpful for you. I’m going to be using sqlite3 , but I’ll add lots of headings, so you can skip ahead if sqlite is not your thing.

My plan is to add SQLite persistence to the backend service so that my workouts aren’t lost if the service goes down. And once I have that, I’ll add the --list command to my command line client and add an end point for it. it’s the type of feature that is simple to do with a SQL backend.

Install SQLite

The first thing I need is to set up my dev environment. I need to install SQLite3 and SQLite-utils:

really care about static types. Richard Hipp, the creator, doesn’t even like the term static types. He prefers to call them rigid types ( which he thinks are often a mistake.1)

Because of this stance, there is no statically verified TIME or DATETIME type in SQLite. Only INTEGER, REAL, TEXT, and BLOB. If you set the type as DATETIME, you can insert anything you want into it because it’s stored as TEXT on disk:

.sqliterc to get a nicer output out of sqlite3.

sqlite-utils also has a dump command, which is helpful if I want a text backup of my database contents to version control.

go-sqlite3 which I can install like this:

database

Golang Insert Into Database

I can now use my sql.db handle to insert data and get back the primary-key. The most concise way to do this is using db.Exec like this:

sql.QueryRow, which does just this:

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row’s Scan method is called. if the query selects no rows, the Row’s Scan will return ErrNoRows. Otherwise, theRow’s Scan scans the first selected row and discards the rest.

My usage looks like this:

convertAssign like this:

this example helpful for understanding what using prepared statements would look like.

Testing List

With that list method threaded through to /list I can start pulling out lists of items using curl:

Earthly to test my CI integration tests:

2

diff and the full code on GitHub.

What’s Next

Next, I’m planning to explore gRPC and protocol buffers, along with considering richer records and reporting options. Also if you’re building with Golang, consider giving Earthly a whirl for consistent and efficient builds.

And if you want to be notified about the next installment, sign up for the newsletter:

  • bbkane_ pointed out to me that SQlite now has a STRICT mode. It doesn’t support DateTime so far, but perhaps it one day will.↩︎

  • There is a machine translated pure Go SQLITE implementation that saves you from needing GCC, although it is slower and probably less extensively tested. Thanks again bbkane_↩︎

  • Spreading the word about Earthly. Host of CoRecursive podcast. Physical Embodiment of Cunningham's Law.
    @adamgordonbell ✉Email Adam✉