2017-08-03 210 views

回答

0
package main 

import (
"database/sql" 
"log" 
_ "./pq" 
"fmt" 

) 

type DbInfo struct { 
id string 
person_id int 
timestamp int 
age string 
gender string 
attention string 
interest int 
happines int 
surprise int 
anger int 
disgust int 
fear string 
sadness int 
neutrall string 

} 

var DBObject DbInfo 
var DbArray []DbInfo 




func main() { 


db, err := sql.Open("****", "postgres://postgres:********/cam? 
sslmode=disable") 
if err != nil { 
    log.Fatal(err) 
} 
defer db.Close() 
rows, err := db.Query(`SELECT avg(age), avg(id), avg(neutrall),avg(fear) 
FROM public.stat`) //stringTimeDayAgoQuery) 
if err != nil { 
    log.Fatal(err) 
} 
defer rows.Close() 
for rows.Next() { 
    //err := rows.Scan(&DBObject.id, &DBObject.person_id, 
&DBObject.timestamp,&DBObject.age, 
&DBObject.gender,&DBObject.attention,&DBObject.interest,&DBObject.happines, 

    &DBObject.surprise,&DBObject.anger,&DBObject.disgust,&DBObject.fear, 
    &DBObject.sadness,&DBObject.neutrall) 
    err := rows.Scan(&DBObject.age,&DBObject.id,&DBObject.neutrall, 
    &DBObject.fear) 

    if err != nil { 
     log.Fatal(err) 
    } 
    DbArray = append(DbArray, DBObject) 
} 
fmt.Println(DbArray) 

} 在这段代码中。我从postgreSQL读取了我的数据。我将它作为数组读取,现在我想将输出导入Influxdb。因为我想使用Grafana,并且它不适用于postgresql。