1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package handle
import (
"database/sql"
"errors"
"fmt"
"pgsql/config"
_ "github.com/lib/pq"
)
func Executl() error {
config.Init()
if config.PgsqlData != nil {
//创建mysql连接
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", config.PgsqlData.Address, config.PgsqlData.Port, config.PgsqlData.Username, config.PgsqlData.Password, config.PgsqlData.Database))
if err != nil {
return err
}
defer db.Close()
str := fmt.Sprintf("select %s,%s from %s where NAME='%s'", config.PgsqlData.AccountField, config.PgsqlData.PwdField, config.PgsqlData.Table, config.PgsqlData.Username)
rows := db.QueryRow(str)
var name *sql.NullString
var pwd *sql.NullString
err = rows.Scan(&name, &pwd)
if err != nil {
return err
}
if name.String == "" || pwd.String == "" {
return errors.New("data is null")
}
fmt.Println("name:",name.String)
fmt.Println("passwd:",pwd.String)
return nil
}
return errors.New("open config file failed")
}
|