Skip to content

All columns on the query result are empty when the first field is empty #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DouglasPioStudiare opened this issue May 7, 2020 · 4 comments

Comments

@DouglasPioStudiare
Copy link

DouglasPioStudiare commented May 7, 2020

Issue description

When run query shoud return values as "", "123", but instead return "", "".

Example code

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"os"
)

type ResultStruct struct {
	field1 string `db:"field1"`
	field2 string `db:"field2"`
}

func main() {
	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", os.Getenv("USER"), os.Getenv("PASS"), os.Getenv("ENDPOINT"), os.Getenv("PORT"), os.Getenv("DBNAME")))
	if err = db.Ping(); err != nil {
		log.Fatal("Error : " + err.Error())
	}
	defer db.Close()

	params := "123,456,789,121"

	query := fmt.Sprintf(`select field1, field2 from test_table where field1 in (%s) or field2 in (%s)`, params, params)

	rows, selectErr := db.Query(query)
	if selectErr != nil {
		log.Fatal("Error : " + selectErr.Error())
	}

	for rows.Next() {
		var rs ResultStruct

                err := rows.Scan(&rs.field1, &rs.field2)
		if err != nil {
			log.Fatal("Error : " + err.Error())
		}

		log.Printf("field1: %v | field2: %v", rs.field1, rs.field2)
	}
}

Error log

Error : sql: Scan error on column index 0, name "field1": unsupported Scan, storing driver.Value type <nil> into type *string

Configuration

Driver version (or git SHA): Implements a Travis CI task (#1070) git: 681ffa8

Go version: 1.12.7

Server version: MySQL 5.6.48

Server OS: Manjaro Linux 18.0.4

@methane
Copy link
Member

methane commented May 8, 2020

It is not "reproducible example". Please provide schema and dataset too.

@peergynt
Copy link
Contributor

peergynt commented May 8, 2020

Does rows.Scan return an error?

@DouglasPioStudiare
Copy link
Author

That isn't a bug, that's a misunderstanding.

sql.NullString help avoid that problem.

However follows a database dump:
https://shrib.com/#database_dump

Thanks guy!

@DouglasPioStudiare
Copy link
Author

Can close this issue? Your have any more question?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants