doumo6356 2018-08-13 18:02
浏览 127
已采纳

Golang从Postgresql函数获取值,该函数返回一个int但也接收一个字符串数组

I have a postgresql function that basically returns a number, but also as you can see the function receive an array of string.

Create Or Replace Function fnRegisterUserRoleArray(idUserFather int, rolesArray Text[]) returns int language plpgsql
as
$body$
declare ids INT;
declare roleID INT;
declare sanitazedRole TEXT;
declare counter int = 0;
begin
if(empty2null(idUserFather::text) is null) then
    ids := 0;
elsif exists( select 1 from win_users where id_user = idUserFather limit 1) then
    for counter in 1 .. array_upper(rolesArray, 1)
    loop
        select id_role from win_roles where rolename = rolesArray[counter] into roleID;
        insert into win_user_role(id_user, id_role) values (idUserFather, roleID);
        ids := ids + 1;
    end loop;
else
    ids := 0;
end if;
return ids;
end $body$;

and in my Go Function I have my variable database that receive the connection with the postgresql database,

database, err := getConnection()

however when I call the function fnRegisterUserViewsPermission and send the values I receive an error.

This is how I set the values:

var resultRole int 
err = database.QueryRow("Select fnRegisterUserRoleArray($1, $2);", resultUser, pq.Array(roleArray)).Scan(&resultRole, &int)
    fmt.Println(resultRole)
    if err != nil {
        fmt.Println(resultRole)
        return nil, err
    }
    if resultRole != 0 && resultRole != 1 {
        response = response + "their roles has been assigned correctly "
    } else {
            response = response + "however there was an error during the assignation of the role."
    }

and the output that I receive is this:

"message": "sql: Scan error on column index 0: converting driver.Value type (\"\") to a int: invalid syntax",

But the values get stored in my database, so the function receives in a good way the values, but the return is where it goes bam :(

This only occurs when i send an Array but if i send anything rather than an array the Scanner return the Id obtained from the postgresql function.

Is there any way to obtain the result id but also send the array in a more elegant way?

this is the values that contains role array:

[Carrier Brand]

And the value that contains resultUser is just a number, that represent the id of the user that has been registered in the database, in this case the registered user has an ID of...

result user: 63

Thanks! :)

  • 写回答

1条回答 默认 最新

  • dqt20140129 2018-08-13 18:50
    关注

    As the error says:

    "message": "sql: Scan error on column index 0: converting driver.Value type (\"\") to a int: invalid syntax",

    The error is when scanning the result coming from the function when the value is returned from it.

    Remove the pointer to int primitive type, Since only single value is returned form the function which is of int type.

    Scan the value into int type variable as

    var resultRole int 
    err = database.QueryRow("Select fnRegisterUserRoleArray($1, $2);", resultUser, pq.Array(roleArray)).Scan(&resultRole)
    

    For handling these types of situations it will be better if you handle scanning the result separately from the query as:

    sqlStatement := `Select fnRegisterUserRoleArray($1, $2);`
    var resultRole int 
    // Replace 3 with an ID from your database or another random
    // value to test the no rows use case.
    row := db.QueryRow(sqlStatement, resultUser, pq.Array(roleArray))
    switch err := row.Scan(&resultRole); err {
    case sql.ErrNoRows:
      fmt.Println("No rows were returned!")
    case nil:
      fmt.Println(resultRole)
    default:
      panic(err)
    }
    

    Using above approach of separating queryRow with Scan help you to analyze more about the result and returned errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题