一、第一种写法
首先说一下遇到的一个问题
sql: converting argument $1 type: unsupported type []int, a slice of int
以下代码是最初遇到问题的代码
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
func placeholders(n int) string {
var b strings.Builder
for i := 0; i < n - 1; i++ {
b.WriteString("?,")
}
if n > 0 {
b.WriteString("?")
}
return b.String()
}
func (c *GetMovieInfoController) Get() {
ids := []int{5,6,7}
db := models.ConnectDb();
defer db.Close()
query := fmt.Sprintf("select id,movie_name ,movie_director from movie_info where id in (%s)", placeholders(len(ids)))
rows, err := db.Query(query,ids)
fmt.Fprint(c.Ctx.ResponseWriter, rows)
fmt.Fprint(c.Ctx.ResponseWriter, ids)
fmt.Fprint(c.Ctx.ResponseWriter, err)
}
解决方案
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
"strings"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
func placeholders(n int) string {
var b strings.Builder
for i := 0; i < n - 1; i++ {
b.WriteString("?,")
}
if n > 0 {
b.WriteString("?")
}
return b.String()
}
func (c *GetMovieInfoController) Get() {
ids := []interface{}{5,6,7} //这里使用interface关键字
db := models.ConnectDb();
defer db.Close()
query := fmt.Sprintf("select id,movie_name,movie_director from movie_info where id in (%s)", placeholders(len(ids)))
rows, err := db.Query(query,ids...) //加... 这里是解决了问题,但是实质性还不知道哈,基础入门阶段,以后再了解
fmt.Fprint(c.Ctx.ResponseWriter, rows)
fmt.Fprint(c.Ctx.ResponseWriter, ids)
fmt.Fprint(c.Ctx.ResponseWriter, err)
item := MovieInfo{}
list := []MovieInfo{}
for rows.Next(){
var mid int64
var movie_name, movie_director string
err = rows.Scan(&mid,&movie_name,&movie_director)
if err != nil {
panic(err.Error())
}
item.Id = mid
item.Movie_name = movie_name
item.Movie_director = movie_director
list = append(list,item)
}
fmt.Fprint(c.Ctx.ResponseWriter, list)
fmt.Println(list)
}
二、第二种写法
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"mypro/models"
)
type GetMovieInfoController struct {
beego.Controller
}
type MovieInfo struct {
Id int64
Movie_name string
Movie_director string
}
func formatids(ids []int) ([]interface{},string) {
inIds := ""
params:=make([]interface{},0)
for i:=0;i<len(ids);i++{
if i==0{
inIds+="?"
}else{
inIds+=",?"
}
params=append(params , ids[i])
}
return params,inIds
}
func (c *GetMovieInfoController) Get() {
ids := []int{8,9,10}
db := models.ConnectDb();
defer db.Close()
sql := "select id,movie_name,movie_director from movie_info where id in (%s)"
params,inIds := formatids(ids)
sql = fmt.Sprintf(sql ,inIds )
fmt.Fprint(c.Ctx.ResponseWriter,sql)
rows,err := db.Query(sql , params...)
fmt.Fprint(c.Ctx.ResponseWriter, rows)
fmt.Fprint(c.Ctx.ResponseWriter, ids)
fmt.Fprint(c.Ctx.ResponseWriter, err)
item := MovieInfo{}
list := []MovieInfo{}
for rows.Next(){
var mid int64
var movie_name, movie_director string
err = rows.Scan(&mid,&movie_name,&movie_director)
if err != nil {
panic(err.Error())
}
item.Id = mid
item.Movie_name = movie_name
item.Movie_director = movie_director
list = append(list,item)
}
fmt.Fprint(c.Ctx.ResponseWriter, list)
fmt.Println(list)
}
三、第三种写法
func (c *GetMovieInfoController) Get() {
ids := []string{"8", "9", "10"}
db := models.ConnectDb();
defer db.Close()
idStr := strings.Join(ids, "','")
fmt.Println(idStr)
sqlText := "select id,movie_name,movie_director from movie_info where id in ('%s')"
sqlText = fmt.Sprintf(sqlText, idStr)
rows,err := db.Query(sqlText)
}
//sql
select id,movie_name,movie_director from movie_info where id in ('8','9','10')