Questions tagged [sqlx]

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Major additional concepts are:

  • Marshal rows into structs (with embedded struct support), maps, and slices
  • Named parameter support including prepared statements
  • Get and Select to go quickly from query to struct/slice
  • LoadFile for executing statements from a file

There is now some fairly comprehensive documentation for sqlx. You can also read the usage below for a quick sample on how sqlx works, or check out the API documentation on godoc.

193 questions
81
votes
4 answers

unsupported Scan, storing driver.Value type []uint8 into type *time.Time

I have difficulty querieing for users, which is defined as: type User struct { ID int `db:"id" json:"id"` UserName string `db:"username" json:"username"` Email string `db:"email"…
Karlom
  • 13,323
  • 27
  • 72
  • 116
42
votes
4 answers

Efficiently mapping one-to-many many-to-many database to struct in Golang

Question When dealing with a one-to-many or many-to-many SQL relationship in Golang, what is the best (efficient, recommended, "Go-like") way of mapping the rows to a struct? Taking the example setup below I have tried to detail some approaches with…
Ewan
  • 14,592
  • 6
  • 48
  • 62
42
votes
2 answers

Go: How to get last insert id on Postgresql with NamedExec()

I use jmoiron/sqlx library for communicating with my PostgreSql server in my Go apps. Somewhere on my apps i have this following code: sqlQuery := ` INSERT INTO table_to_insert ( code, status, create_time, …
Wendy Adi
  • 1,397
  • 3
  • 18
  • 26
27
votes
1 answer

sqlx - non-struct dest type struct with >1 columns (2)

I have searched the error and I have find two questions: This one, but my question is not duplicate of it And this one, but there is no answer in this question. Here is my code: package main import ( "log" "github.com/jmoiron/sqlx" …
ceth
  • 44,198
  • 62
  • 180
  • 289
17
votes
1 answer

How can I log all outgoing SQL statements from Go / MySQL?

I'm using a non-framework Go stack with sqlx, and MySQL for a web project. I would like to log all outgoing SQL statements for debugging. Is it possible to do this? Hopefully looking to get output like this (copied from a Rails project): User…
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
14
votes
3 answers

How to use sqlx to query mysql IN a slice?

I want to query a table in mysql database for values IN a slice: var qids []int //fill qids dynamically err = database.SQL.Select("es, "SELECT * FROM quote WHERE qid IN $1", qids) if err != nil { log.Println(err) } But I get this…
Karlom
  • 13,323
  • 27
  • 72
  • 116
14
votes
1 answer

golang sqlx "missing destination name ...."

I have error when use sqlx with postgresql : "missing destination name rec_created_by" type Client struct { ClientID string `json:"client_id" db:"id"` Name string `json:"name" …
Tuan Hoang Anh
  • 994
  • 12
  • 31
9
votes
3 answers

Get back newly inserted row in Postgres with sqlx

I use https://github.com/jmoiron/sqlx to make queries to Postgres. Is it possible to get back the whole row data when inserting a new row? Here is the query I run: result, err := Db.Exec("INSERT INTO users (name) VALUES ($1)", user.Name) Or should…
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
7
votes
3 answers

How to use wildcard in sqlx query in Go?

I'm using sqlx with mysql database and want to query author table for names which start with certain letters. Here is the query: sqlx.DB.Select(&authors, "SELECT * FROM author WHERE first_name LIKE ?% OR last_name LIKE ?%", letter,letter) but I get…
Karlom
  • 13,323
  • 27
  • 72
  • 116
6
votes
3 answers

How to scan into nested structs with sqlx?

Let's assume that I have two models, type Customer struct { Id int `json:"id" db:"id"` Name string `json:"name" db:"name"` Address Address `json:"adress"` } type Address struct { Street…
kuzua
  • 177
  • 2
  • 9
6
votes
1 answer

struct Time property doesn't load from Go sqlx library

I have a struct with a property time: type Basket struct { ... Created_at time.Time `db:"created_at"` } with the time saved as: basket.Created_at = time.Now().UTC() If I save it using Insert sql statement, it saves the time nicely in the…
Shah-G
  • 652
  • 1
  • 8
  • 22
5
votes
1 answer

intermittent "connection reset by peer" sql postgres

After a period of inactivity, my go web service is getting a net.OpError with message read tcp x.x.x.x:52086->x.x.x.x:24414: read: connection reset by peer when executing the first postgres sql query. After the error, the subsequent requests will…
Nick
  • 61
  • 1
  • 4
5
votes
1 answer

Bindvars in golangs sqlx.DB.Select() statement requires 0 parameters

I'm using SQLX and PQ to query an SQL database with PostGress. I'm using the function Select from SQLX with bindvars but PQ panics with pq: got 1 parameters but the statement requires 0. query = ` SELECT count(*) AS count FROM…
Luis Serra
  • 131
  • 1
  • 7
5
votes
0 answers

Using sqlx, to populated embedded structs from a table joined twice

My question in a nutshell: can I use sqlx's StructScan to populate two embedded structs with values sourced from the same SQL table joined twice? The help files to the useful sqlx package state this: A StructScan will set an id column result in…
Brent
  • 805
  • 9
  • 20
5
votes
3 answers

Sqlx "missing destination name" for struct tag through pointer

I have a model like this: type Course struct { Name string `db:"course_name"` } type Group struct { Course *Course } type Groups []Group And when i try to do sqlx.Select for Groups with a query like this: SELECT c.name as course_name FROM…
evocatus
  • 111
  • 1
  • 2
  • 8
1
2 3
12 13