ODBC Driver for IBM Cloud Data Engine

Build 23.0.8839

パラメータ化されたステートメント

次のコード例は、パラメータをバインドしてパラメータ化されたステートメントを作成する方法を示します。

シングルユース ステートメント

Query とExec 関数は両方とも、クエリパラメータを値にバインドするための追加パラメータを受け入れます。

rows, _ := db.Query("SELECT Id, Status FROM [CloudObjectStorage_1].[SampleBucket_1].Jobs WHERE UserId = ?", "gentit@cdata.com")
defer rows.Close()
for rows.Next() {
        var (
                Id string
                Status string
        )

        rows.Scan(&Id, &Status)
        fmt.Printf("Id = %s, Status = %s\n", Id, Status)
}

リユーザブル ステートメント

Prepare 関数は、プリペアドStmt オブジェクトを作成します。これは、複数のQuery およびExec 呼び出しで再利用できます。

stmt, _ := db.Prepare("SELECT Id, Status FROM [CloudObjectStorage_1].[SampleBucket_1].Jobs WHERE UserId = ?")
defer stmt.Close()

rows, _ := stmt.Query("gentit@cdata.com 1")
defer rows.Close()
for rows.Next() {
        var (
                Id string
                Status string
        )

        rows1.Scan(&Id, &Status)
        fmt.Printf("Id = %s, Status = %s\n", Id, Status)
}

rows, _ = stmt.Query("gentit@cdata.com 2")
defer rows.Close()
for rows.Next() {
        var (
                Id string
                Status string
        )

        rows2.Scan(&Id, &Status)
        fmt.Printf("Id = %s, Status = %s\n", Id, Status)
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839