golang mysql exec 占位符 “? ” 未展开

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

golang mysql exec placeholder "?" not expanded

问题

我觉得我一定完全错过了重点。
我尝试运行类似下面示例的代码,但是?没有被展开为传入的参数。

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open(...)
if err != nil { ... }
_, err = db.Query("SELECT * FROM foo WHERE bar=?", bar)

此外,谁负责展开它?在database/sql文档中有提到,但其他对话暗示可能是驱动程序的责任。

我错过了什么?非常感谢任何指向正确方向的指点。

英文:

I feel like I must be totally missing the point.
I try to run something along the lines of the example below, but the ? is not expanded into the argument passed in.

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

db, err := sql.Open(...)
if err != nil { ... }
_, err = db.Query("SELECT * FROM foo WHERE bar=?", bar)

Also, who's concern is expanding it? it show's up in the doc of database/sql but other conversations hinted it may be the concern of the driver.

What am I missing? Any pointer in the right direction is greatly appreciated.

答案1

得分: 1

你(可能)没有告诉它使用mysql驱动程序;

db, err := sql.Open("mysql", connString)

这是我的一些示例代码:

var query = "INSERT IGNORE INTO blah (`col1`, `col2`, `col3`) VALUES (?, ?, ?)"

r, err := db.Query(query, some_data_1, some_data_2, some_data_3)

// 尝试存储数据时失败
if err != nil {
    msg := fmt.Sprintf("失败:%s", err.Error())
    fmt.Println(msg)

    return err
}

r.Close() // 始终这样做,否则会泄漏连接

我使用以下代码创建MySQL池(是的,它是一个池,而不是一个连接):

import (
    // mysql驱动程序
    _ "github.com/go-sql-driver/mysql"
    
    "database/sql"
    "fmt"
)

connString := fmt.Sprintf("%s:%s@(%s:%d)/%s?timeout=30s",
    user,
    password,
    host,
    port,
    database,
)

db, err := sql.Open("mysql", connString)

if err != nil {
    return nil, err
}

err = db.Ping() // 测试池连接

if err != nil {
    return nil, err
}

return db, nil // 没有错误,返回池连接

我还强烈建议您打印出数据库连接过程和查询过程中的任何错误。可能是您正在使用驱动程序,但缺少权限,数据库已关闭等等。

英文:

You're (probably) not telling it to use the mysql driver;

db, err := sql.Open("mysql", connString)

Here is some of my sample code:

var query = "INSERT IGNORE INTO blah (`col1`, `col2`, `col3`) VALUES (?, ?, ?)"

r, err := db.Query(query, some_data_1, some_data_2, some_data_3)

// Failure when trying to store data
if err != nil {
    msg := fmt.Sprintf("fail : %s", err.Error())
    fmt.Println(msg)

    return err
}

r.Close() // Always do this or you will leak connections

I create the MySQL pool (yes, it's a pool NOT a connection) with:

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

connString := fmt.Sprintf("%s:%s@(%s:%d)/%s?timeout=30s",
    user,
    password,
    host,
    port,
    database,
)

db, err := sql.Open("mysql", connString)

if err != nil {
    return nil, err
}

err = db.Ping() // test the pool connection(s)

if err != nil {
    return nil, err
}

return db, nil // No error, return the pool connections

I also highly suggest you print out any errors in the database connection process and query process. It could be that you are using the driver, but you lack permissions, the database is down, etc.

huangapple
  • 本文由 发表于 2016年3月4日 03:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/35781555.html
匿名

发表评论

匿名网友

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

确定