mysql 드라이버를 사용하여 golang의 매개 변수로 쿼리를 실행하는 동안 오류가 발생했습니다.

마테오 잠본

mysql 데이터베이스에서 쿼리를 실행하는 func를 만들었습니다. 이 패키지 "github.com/go-sql-driver/mysql"을 사용합니다.

그러나 이것을 실행하면 :

...
err := database.ExecuteSql("INSERT INTO xxx(field1, field2, field3) VALUES(?, ?, ?)", "field1", 123, time.Now())
...
func ExecuteSql(cmd string, , args ...interface{}) error {
  ctx := context.Background()
  _, err := db.ExecContext(ctx, cmd, args)
  ctx.Done()
  ...
}

"sql : converting argument $ 1 type : unsupported type [] interface {}, a slice of interface"를 반환합니다.

에민 랄 레토 비치

다음과 같은 방법을 실행할 때 :

_, err := db.ExecContext(ctx, cmd, args)

당신은 통과 args하나로서 []interface{}인수입니다. .NET Core가 아닌 경우 슬라이스는 ExecContext메서드의 인수로 지원되지 않습니다 []byte.

unpack 연산자를 args다음 과 함께 사용해야합니다 .

_, err := db.ExecContext(ctx, cmd, args...)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관