You can use Prepared Statements on Athena.
db, _ := sql.Open("athena", "db=default&output_location=s3://results")
// 1. Prepare
stmt, _ := db.PrepareContext(ctx, "SELECT url, code FROM cloudfront WHERE code = ?")
defer stmt.Close() // 3. Close
// 2. Execute
rows, _ := stmt.QueryContext(ctx, targetCode)
defer rows.Close()
for rows.Next() {
var url string
var code int
rows.Scan(&url, &code)
}
- Run PREPARE on Athena to create a Statement for use with Athena
- Create a Stmt object and keep statement_name inside
- The stmt object is valid until Close method is executed
- Result Mode
- Available under all Result Modes
- ResultMode can be specified in PrepareContext
rows, _ := stmt.PrepareContext(SetDLMode(ctx), sql) // Prepares a statement in DL Mode
- Run a prepared statement using EXECUTE on Athena
- You can specify parameters
- Use QueryContext and ExecContext methods
- Run DEALLOCATE PREPARE and delete the prepared statement
- Use the Close method of the Stmt object
intParam := 1
stringParam := "string value"
stmt, _ := db.PrepareContext(ctx, "SELECT * FROM test_table WHERE int_column = ? AND string_column = ?")
rows, _ := stmt.QueryContext(ctx, intParam, stringParam)
execute SELECT * FROM test_table WHERE int_column = 1 and string_column = 'string value'