登录
首页 >  Golang >  Go问答

出现 UPDATE 和 DELETE SQL 中的“错误连接”是由于 GOLANG MySQL 驱动程序导致的问题吗?

来源:stackoverflow

时间:2024-02-26 21:45:29 138浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《出现 UPDATE 和 DELETE SQL 中的“错误连接”是由于 GOLANG MySQL 驱动程序导致的问题吗?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我在使用 go lang (1.18) 尝试使用 mysql (5.7) 进行第一次 crud 时遇到了困难。 create & read sql 不返回错误,但在 update & delete 中返回错误。我正在搜索,看起来像是连接配置错误或错误...但我不知道如何跟踪它。

mysql 驱动程序:github.com/go-sql-driver/mysql v1.6.0

我的仓库:https://github.com/ramadoiranedar/go_rest_api

错误是

[mysql] 2022/09/22 00:04:44 connection.go:299: invalid connection
driver: bad connection

我的 sql 函数如下所示:

  • 连接
func newdb() *sql.db {
    db, err := sql.open("mysql", "my_username:my_password@tcp(localhost:3306)/go_restful_api")
    helper.paniciferror(err)

    db.setmaxidleconns(5)
    db.setmaxidleconns(20)
    db.setconnmaxlifetime(60 * time.minute)
    db.setconnmaxidletime(10 * time.minute)

    return db
}
  • 更新
func (repository *categoryrepositoryimpl) update(ctx context.context, tx *sql.tx, category domain.category) domain.category {
   sql := "update category set name = ? where id = ?"
   _, err := tx.execcontext(ctx, sql, category.name, category.id)
   helper.paniciferror(err)
   return category
}
  • 删除
func (repository *categoryrepositoryimpl) delete(ctx context.context, tx *sql.tx, category domain.category) {
   sql := "delete from category where id = ?"
   _, err := tx.execcontext(ctx, sql, category.name, category.id)
   helper.paniciferror(err)
}
  • 创建
func (repository *categoryrepositoryimpl) create(ctx context.context, tx *sql.tx, category domain.category) domain.category {
sql := "insert into category (name) values (?)"
   result, err := tx.execcontext(ctx, sql, category.name)
   helper.paniciferror(err)

   id, err := result.lastinsertid()
   if err != nil {
       panic(err)
   }
   category.id = int(id)
        
   return category
}
  • 阅读
func (repository *categoryrepositoryimpl) findall(ctx context.context, tx *sql.tx) []domain.category {
    sql := "select id, name from category"
    rows, err := tx.querycontext(ctx, sql)
    helper.paniciferror(err)

    var categories []domain.category
    for rows.next() {
        category := domain.category{}
        err := rows.scan(
            &category.id,
            &category.name,
        )
        helper.paniciferror(err)
        categories = append(categories, category)
    }
    return categories
}
func (repository *CategoryRepositoryImpl) FindById(ctx context.Context, tx *sql.Tx, categoryId int) (domain.Category, error) {
    SQL := "select id, name from category where id = ?"
    rows, err := tx.QueryContext(ctx, SQL, categoryId)
    helper.PanicIfError(err)

    category := domain.Category{}
    if rows.Next() {
        err := rows.Scan(&category.Id, &category.Name)
        helper.PanicIfError(err)
        return category, nil
    } else {
        return category, errors.New("CATEGORY IS NOT FOUND")
    }
}

正确答案


长话短说:您正在不应该使用数据库事务

将它们保留用于一组必须成功的写入操作。

我建议您重新定义您的存储库及其实现。分层的整个想法是独立于存储库实现,并且能够从 mysql 切换到 mongo,再切换到 postgres,或者 w/e。

//category_controller.go
type CategoryRepository interface {
    Create(ctx context.Context, category domain.Category) domain.Category
    Update(ctx context.Context, category domain.Category) domain.Category
    Delete(ctx context.Context, category domain.Category)
    FindById(ctx context.Context, categoryId int) (domain.Category, error)
    FindAll(ctx context.Context) []domain.Category
}
//category_repository_impl.go
type CategoryRepositoryImpl struct {
    db *sql.DB 
}

func NewCategoryRepository(db *sql.DB) CategoryRepository {
    return &CategoryRepositoryImpl{db: db}
}

请参阅以下要点以及代码文件的修改版本: https://gist.github.com/audrenbdb/94660f707a206d385c42f64ceb93a4aa

今天关于《出现 UPDATE 和 DELETE SQL 中的“错误连接”是由于 GOLANG MySQL 驱动程序导致的问题吗?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>