Golang:如何正确处理动态选择查询。

huangapple go评论82阅读模式
英文:

Golang: how to proper handle dynamic select query

问题

处理Go语言中的select SQL查询结果的最佳方法是什么?

根据你提供的代码,以下是处理select查询结果的最佳方法:

func (s *SQLServiceServer) select_query_func() {
    // 连接数据库并进行验证

    rows, err1 := db.Query(request.GetQuery())

    if err1 != nil {
        panic(err1)
    }

    defer rows.Close()

    columns, err2 := rows.Columns()

    if err2 != nil {
        panic(err2)
    }

    // 循环遍历每一行数据,使用Scan将列数据赋值给结构体字段
    for rows.Next() {
        values := make([]interface{}, len(columns))

        for i := range values {
            values[i] = new(interface{})
        }
        if err2 = rows.Scan(values...); err2 != nil {
            panic(err2)
        }

        for i := range values {
            fmt.Println(values[i])
        }
    }

    return requestOutput(REQUEST_OK, RESULT_OK, ERROR_NULL)
}

总结一下,以上代码是处理select查询结果的最佳方法。

至于你提到的错误,我无法查看图片链接,请提供文字描述或其他方式来说明错误的具体内容。

英文:

what's the best way to handle the result of a select sql query in go?

Context(thats what i got so far...):

@Update

func (s *SQLServiceServer) select_query_func() {
	
    // db_connection and validation

	rows, err1 := db.Query(request.GetQuery())

	if err1 != nil {
		panic(err1)
	}

	defer rows.Close()

	columns, err2 := rows.Columns()

	if err2 != nil {
		panic(err2)
	}

	// Loop through rows, using Scan to assign column data to struct fields.
	for rows.Next() {
		values := make([]interface{}, len(columns))

		for i := range values {
			values[i] = new(interface{})
		}
		if err2 = rows.Scan(values...); err2 != nil {
			panic(err2)
		}

		for i := range values {
			fmt.Println(values[i])
		}
	}

	return requestOutput(REQUEST_OK, RESULT_OK, ERROR_NULL)
}

To sumarize, what's the best approach to do it?

The error that i mentioned

Golang:如何正确处理动态选择查询。

答案1

得分: 3

Scan 需要作为参数的指针是已分配的、即非空的。以下类型是允许的:

  • *string
  • *[]byte
  • *int, *int8, *int16, *int32, *int64
  • *uint, *uint8, *uint16, *uint32, *uint64
  • *bool
  • *float32, *float64
  • *interface{}
  • *RawBytes
  • *Rows(游标值)
  • 任何实现了 Scanner 接口的类型(请参阅 Scanner 文档)

因此,要修复你的代码,你需要用非空指针填充 values 切片,而且对于你的目的,这些指针可以是 *interface{} 类型。

for rows.Next() {
    values := make([]interface{}, len(columns))
    for i := range values {
        values[i] = new(interface{})
    }
    if err := rows.Scan(values...); err != nil {
        return err
    }
}
英文:

Scan requires allocated, i.e. non-nil, pointers as arguments. The following types are allowed:

> none
> *string
> *[]byte
> *int, *int8, *int16, *int32, *int64
> *uint, *uint8, *uint16, *uint32, *uint64
> *bool
> *float32, *float64
> *interface{}
> *RawBytes
> *Rows (cursor value) any type implementing Scanner (see Scanner docs)
>

So to fix your code you need to populate the values slice with non-nil pointers, and for your purpose those pointers can be of type *interface{}.

for rows.Next() {
    values := make([]interface{}, len(columns))
    for i := range values {
        values[i] = new(interface{})
    }
    if err := rows.Scan(values...); err != nil {
        return err
    }
}

huangapple
  • 本文由 发表于 2022年4月8日 20:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/71797042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定