doutuo7609 2019-09-10 10:07
浏览 2178
已采纳

无法将args(类型[] string)用作类型[] interface {} [重复]

This question already has an answer here:

my golang sqlite insert function. i'm using this package "github.com/mattn/go-sqlite3"

func Insert(args ...string)(err error){
  db, err:=sql.Open("sqlite3","sqlite.db")
  if err !=nil {
    return
  }
  q, err := db.Prepare(args[0])
  if err !=nil{
    return
  }
  _,err = q.Exec(args[1:]...)
  return
 }
main (){
  err := Insert("INSERT INTO table(first,last) VALUES(?,?)","Nantha","nk")
  if err !=nil{
  fmt.Println(err.Error())
    return
  }
}

i'm getting this error

cannot use args (type []string) as type []interface {} in argument to q.Exec

</div>
  • 写回答

1条回答 默认 最新

  • douba9425 2019-09-10 10:16
    关注

    The error is pretty clear, the function expects type []interface{} but you're passing in a value of type []string. You have to first convert []string to []interface{} before passing it to Exec. And the way to do that is to loop over the strings and add each one to a new slice of interface{}.

    https://golang.org/doc/faq#convert_slice_of_interface


    As an alternative approach, you can change the Insert argument types.

    func Insert(query string, args ...interface{}) (err error) {
        db, err := sql.Open("sqlite3", "sqlite.db")
        if err != nil {
            return err
        }
        q, err := db.Prepare(query)
        if err != nil {
            return err
        }
        _, err = q.Exec(args...)
        return err
    }
    
    func main() {
        err := Insert("INSERT INTO table(first,last) VALUES(?,?)", "Nantha", "nk")
        if err !=nil{
            fmt.Println(err.Error())
            return
        }
    }
    

    Please note that you're using the database/sql package incorrectly. Many of the objects returned from that package's functions/methods need to be closed to release the underlying resources.

    This is true for *sql.DB returned by Open, *sql.Stmt returned by Prepare, *sql.Rows returned by Query, etc.

    So your function should look closer to something like this:

    func Insert(query string, args ...interface{}) (err error) {
        db, err := sql.Open("sqlite3", "sqlite.db")
        if err != nil {
            return err
        }
        defer db.Close()
    
        q, err := db.Prepare(query)
        if err != nil {
            return err
        }
        defer q.Close()
    
        _, err = q.Exec(args...)
        return err
    }
    

    Also note that sql.DB is reusable, that means that you don't have to sql.Open a new instance every time you need to talk to the database.

    From the docs on Open:

    The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.

    If you keep doing it the way you're doing it, openning a new DB every time you call Insert or any other function that needs to talk to the DB, your program will perform worse than if you had a single DB and have your functions reuse that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答