Sqlite Pure Go

Sunday, November 6, 2022 · 1 minute · 183 words

Would it be possible to embed a database to avoid managing a MySQL or Postgres instance?

Even better, can this database be queried in SQL? I don’t want to learn another way to query data.

The answer is obviously yes (otherwise I will not write this post) thanks to a pure Go implementation of Sqlite3.

So here is a quick introduction to this package !

How to install it?

By adding a line in the import bloc :

_ "modernc.org/sqlite"

Don’t forget to run a go mod tidy to start solving the dependencies and automatically download the package.

How to use it ?

Sqlite can store its data in a file, therefore we need to specify such file :

db, err := sql.Open("sqlite", "/tmp/testbase.db")

How to request it?

Exactly like with another db, using the sql package. For instance, for a simple query that returns rows of results:

rows, err := db.Query("SELECT * FROM userinfo")

That’s it !

This package is a good way to embed a database cheaply in its program while keeping the SQL paradigm.

technical go sqlite