Skip to content
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

My code makes de select and prints <nil> for each row #260

Open
amichelins opened this issue Nov 21, 2018 · 5 comments
Open

My code makes de select and prints <nil> for each row #260

amichelins opened this issue Nov 21, 2018 · 5 comments

Comments

@amichelins
Copy link

My code makes de select and prints for each row

[CODE]
db, err := sql.Open("ora", "user/[email protected]:1521/ORCL")
defer db.Close()

rows, err := db.Query("SELECT A4_FILIAL, A4_COD, A4_NOME  FROM SA4010")
defer rows.Close()

if err != nil {
	fmt.Printf("%s", err)
	return
}

for rows.Next() {
	rows.Scan(rowP)
	fmt.Printf("%v", rowP[0])
	//fmt.Printf("{%v} -- {%v} ", rowP[0], rowP[1])
}

[/CODE]
How to get columns values?

@tgulacsi
Copy link
Collaborator

What is rowP ?

If you know the column types, then

for rows.Next() {
  var fil, nome string
  var cod int64
  if err = rows.Scan(&fil, &cod, &nome); err != nil {
    return err
  }
  fmt.Printf("%q, %d, %q", fil, cod, nome)
}

@amichelins
Copy link
Author

RowP is => rowP := make([]interface{}, 3)
I test your code and work perfectly.
thanks!!!!

@amichelins
Copy link
Author

I can return from rows.Scan to and struct?
The struct :
type tabspreco struct {
Filial string
Codpro string
Codtab string
Preco float32
}
The select:
SELECT DA1_FILIAL AS Filial, DA1_CODPRO AS Codpro,
DA1_CODTAB AS Codtab,
DA1_PRCAR AS Preco
FROM DA1010 WHERE DA1_FILIAL = '01' AND DA1_CODTAB = '068'

Each field has the name os struct element.

var Tabs2 tabspreco
err = rows.Scan(&Tabs2)

Is that possivel?

@tgulacsi
Copy link
Collaborator

rows.Scan(&Tabs2.Filial, &Tabs2.Codpro, &Tabs2.Codtab, &Tabs2.Preco)

or use a helper like github.com/jmoiron/sqlx or github.com/kisielk/sqlstruct

@amichelins
Copy link
Author

Thanks alot.
I install github.com/jmoiron/sqlx and solve the problem.

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

2 participants