Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pdk
Created May 19, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdk/9f0bf3a811079a2995777c123e55163d to your computer and use it in GitHub Desktop.
Save pdk/9f0bf3a811079a2995777c123e55163d to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
dbname = "pkelly"
)
// pkelly=# create table late_attendance_daily (id int);
// CREATE TABLE
// pkelly=# insert into late_attendance_daily values (1), (2), (3);
// INSERT 0 3
// pkelly=# select count(*) from late_attendance_daily;
// count
// -------
// 3
// (1 row)
// pkelly=# select count(id) from late_attendance_daily;
// count
// -------
// 3
// (1 row)
// pkelly=# select count('id') from late_attendance_daily;
// count
// -------
// 3
// (1 row)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d dbname=%s sslmode=disable",
host, port, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatalf("%s", err.Error())
}
defer db.Close()
row := db.QueryRow(`select count($1::text) from late_attendance_daily`, "id")
var countVal int64
err = row.Scan(&countVal)
if err != nil {
log.Fatalf("%s", err.Error())
}
fmt.Printf("%v\n", countVal)
row = db.QueryRow(`select count(id) from late_attendance_daily`)
err = row.Scan(&countVal)
if err != nil {
log.Fatalf("%s", err.Error())
}
fmt.Printf("%v\n", countVal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment