"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Resolve "sql: expected 1 destination arguments in Scan" Error in Golang QueryRow with JSONB Data?

How to Resolve "sql: expected 1 destination arguments in Scan" Error in Golang QueryRow with JSONB Data?

Published on 2024-11-12
Browse:376

How to Resolve

“sql: expected 1 destination arguments in Scan” Error in Golang QueryRow with JSONB Data

The issue arises when attempting to use db.QueryRow to retrieve data from a JSONB column and storing the results in multiple destination arguments. However, the query returns only one field, leading to the "expected 1 destination arguments in Scan" error.

To resolve this issue, there are several approaches:

  • Return Multiple Fields in the Query:
    Modify the query to return each field as separate columns, allowing for each value to be stored in its own destination argument:
err := db.QueryRow("SELECT data->>'id', data->>'type', data->>'title' FROM message WHERE data->>'id'=$1"
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3