英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论