ora
ora copied to clipboard
My code makes de select and prints <nil> for each row
My code makes de select and prints
[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?
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)
}
RowP is => rowP := make([]interface{}, 3) I test your code and work perfectly. thanks!!!!
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?
rows.Scan(&Tabs2.Filial, &Tabs2.Codpro, &Tabs2.Codtab, &Tabs2.Preco)
or use a helper like github.com/jmoiron/sqlx or github.com/kisielk/sqlstruct
Thanks alot. I install github.com/jmoiron/sqlx and solve the problem.