예제 #1
0
func main() {
	conn, err := pgsql.Connect("dbname=postgres user=cbbrowne port=7099", pgsql.LogError)

	if err != nil {
		os.Exit(1)
	}
	defer conn.Close()

	rs, err := conn.Query("SELECT 1 AS num; SELECT 2 AS num; SELECT 3 AS num;")
	if err != nil {
		os.Exit(1)
	}
	defer rs.Close()

	for {
		hasRow, err := rs.FetchNext()
		if err != nil {
			os.Exit(1)
		}
		if hasRow {
			num, _, _ := rs.Int(0)
			fmt.Println("num:", num)
		} else {
			hasResult, err := rs.NextResult()
			if err != nil {
				os.Exit(1)
			}
			if !hasResult {
				break
			}
			fmt.Println("next result")
		}
	}
}
예제 #2
0
func main() {
	conn, err := pgsql.Connect("dbname=postgres user=cbbrowne port=7099", pgsql.LogError)

	if err != nil {
		os.Exit(1)
	}
	defer conn.Close()

	command := "SELECT * FROM table1 WHERE id = @id;"
	idParam := pgsql.NewParameter("@id", pgsql.Integer)

	stmt, err := conn.Prepare(command, idParam)
	if err != nil {
		os.Exit(1)
	}
	defer stmt.Close()

	for id := 1; id <= 3; id++ {
		err = idParam.SetValue(id)
		if err != nil {
			os.Exit(1)
		}
		queryAndPrintResults(stmt)
	}
}
예제 #3
0
파일: scan.go 프로젝트: jhbrewer/go-pgsql
func main() {
	conn, err := pgsql.Connect("dbname=testdatabase user=testuser password=testpassword", pgsql.LogError)
	if err != nil {
		os.Exit(1)
	}
	defer conn.Close()

	var x item

	_, err = conn.Scan("SELECT 123, 'abc', 14.99;", &x.id, &x.name, &x.price)
	if err != nil {
		os.Exit(1)
	}

	fmt.Printf("item x: '%+v'\n", x)
}
예제 #4
0
파일: scan.go 프로젝트: samuel/go-pgsql
func main() {
	// conn, err := pgsql.Connect("dbname=postgres user=cbbrowne port=7099", pgsql.LogError)
	// Can have a long connection string, if needed, but if values are set in environment, it's all optional
	conn, err := pgsql.Connect("", pgsql.LogError)
	if err != nil {
		os.Exit(1)
	}
	defer conn.Close()

	var x item

	_, err = conn.Scan("SELECT 123, 'abc', 14.99;", &x.id, &x.name, &x.price)
	if err != nil {
		os.Exit(1)
	}

	fmt.Printf("item x: '%+v'\n", x)
}