11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dbrで発行するSQLを確認する

Last updated at Posted at 2018-12-09

dbr.NewBuffer()で取得したバッファから、プレースホルダー付きSQLとプレースホルダーにセットする値が確認できる。
因みにundefined: strings.Builderのエラーになる場合はgo version1.10以上にアップデートしてください。

sample.go
package main

import (
	"fmt"

	"github.com/gocraft/dbr/dialect"

	_ "github.com/go-sql-driver/mysql"
	"github.com/gocraft/dbr"
)

func main() {
	// エラー処理は省略
	conn, _ := dbr.Open("mysql", "user:pass@/example", nil)

	sess := conn.NewSession(nil)
	// userID = 1で検索するSQL
	stmt := sess.Select("*").From("users").Where("id = ?", 1)

	buf := dbr.NewBuffer()
	stmt.Build(dialect.MySQL, buf)

	fmt.Println(buf.String()) // SELECT * FROM users WHERE (id = ?)
	fmt.Println(buf.Value())  // [1]
}
11
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?