sql - 如果未找到行,QueryRow().Scan() 将返回错误。怎么解决?

标签 sql postgresql go

我花了很多时间试图解决这个问题。

我有一个结构:

type Token struct {
    Id             *int64     `db:"id"`
    Email          *string    `db:"email"`
    OperationType  *string    `db:"operation_type"`
    Token          *string    `db:"token"`
    ExpirationDate *time.Time `db:"expiration_date"`
}

我有一个通过电子邮件找到一个 token 的函数:

func (r Repo2) FindOneByEmail(ctx context.Context, email string, ct *Token) error {
    row := r.DB.QueryRow(`
        SELECT id, email, operation_type, token, expiration_date 
        FROM tokens 
        WHERE email=$1 AND type=$2 AND expiration_date>$3::date`,
        email, "registration", time.Now(),
    )

    err := row.Scan(&ct.Id, &ct.Email, &ct.OperationType, &ct.Token, &ct.ExpirationDate)
    if err != nil {
        return err
    }

    return nil
}

db 中的某些字段可以为空(这就是我在结构中使用指针的原因)

但是当我执行 .Scan 时,它会抛出错误(因为值为空,它不能使用地址“&”)

但是如果我删除“&”,它也会抛出错误“无效的内存地址或零指针取消引用”

那么我该如何解决这个问题呢?

想法是:如果找到一行,则需要获取字段值,但如果未找到行,则需要抛出 sql.ErrNoRows

最佳答案

pgx驱动解决方案:

从结构中删除指针

type Token struct {
    Id             int64     `db:"id"`
    Email          string    `db:"email"`
    OperationType  string    `db:"operation_type"`
    Token          string    `db:"token"`
    ExpirationDate time.Time `db:"expiration_date"`
}

重写函数

func (r Repo2) FindOneByEmail(ctx context.Context, email string, ct *Token) error {
    var date pgtype.Timestamptz

    row := r.DB.QueryRow(`
        SELECT id, email, operation_type, token, expiration_date 
        FROM tokens 
        WHERE email=$1 AND type=$2 AND expiration_date>$3::date`,
        email, "registration", time.Now().UTC(),
    )

    err := row.Scan(&ct.Id, &ct.Email, &ct.OperationType, &ct.Token, &date)
    if err != nil {
        return err
    }

    ct.ExpirationDate = date.Time

    return nil
}

关于sql - 如果未找到行,QueryRow().Scan() 将返回错误。怎么解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444649/

相关文章:

python - 添加 hstore 字段后如何创建迁移? (django-hstore 与南方)

ruby-on-rails - Postgresql 和 ActiveRecord 其中 : Regex matching

sql - 导出数据库的 CREATE 脚本

sql - 按组的第一个值对值进行分组

sql - 将具有公共(public) ID 的行压缩为一行

sql - 如何让 ADO 将 .csv 文件中的所有日期识别为美国日期?

python mysql删除语句不起作用

google-app-engine - 您如何运行带有客户端的 Google App Engine Dev Server for Google Cloud Storage,其中一切都在本地模拟?

go - 文本/模板问题 Parse() 与 ParseFiles()

sorting - Golang 自定义排序比原生排序更快