Skip to content

PrepareContextにおけるマッピングの失敗 #32

@azul915

Description

@azul915

ページ番号:p214(初版)
セクション番号:9.6.1
内容タイトル:プリペアードステートメントを使う
誤っている内容:

  • プリペアードステートメントの構築において、構造体のフィールド数と割当てたいプレースホルダーの数が一致していない
     yyyy/MM/dd hh:mm:ss sql: expected 3 arguments, got 2
     exit status 1

正しい内容:

  • nullcurrent_timestampで埋める
stmt, err := tx.PrepareContext(ctx, `
INSERT INTO users(
	user_id,
	user_name,
	created_at
) VALUES (
	$1, $2, null
);`)

その他コメント:

  • tx.ParpareContextとサンプルコードにあるが、*Txがあることが前提なので、接続とトランザクション開始が省略されている

    conn, err := sql.Open("pgx", "host=localhost port=5432 user=testuser dbname=tesstdb password=pass sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    
    tx, err := conn.BeginTx(ctx, nil)
    if err != nil {
        log.Fatal(err)
    }

Metadata

Metadata

Assignees

Labels

erattacorrect in next reprint.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions