sqlx

SQLX “missing destination name” when using table name in the struct tag

本小妞迷上赌 提交于 2021-01-29 19:35:27
问题 The issue is that when I use struct tags with an object, they do not work properly. I've worked on projects before that have done the same thing but have had no issue, but I can't figure out why. Example: this does not work: type Category struct { ID int `json:"id" db:"category.id"` Name string `json:"name" db:"category.name"` Description string `json:"description" db:"category.description"` } error received: missing destination name id in *[]Category this works fine: type Category struct {

How to use sqlx to query mysql IN a slice?

坚强是说给别人听的谎言 提交于 2019-12-19 07:35:30
问题 I want to query a table in mysql database for values IN a slice: var qids []int //fill qids dynamically err = database.SQL.Select(&quotes, "SELECT * FROM quote WHERE qid IN $1", qids) if err != nil { log.Println(err) } But I get this error: sql: converting Exec argument #0's type: unsupported type []int, a slice quotes [] How can I fix this? 回答1: sqlx has a great helper for that: In() we just have to prepare the query taking the args and Rebind, like this: var qids []int // fills qids on

Go language, scanning embeded struct with sqlx.StructScan

北城以北 提交于 2019-12-10 11:43:31
问题 I have just started learning Go language. I wrote following simple program. Here i am trying to fill struct with all Books and related Authers. Book struct has embeded Author struct. package main import ( "fmt" "log" "time" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) type Book struct { ID int Title string Year int Bauther Auther `db:"auther"` } type Auther struct { ID int Name string Dob time.Time } func main() { db, err := sqlx.Open("postgres", "host=localhost user=testuser dbname

Go SQL query inconsistency

送分小仙女□ 提交于 2019-12-09 14:05:33
问题 I am experiencing some really weird inconsistencies when executing queries, and was wondering if anyone knew why. Imagine I have a struct defined as follows: type Result struct { Afield string `db:"A"` Bfield interface{} `db:"B"` Cfield string `db:"C"` Dfield string `db:"D"` } And a MySQL Table with the following cols: A : VARCHAR(50) B : INT C : VARCHAR(50) D : VARCHAR(50) The query I would like to execute: SELECT A, B, C, D FROM table WHERE A="a" first way it can be executed: db.Get(&result

Go SQL query inconsistency

匆匆过客 提交于 2019-12-03 21:29:40
I am experiencing some really weird inconsistencies when executing queries, and was wondering if anyone knew why. Imagine I have a struct defined as follows: type Result struct { Afield string `db:"A"` Bfield interface{} `db:"B"` Cfield string `db:"C"` Dfield string `db:"D"` } And a MySQL Table with the following cols: A : VARCHAR(50) B : INT C : VARCHAR(50) D : VARCHAR(50) The query I would like to execute: SELECT A, B, C, D FROM table WHERE A="a" first way it can be executed: db.Get(&result, `SELECT A, B, C, D FROM table WHERE A="a"`) second way it can be executed: db.Get(&result, `SELECT A,

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

人盡茶涼 提交于 2019-12-03 06:46:00
问题 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" json:"email"` CreatedAt time.Time `db:"created_at" json:"created_at"` StatusID uint8 `db:"status_id" json:"status_id"` Deleted uint8 `db:"deleted" json:"deleted"` ... } And the table in MariaDB is defined as: +--------------+------------------+------+-----+-------------------+----------------+ | Field | Type | Null |

How to use sqlx to query mysql IN a slice?

回眸只為那壹抹淺笑 提交于 2019-12-01 06:39:27
I want to query a table in mysql database for values IN a slice: var qids []int //fill qids dynamically err = database.SQL.Select(&quotes, "SELECT * FROM quote WHERE qid IN $1", qids) if err != nil { log.Println(err) } But I get this error: sql: converting Exec argument #0's type: unsupported type []int, a slice quotes [] How can I fix this? sqlx has a great helper for that: In() we just have to prepare the query taking the args and Rebind, like this: var qids []int // fills qids on query dynamically query, args, err := sqlx.In("SELECT * FROM quote WHERE qid IN (?)", qids) if err != nil { log