本文整理汇总了Golang中database/sql.Rows类的典型用法代码### 示例。如果您正苦于以下问题:Golang Rows类的具体用法?Golang Rows怎么用?Golang Rows使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了Rows类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: ScanAll

/*
ScanAll accepts a pointer to a slice of a type and fills it with repeated calls to Scan.

ScanAll only works if you're trying to extract a single object from each row
of the query results. Additionally, it closes the passed sql.Rows object. ScanAll
effectively replaces this code

  // old code
  defer rows.Close()
  objs := []Object{}
  for rows.Next() {
      var obj Object
      Scan(rows, &obj)
      objs = append(objs, obj)
  }

With simply

  // new code
  objs := []Object{}
  ScanAll(rows, &objs)
*/
func ScanAll(rows *sql.Rows, slicePtr interface{}) error {
    defer rows.Close()

    sliceVal := reflect.ValueOf(slicePtr).Elem()

    if sliceVal.Kind() != reflect.Slice {
        return fmt.Errorf("Argument to crud.ScanAll is not a slice")
    }

    elemType := sliceVal.Type().Elem()

    if elemType.Kind() != reflect.Struct {
        return fmt.Errorf("Argument to crud.ScanAll must be a slice of structs")
    }

    for rows.Next() {
        newVal := reflect.New(elemType)

        if er := Scan(rows, newVal.Interface()); er != nil {
            return er
        }

        sliceVal.Set(reflect.Append(sliceVal, newVal.Elem()))
    }

    return nil
}

开发者ID:bhaumikshah23,项目名称:crud,代码行数:49,代码来源:scan.go

示例2: scanUser

func scanUser(user *data.User, rows *sql.Rows) error {
    var createTimeString string

    err := rows.Scan(
        &user.Username,
        &user.Password,
        &user.Salt,
        &user.Role,
        &user.TrustLevel,
        &createTimeString,
    )

    if err != nil {
        return err
    }

    createTime, err := time.Parse("2006-01-02 15:04:05", createTimeString) // this assumes UTC as timezone

    if err != nil {
        log.Println("User scanner failed to parse time.")
        return err
    }

    user.CreateTime = createTime

    return nil
}

开发者ID:falahhaprak,项目名称:rter,代码行数:27,代码来源:scanners.go

示例3: scanItemComment

func scanItemComment(comment *data.ItemComment, rows *sql.Rows) error {
    var updateTimeString string

    err := rows.Scan(
        &comment.ID,
        &comment.ItemID,
        &comment.Author,
        &comment.Body,
        &updateTimeString,
    )

    if err != nil {
        return err
    }

    updateTime, err := time.Parse("2006-01-02 15:04:05", updateTimeString) // this assumes UTC as timezone

    if err != nil {
        log.Println("ItemComment scanner failed to parse time. " + updateTimeString)
        return err
    }

    comment.UpdateTime = updateTime

    return nil
}

开发者ID:falahhaprak,项目名称:rter,代码行数:26,代码来源:scanners.go

示例4: sexDomain

func sexDomain(dom string) (sexAnswer bool, err error) {
    var stmt *sql.Stmt

    stmt, err = db.Prepare(`SELECT dom FROM domains WHERE dom = $1 AND sex = true LIMIT 1`)

    if err != nil {
        fmt.Printf("db.Prepare error: %v\n", err)
        return false, err
    }

    var rows *sql.Rows

    rows, err = stmt.Query(dom)
    if err != nil {
        fmt.Printf("stmt.Query error: %v\n", err)
        return false, err
    }

    defer stmt.Close()
    if rows.Next() {
        var domain string

        err = rows.Scan(&domain)
        if err != nil {
            fmt.Printf("rows.Scan error: %v\n", err)
            return false, err
        }
        if configuration.Debug == "Yes" {
            fmt.Println("domain => \"" + domain + "\"")
        }
        return true, err
    }

    return false, nil
}

开发者ID:beemoboy,项目名称:squid-helpers,代码行数:35,代码来源:filtering_http_api.go

示例5: FetchRow

/*
    Copies *sql.Rows into the slice of maps or structs given by the pointer dst.
*/
func (self *T) FetchRow(dst interface{}, rows *sql.Rows) error {

    dstv := reflect.ValueOf(dst)

    if dstv.IsNil() || dstv.Kind() != reflect.Ptr {
        return db.ErrExpectingPointer
    }

    itemv := dstv.Elem()

    columns, err := getRowColumns(rows)

    if err != nil {
        return err
    }

    next := rows.Next()

    if next == false {
        return db.ErrNoMoreRows
    }

    item, err := self.fetchResult(itemv.Type(), rows, columns)

    if err != nil {
        return err
    }

    itemv.Set(reflect.Indirect(item))

    return nil
}

开发者ID:pzduniak,项目名称:db,代码行数:35,代码来源:main.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng