ODBC Driver for Oracle OCI

Build 20.0.7654

Parameterized Statements

The following code example shows how to bind parameters to create parameterized statements.

Single-Use Statements

The Query and Exec functions both accept additional parameters for binding query parameters to values.

rows, _ := db.Query("SELECT CompanyName, City FROM Customers WHERE Country = ?", "US")
defer rows.Close()
for rows.Next() {
        var (
                CompanyName string
                City string
        )

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

Reusable Statements

The Prepare function creates prepared Stmt objects, which can be re-used across multiple Query and Exec calls.

stmt, _ := db.Prepare("SELECT CompanyName, City FROM Customers WHERE Country = ?")
defer stmt.Close()

rows, _ := stmt.Query("US 1")
defer rows.Close()
for rows.Next() {
        var (
                CompanyName string
                City string
        )

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

rows, _ = stmt.Query("US 2")
defer rows.Close()
for rows.Next() {
        var (
                CompanyName string
                City string
        )

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

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7654