如果我运行以下代码,但用"SELECT * FROM User LIMIT 1"
替换'CALL',我会找回一个用户。如果我切换到调用存储过程,我会一直收到此错误:
panic: Error 1312: PROCEDURE MobiFit_Dev.User_ByEmail can't return a result set in the given context [recovered]
panic: Error 1312: PROCEDURE MobiFit_Dev.User_ByEmail can't return a result set in the given context
如何拨打我的SP?
这是我的SP:
DELIMITER $$
USE `MobiFit_Dev`$$
DROP PROCEDURE IF EXISTS `User_ByEmail`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `User_ByEmail`(pEmail VARCHAR(250))
BEGIN
SELECT
*
FROM
`User`
WHERE `Email` = pEmail ;
END$$
DELIMITER ;
包裹实体
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
"mobifit/db"
// "time"
)
const (
Male = "Male"
Female = "Female"
)
type User struct {
Id sql.NullInt64
Email sql.NullString
HashedPassword sql.NullString
RoleId sql.NullInt64
FirstName sql.NullString
LastName sql.NullString
Gender sql.NullString
DateOfBirth mysql.NullTime
Height sql.NullFloat64
CurrentWeight sql.NullFloat64
CreatedAt mysql.NullTime
ConfirmedAt mysql.NullTime
LastActivityAt mysql.NullTime
DeletedAt mysql.NullTime
}
func UserByEmail(email string) *User {
db := db.DB()
u := new(User)
rows, err := db.Query("CALL User_ByEmail(?)", email) << A SELECT *... works but this doesn't
if err != nil {
panic(err)
}
fmt.Println(rows.Columns())
for rows.Next() {
if err := rows.Scan(
&u.Id,
&u.Email,
&u.HashedPassword,
&u.RoleId,
&u.FirstName,
&u.LastName,
&u.Gender,
&u.DateOfBirth,
&u.Height,
&u.CurrentWeight,
&u.CreatedAt,
&u.ConfirmedAt,
&u.LastActivityAt,
&u.DeletedAt); err != nil {
panic(err)
}
}
if err := rows.Err(); err != nil {
panic(err)
}
fmt.Println(u)
return u
}
答案 0 :(得分:6)
使用MySQL,您当前无法调用存储过程,也不能立即执行多个语句。
答案 1 :(得分:3)
尝试使用单个qoutes。
喜欢这个
CALL User_ByEmail('?');
当输入参数的类型为VARCHAR
时,您应该始终在值周围使用单引号。