go - How do i use gorp select for an empty interface

402 views Asked by At

Hi i am using gorp and want to use select query for any table without actually knowing its schema for that i am using the query

db, err := sql.Open("mysql", "root:1234@tcp(localhost:3306)/information_schema")
checkErr(err, "sql.Open failed")
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{}}
var data []interface{}
_, err = dbmap.Select(&data, "select * from collations")
checkErr(err, "select query failed")
fmt.Println(data)

}

However this is resulting in an error because i can only pass a struct as first parameter to select

this returns an error

select query failed gorp: select into non-struct slice requires 1 column, got 6

suggest me some corrections or any other alternative so that i can use select query on any table name dynamically selected by user

1

There are 1 answers

0
Cedmundo On

If you don't know the schema, don't use GORP ... Why? because GORPs is a mapper, it needs a source and a target field to know how to process data, if you don't pass a target then GORP really doesn't know what to do.

However, you can do this using the standard SQL package. See this answer for more information: sql: scan row(s) with unknown number of columns (select * from ...)