2017-08-03 55 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。