英文:
How to parse current time to InfluxDB Go client?
问题
我正在使用以下Go客户端("github.com/influxdata/influxdb/client/v2")查询InfluxDB,并且它工作正常。
q = fmt.Sprintf("SELECT * FROM %s WHERE time > now() - 3600s", Measurement)
但是我想使用Go的时间变量代替InfluxDB的now()。
t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %s - 3600s", Measurement, t)
但是出现了错误解析查询:在第1行,第101个字符处找到-01,预期为;。
英文:
I am using below Go client ("github.com/influxdata/influxdb/client/v2") to query InfluDB and it is working fine
q = fmt.Sprintf("SELECT * FROM %s WHERE time > now() - 3600s", Measurement)
but I want to use Go time variable instead of InfluxDB now()
t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > %s - 3600s", Measurement, t)
but getting error parsing query: found -01, expected ; at line 1, char 101
答案1
得分: 2
日期字符串必须使用单引号括起来。使用以下代码可以解决你的问题:
t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > '%s' - 3600s", Measurement, t)
英文:
Date strings must be single quoted. Using
t := time.Now().Format(time.RFC3339)
q = fmt.Sprintf("SELECT * FROM %s WHERE time > '%s' - 3600s", Measurement, t)
Should solve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论