Created
May 19, 2020 16:29
-
-
Save pdk/9f0bf3a811079a2995777c123e55163d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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