在Go语言中使用QueryRowContent调用存储过程

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

Calling Stored Procedure in GoLang with QueryRowContent

问题

我正在尝试运行下面的代码段,但是在QueryRowContext中出现了以下错误:

> sql: 期望 0 个参数,但给出了 2 个

这种方法在普通的 SQL 查询中是有效的,但是当我尝试使用 CALL 关键字调用存储过程时,就会出现这个问题。

import (
	"database/sql"
	"net/http"
)

func VerifyUser(user User) (*User, string, error) {
	db, ctx := db.GetDB()
	query := "CALL usp_GetUserByUsername(@Email)"

	stmt, err := db.Prepare(query)
	if err != nil {
		log.Errorln("Error in preparing statement. " + err.Error())
		return nil, "Error in preparing statement.", err
	}
	defer stmt.Close()

	row := stmt.QueryRowContext(ctx, sql.Named("Email", user.Email))

	var retUser User
	err = row.Scan(&retUser.ID, &retUser.Email, &retUser.Password, &retUser.Status)
	if err != nil {
		log.Warningln("Unknown Email: " + user.Email + ". " + err.Error())
		return nil, "Invalid user.", err
	}

这里有什么问题?提前感谢。

英文:

Im trying to run the below code segment and keeps throwing the below error from QueryRowContext

> sql: expected 0 arguments, got 2

This approach works with plain SQL queries and I keep getting the issue when I try to call a Stored Prod to the query param with the CALL keyword.

import (
"database/sql"
"net/http"
)

func VerifyUser(user User) (*User, string, error) {
  db, ctx := db.GetDB()
  query := "CALL usp_GetUserByUsername(@Email)"

stmt, err := db.Prepare(query)
if err != nil {
	log.Errorln("Error in preparing statement. " + err.Error())
	return nil, "Error in preparing statement.", err
}
defer stmt.Close()

row := stmt.QueryRowContext(ctx, sql.Named("Email", user.Email))

var retUser User
err = row.Scan(&retUser.ID, &retUser.Email, &retUser.Password, &retUser.Status)
if err != nil {
	log.Warningln("Unknown Email: " + user.Email + ". " + err.Error())
	return nil, "Invalid user.", err
}

What seems to be wrong here? Thanks in advance.

答案1

得分: 1

import (
    "database/sql"
    "net/http"
)

func VerifyUser(user User) (*User, string, error) {
    db, ctx := db.GetDB()
    query := "CALL usp_GetUserByUsername(?)"

    stmt, err := db.Prepare(query)
    if err != nil {
        log.Errorln("准备语句时出错。" + err.Error())
        return nil, "准备语句时出错。", err
    }
    defer stmt.Close()

    row := stmt.QueryRowContext(ctx, user.Email)

    var retUser User
    err = row.Scan(&retUser.ID, &retUser.Email, &retUser.Password, &retUser.Status)
    if err != nil {
        log.Warningln("未知的电子邮件:" + user.Email + "。" + err.Error())
        return nil, "无效的用户。", err
    }
}

将查询中的@Email替换为?,并将电子邮件传递给QueryRowContext而不是命名语句。

英文:
import (
"database/sql"
"net/http"
)

func VerifyUser(user User) (*User, string, error) {
  db, ctx := db.GetDB()
  query := "CALL usp_GetUserByUsername(?)"

stmt, err := db.Prepare(query)
if err != nil {
    log.Errorln("Error in preparing statement. " + err.Error())
    return nil, "Error in preparing statement.", err
}
defer stmt.Close()

row := stmt.QueryRowContext(ctx, user.Email)

var retUser User
err = row.Scan(&retUser.ID, &retUser.Email, &retUser.Password, &retUser.Status)
if err != nil {
    log.Warningln("Unknown Email: " + user.Email + ". " + err.Error())
    return nil, "Invalid user.", err
}

Replace @Email in your query with ? and pass the email into QueryRowContext not named statement

huangapple
  • 本文由 发表于 2022年7月21日 01:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73056245.html
匿名

发表评论

匿名网友

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

确定