Skip to content

Instantly share code, notes, and snippets.

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 llhua2329/0e72855590eb3053344f3cce3a0bc771 to your computer and use it in GitHub Desktop.
Save llhua2329/0e72855590eb3053344f3cce3a0bc771 to your computer and use it in GitHub Desktop.
go操作postgresql查询测试
<component name="libraryTable">
<library name="Go SDK">
<CLASSES>
<root url="file://D:/go/src" />
</CLASSES>
<SOURCES>
<root url="file://D:/go/src" />
</SOURCES>
</library>
</component>
<component name="libraryTable">
<library name="GOPATH &lt;pg-test&gt;">
<CLASSES>
<root url="file://E:/Go/src/helloworld" />
<root url="file://E:/Go/src/ucai_go" />
<root url="file://E:/Go/src/test" />
<root url="file://E:/Go/src/github.com" />
<root url="file://E:/Go/src/UDPServer" />
<root url="file://E:/Go/src/webServer" />
<root url="file://E:/Go/src/hello" />
<root url="file://E:/Go/src/.idea" />
<root url="file://E:/Go/src/beeblog" />
<root url="file://E:/Go/src/netTest" />
<root url="file://E:/Go/src/UDPClient" />
</CLASSES>
<SOURCES>
<root url="file://E:/Go/src/helloworld" />
<root url="file://E:/Go/src/ucai_go" />
<root url="file://E:/Go/src/test" />
<root url="file://E:/Go/src/github.com" />
<root url="file://E:/Go/src/UDPServer" />
<root url="file://E:/Go/src/webServer" />
<root url="file://E:/Go/src/hello" />
<root url="file://E:/Go/src/.idea" />
<root url="file://E:/Go/src/beeblog" />
<root url="file://E:/Go/src/netTest" />
<root url="file://E:/Go/src/UDPClient" />
</SOURCES>
<excluded>
<root url="file://$PROJECT_DIR$" />
</excluded>
</library>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pg-test.iml" filepath="$PROJECT_DIR$/.idea/pg-test.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" b="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Go SDK" level="project" />
<orderEntry type="library" name="GOPATH &lt;pg-test&gt;" level="project" />
</component>
</module>
package main
import (
_ "github.com/lib/pq"
"database/sql"
"log"
"fmt"
)
func main() {
host := "localhost"
port := "5432"
user := "postgres"
pwd := "123"
dbname := "test"
//db, err := sql.Open("postgres", "user=umap dbname=umap password=umap201606 host=192.168.186.207 port=5433 sslmode=disable")
//db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
db, err := sql.Open("postgres",
"host=" + host +
" port=" + port +
" user=" + user +
" password=" + pwd +
" dbname=" + dbname +
" sslmode=disable")
if err != nil{
fmt.Println("open database error: %s", err.Error())
log.Fatal(err)
return
}
rows, err := db.Query("SELECT * FROM g_session")
if err != nil{
fmt.Println(err.Error())
log.Fatal(err)
return
}
defer rows.Close()
fmt.Println("")
cols, _ := rows.Columns()
for i := range cols{
fmt.Print(cols[i])
fmt.Print("\t")
}
fmt.Println("")
var id, connecttimes, position, loginip, loginmachinename, localip, localmachinename, loginusername, videoname,picsize,
logintime,logouttime string
for rows.Next(){
err := rows.Scan(&id,&connecttimes,&position,&loginip,&loginmachinename,&localip,&localmachinename,&loginusername,&videoname,&picsize,&logintime,&logouttime)
if err == nil{
fmt.Print(id)
fmt.Print("\t")
fmt.Print(connecttimes)
fmt.Print("\t")
fmt.Print(position)
fmt.Print("\t")
fmt.Print(loginip)
fmt.Print("\t")
fmt.Print(loginmachinename)
fmt.Print("\t")
fmt.Print(localip)
fmt.Print("\t")
fmt.Print(localmachinename)
fmt.Print("\t")
fmt.Print(loginusername)
fmt.Print("\t")
fmt.Print(videoname)
fmt.Print("\t")
fmt.Print(picsize)
fmt.Print("\t")
fmt.Print(logintime)
fmt.Print("\t")
fmt.Print(logouttime)
fmt.Print("\t\r\n")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment