K
K
Kostyan Kondr2017-02-14 13:10:49
SQL
Kostyan Kondr, 2017-02-14 13:10:49

How to get the result of a sql query in go in parts?

I'm trying to query a database that returns a lot of data. How can you process them as they are received, let's say 1000 rows each? Allows such standard database/sql?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Tomilov, 2017-02-14
@kondr1

Allows by default, but not by a thousand rows, but in the form of a cursor that moves with each call to rows.Next(), which you can read more about in the database/sql documentation:
https://golang.org/pkg/database/sql /#Rows
There is also an example that illustrates this:

rows, err := db.Query("SELECT ...")
...
defer rows.Close()
for rows.Next() {
    var id int
    var name string
    err = rows.Scan(&id, &name)
    ...
}
err = rows.Err() // get any error encountered during iteration
...

Only Go has different drivers written for specific databases: https://github.com/golang/go/wiki/SQLDrivers
for example, immediately all in a slice and gives out elements from it one by one.

R
Roman Mirilaczvili, 2017-02-14
@2ord

www.stackoverflow.com/a/9261762/2840001

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question