Skip to content

Query

Query 方法用给定的参数执行一条准备好的查询语句,并将查询结果作为 *Rows 返回。

函数语法

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

示例

package main

import (
   _ "./go-driver-xugusql"
   "database/sql"
   “log”
)

func main() {
   db, _ := sql.Open("xugusql", "IP=192.168.78.130;DB=SYSTEM;User=SYSDBA;"
       + "PWD=SYSDBA;Port=5138;AUTO_COMMIT=on;CHAR_SET=UTF8")

   stmt, _ := db.Prepare(“select * from go_test where id = ?;”)
   rows, err := stmt.Query(3)
   if err != nil {
       log.Fatal(err)
   }

   var cols []string 
   cols, err = rows.Columns()
   if err != nil {
       log.Fatal(err)
   }
   pvals := make([]interface{}, len(cols))
   for key, _ := range pvals {

       dest := make([]byte, 216)
       pvals[key] = &dest
   }  /* end for */

   for rows.Next() {
       err = rows.Scan(pvals...)
       if err != nil {
           log.Fatal(err)
       }

       for _, v := range pvals {
           fmt.Printf(“%s\t”, string(*(v.(*[]byte))))
       }
       fmt.Printf("\n")
   }

   rows.Close()
}