【英文】Go语言通过sqlx模块操作数据库报错

Preface

An error occurs when using the sqlx module in Go language to operate the database: Scan error on column index 2, name "xxx": converting NULL to string is unsupported

Cause

  • It is not possible to assign an empty value to a string data type.

Solution

  • Change string to *string:
1
2
3
4
type User struct {
Username *string `db:"Username"`
Password *string `db:"Password"`
}

Complete

References

CSDN - AtlanSI