无法获取数据库响应以执行进一步的Go更新查询。

huangapple go评论82阅读模式
英文:

Unable to get db response for update query for further execution in go

问题

我想要更新远程数据库表中的数据并执行其他任务,但无法实现。

使用相同的插入查询代码,我能够在同一张表中插入值,我得到的响应非常快,并继续执行其他任务。
但是在更新查询中,它确实更新了表中的值,但无法继续执行。

这是我尝试过的示例代码:

package src

import (
    "github.com/go-sql-driver/mysql"
    "database/sql"
    "fmt"
    "log"
    "net"
)

const (
    DB_NAME = "test_db"
    DB_HOST = "远程数据库IP地址:端口"
    DB_USER = "用户名"
    DB_PASS = "密码"
)

const (
    bufferSize int    = 1024
    port       string = ":23456"
)

func main() {

    udpAddr, err := net.ResolveUDPAddr("udp6", port)

    var s Server
    s.conn, err = net.ListenUDP("udp4", udpAddr)

    fmt.Println("尝试连接远程数据库")

    dsn := DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8"
    db, err := sql.Open("mysql", dsn)
    if err != nil {
        log.Fatal(err)
    }

    defer db.Close()

    fmt.Println("已连接数据库")

    rows, err := db.Query("UPDATE user_name_table SET user_id = ? WHERE user_name = ?", "abcd", "admin")
    defer rows.Close()

    if err != nil {

        log.Fatal(err)
        fmt.Println("更新值时出错")

    } else {

        fmt.Println("在数据库中成功更新")

    }

    // 无法继续执行后续任务

}

此外,我尝试从PHP Web服务更新数据库表,它可以正常工作,但是使用Go语言时,无论是在更新值后还是在继续执行后续任务时都没有响应。

请帮助我。提前感谢。

英文:

I want to update data in remote db table and do further tasks but could not do it.

Using same code with insert query, I am able to insert values in same table where I am getting response very fast and going ahead for further tasks.
But in update query it does update values in table but could not going further.

Here is my sample code that I have tried:

package src

import (
    "github.com/go-sql-driver/mysql"
	"database/sql"
	"fmt"
	"log"
	"net"
)

const (
	DB_NAME = "test_db"
	DB_HOST = "remote db ip address:port"
	DB_USER = "username"
	DB_PASS = "password"
)

const (
	bufferSize int    = 1024
	port       string = ":23456"
)

func main() {

	udpAddr, err := net.ResolveUDPAddr("udp6", port)

	var s Server
	s.conn, err = net.ListenUDP("udp4", udpAddr)

	fmt.Println("Trying to connect remote db")

	dsn := DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	fmt.Println("Connected db")

	rows, err := db.Query("UPDATE user_name_table SET user_id = ? WHERE user_name = ? ", "abcd", "admin")
	defer rows.Close()

	if err != nil {

		log.Fatal(err)
		fmt.Println("error in updating values")

	} else {

		fmt.Println("updated successfully in db")

	}

	// not coming here for doing further tasks

}

Also , I have tried to update db table from php webservice and its working fine but using go neither its giving response after updating values nor moving further.

Please help me .
Thanks in advance.

答案1

得分: 1

这是因为log.Fatal(err)会调用os.Exit(1)。换句话说,当err != nil时,它会终止你的程序。

英文:

This happens because log.Fatal(err) will call os.Exit(1). In other words, it will terminate your program when err != nil.

答案2

得分: 0

找到了解决方案。只需删除更新查询的返回响应中的不需要的行,一切都正常工作。

_, err := db.Query("UPDATE user_name_table SET user_id = ? WHERE user_name = ?", "abcd", "admin")
// defer rows.Close()

英文:

Found out the solution.Just removed unwanted rows for returned response for updating query and everything working fine again.

_, err := db.Query("UPDATE user_name_table SET user_id = ? WHERE user_name = ? ",   "abcd", "admin")
    // defer rows.Close()

huangapple
  • 本文由 发表于 2014年8月22日 23:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/25450177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定