Coder Social home page Coder Social logo

Comments (3)

Fung-Chai avatar Fung-Chai commented on June 22, 2024

Using the delve debugger, I dug into the go-sql-driver code and discovered that Row.Scan calls binaryRows.Next and Rows.Scan calls textRows.Next. This explains why QueryRow returns the correct types but Query returns []uint8 for all columns. The question now is why Rows.Scan doesn't call binaryRows.Next as well. I will dig on.

from mysql.

Fung-Chai avatar Fung-Chai commented on June 22, 2024

Another discovery.

My original code, which makes use of Query produces the wrong results.

// Query will call the go-sql-driver's textRows.Next function, which presents []uint8 (strings) to all the scanners.
rows, err := db.Query("select * from fruits;")
// err is nil
defer rows.Close()
for rows.Next() {
        err := rows.Scan(id_scanner, name_scanner, year_scanner, amount_scanner, price_scanner)
        // err is nil, but output is wrong
}

But I can get the right output with Prepare, calling Query via the statement object.

statement, err := db.Prepare("select * from fruits;")
// err is ni
rows, err := statement.Query()
// err is nil
defer rows.Close()
for rows.Next() {
        err := rows.Scan(id_scanner, name_scanner, year_scanner, amount_scanner, price_scanner)
        // err is nil, and output is the same as that produced by Row.Scan
}

The mysqlConn.queyy returns textRows while mysqlStmt.query returns *binaryRows.

from mysql.

methane avatar methane commented on June 22, 2024

Using placeholder makes your query binary.
When implementing Scanner, you should expect many driver.Value type input, not single consistent one.

See #1452 for mitigations implemented in next release.

from mysql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.