I'm stuck over to pass argument to the Exec
function on the basis of some conditions. I googled for this but found the solution with MySQL to use args := []interface{}{var1, var2, ... varN}
.
But my problem with PostgreSQL below is my conditional query:
strQry := `UPDATE core_channel SET title = $1, description = $2, `
if updateChannelReq.FeaturedVideo != 0 {
strQry += `default_video_id = $3, `
}
strQry += ` original_keywords = $4, banner_link = $5 WHERE create_user_id = $6`
And here how I'm passing argument to execute this query:
args := []interface{}{
updateChannelReq.Title,
updateChannelReq.Description,
}
if updateChannelReq.FeaturedVideo != 0 {
args = append(args, updateChannelReq.FeaturedVideo)
}
args = []interface{}{
updateChannelReq.OriginalKeyWords,
updateChannelReq.BannerLink,
AuthUserID,
}
_, err = stmt.Exec(args...)
But I'm getting the error:
pq: could not determine data type of parameter $3
please help me that how to manage these things. Thanks.
Change the condition by using else condition to skip the third column when creating query string.
strQry := `UPDATE core_channel SET title = $1, description = $2, `
if updateChannelReq.FeaturedVideo != 0 {
strQry += `default_video_id = $3, original_keywords = $4, banner_link = $5 WHERE create_user_id = $6 `
} else {
strQry += ` original_keywords = $3, banner_link = $4 WHERE create_user_id = $5`
}