参数化 SQL 查询

数据库操作

// 连接数据库
// ...

type Row struct {
    language   string
    percentage float32
}

// 执行 SQL 查询
statement, _ := conn.Prepare(`
    SELECT
      Language, Percentage
    FROM countrylanguage
    WHERE 
      CountryCode = ? AND
      Percentage > ?`)

// 获取所有行
rows, _ := statement.Query("RUS", 0.5)
for rows.Next() {
    row := new(Row)
    rows.Scan(&row.language, &row.percentage)
    fmt.Println(row)
}

// 关闭连接
conn.Close()