Open marhar opened 1 year ago
@marhar sqlx
expects the target to be a slice of structs because it wants to map columns to struct fields.
You can perform raw database queries using database/sql
to achieve a similar effect as Python's fetchall(). Something like this
rows, err := db.Query(randomSelectString)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
results := make([][]interface{}, 0)
cols, _ := rows.Columns()
for rows.Next() {
columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i := range columns {
columnPointers[i] = &columns[i]
}
if err := rows.Scan(columnPointers...); err != nil {
log.Fatal(err)
}
results = append(results, columns)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
This is a bit inconvenient compared to using sqlx
and scanning into a slice of struct, but it's the trade-off for being able to handle arbitrary query result structures.
@marhar did you solve your problem?
I'm looking for a way to read a slice of slices, so I can write some general purpose routines to do, e.g., query formatting. Something like Python dbapi cursor.fetchall(), which returns data in the shape of [(1,2,3),(4,5,6)] etc.
Trying this
results in an error such as