Python influx query inserting dates into query - python

When I run the following statement to query influx I get the error :
InfluxDBClientError: invalid timestamp string
date_from = '2019-12-02 T00:00:00Z'
date_to = '2019-12-02T01:00:00Z'
"""SELECT "value" FROM "location/PRESSURE_SENSOR_1" WHERE time >= """ + '\'' + date_from + '\'' + """ and time <= '2019-12-02T01:00:00Z' """
If I run the following it works fine :
client.query("""SELECT "value" FROM "location/PRESSURE_SENSOR_1" where time >= '2019-10-02 00:00:00' and time < '2019-10-03 00:00:00'"""))
The aim is to build the statement where i can insert variables but i dont seam to be able to do it. Any help appreciated?
Thanks

You should utilize bind_params in the api to accomplish this task.
query = "SELECT value FROM 'location/PRESSURE_SENSOR_1' where time >= $start_time and time < $end_time"
bind_params = {'end_time': '2019-10-03 00:00:00', 'start_time': '2019-10-02 00:00:00'}
client.query(query, bind_params=bind_params))
Find more information here:
Influx Client Documenation

Related

Getting a syntax error when trying to execute this SQL query in Python

I'm trying to get the script to automatically set the query start time as 6am yesterday (and will set the end time as 6am today once I figure out the Start time error) but I am getting a syntax error (102, b"Incorrect syntax near '06'.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n").
If I take out the """+StartTime+""" and change to '20211212 07:00:00.000' then it works but I need a way for the script to keep track of the current date rather than going back and changing every day.
from datetime import timedelta
import numpy as np
from pandas import DataFrame
import pandas as pd
import pymssql
from datetime import date, timedelta
today = date.today()
yesterday = today - timedelta(days = 1)
BEGINDATE=yesterday
ENDDATE=today
BEGINDATE=BEGINDATE.strftime("%Y%m%d")
ENDDATE=ENDDATE.strftime("%Y%m%d")
StartTime = BEGINDATE +' 06:00:00:000'
print(StartTime)
Query= """SET NOCOUNT ON
DECLARE #StartDate DateTime
DECLARE #EndDate DateTime
SET #StartDate = """+StartTime+"""
SET #EndDate = '20211213 07:00:00.000'
SET NOCOUNT OFF
SELECT temp.TagName ,DateTime ,Value ,vValue ,MinRaw = ISNULL(Cast(AnalogTag.MinRaw as VarChar(20)),'N/A') ,MaxRaw = ISNULL(Cast(AnalogTag.MaxRaw as VarChar(20)),'N/A') ,Unit = ISNULL(Cast(EngineeringUnit.Unit as VarChar(20)),'N/A') ,StartDateTime From (
SELECT *
FROM History
WHERE History.TagName IN ('S03_FT03_04_TOT01')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 1440
AND wwVersion = 'Latest'
AND DateTime >= #StartDate
AND DateTime <= #EndDate) temp
LEFT JOIN AnalogTag ON AnalogTag.TagName =temp.TagName
LEFT JOIN EngineeringUnit ON AnalogTag.EUKey = EngineeringUnit.EUKey
WHERE temp.StartDateTime >= #StartDate"""
cur.execute(Query)
print(Query)
Consider running parameterization in Python passed into a prepared SQL statement without declaring # params. Also, keep all variables in datetimes without any string conversion.
Not quite sure what your date ranges require given 6 AM and 7 AM confusion, below calculates range from yesterday at 6 AM to today at 5:59:59 AM (for 24 hours). Adjust as needed. Finally, final WHERE is moved into subquery.
from datetime import datetime, date, timedelta
import numpy as np
from pandas import DataFrame
import pandas as pd
import pymssql
BEGINDATE = datetime.combine(
date.today() - timedelta(days = 1), datetime.min.time()
) + timedelta(hours = 6)
print(BEGINDATE)
# 2021-12-12 06:00:00
ENDDATE = BEGINDATE + timedelta(days = 1) - timedelta(seconds=1)
print(ENDDATE)
# 2021-12-13 05:59:59
### PREPARED STATEMENT WITH %s PLACEHOLDERS
Query= """SELECT temp.TagName
, [DateTime]
, [Value]
, vValue
, MinRaw = ISNULL(CAST(AnalogTag.MinRaw AS VARCHAR(20)), 'N/A')
, MaxRaw = ISNULL(CAST(AnalogTag.MaxRaw AS VARCHAR(20)), 'N/A')
, Unit = ISNULL(CAST(EngineeringUnit.Unit AS VARCHAR(20)), 'N/A')
, StartDateTime
FROM (
SELECT *
FROM History
WHERE History.TagName IN ('S03_FT03_04_TOT01')
AND wwRetrievalMode = 'Cyclic'
AND wwCycleCount = 1440
AND wwVersion = 'Latest'
AND [DateTime] >= %s
AND [DateTime] <= %s
AND StartDateTime >= %s
) temp
LEFT JOIN AnalogTag ON AnalogTag.TagName = temp.TagName
LEFT JOIN EngineeringUnit ON AnalogTag.EUKey = EngineeringUnit.EUKey
"""
# RUN QUERY WITH PARAMETERS
cur.execute(Query, [BEGINDATE, ENDDATE, BEGINDATE])
print(Query)

Issue with passing parameters to SQL via pyodbc

Having trouble passing parameters to a SQL Server query. The query works with raw data, but not when I try to use parameters. SQL is not identifying parameter markers.
The error I get is
The SQL contains 0 parameter markers, but 2 parameters were supplied', 'HY000'
This is my code I'm having issues with:
currenttime = datetime.datetime(year=2021, month=6, day=7, hour=7, minute=0, second=0, microsecond=999999)
print(currenttime)
print(type(currenttime))
starttime = currenttime = datetime.datetime(year=2021, month=6,day=7,hour=6,minute=0,second=0,microsecond=999999)
params = (starttime,currenttime)
sql='''
SET QUOTED_IDENTIFIER OFF
SELECT * FROM OPENQUERY(INSQL, "SELECT DateTime, [AH41_DP04]
FROM WideHistory
WHERE wwRetrievalMode = 'Cyclic'
AND wwResolution = 1000
AND wwVersion = 'Latest'
AND DateTime >= ?
AND DateTime <= ?")
'''
print(type(params))
cursor.execute(sql,params)
for row in cursor:
print(row)``

Conditionally add WHERE clause in Python for cx_Oracle

I have the following Python code :
params = {}
query = 'SELECT * FROM LOGS '
if(date_from and date_to):
query += ' WHERE LOG_DATE BETWEEN TO_DATE(:date_start, "MM-DD-YYYY") AND LOG_DATE <= TO_DATE(:date_end, "MM-DD-YYYY")'
params['date_start'] = date_from
params['date_end'] = date_to
if(structure):
query += ' AND STRUCTURE=:structure_val'
params['structure_val'] = structure
if(status):
query += ' AND STATUS =:status'
params['status'] = status
cursor.execute(query, params)
Here I am conditionally adding the WHERE clause to the query. But there is an issue when I don't have value for the dates as it will not take the WHERE and will add AND without WHERE. If I add the where clause with the query, if there is no filter, then it will give the wrong query. Is there any better way to do this ? I have been using Laravel for sometime and it's query builder has a method when, which will help to add conditional where clauses. Anything like this in Python for cx_Oracle ?
params = {}
query = 'SELECT * FROM LOGS '
query_conditions = []
if(date_from and date_to):
query_conditions.apend(' WHERE LOG_DATE BETWEEN TO_DATE(:date_start, "MM-DD-YYYY") AND LOG_DATE <= TO_DATE(:date_end, "MM-DD-YYYY")')
params['date_start'] = date_from
params['date_end'] = date_to
if(structure):
query_conditions.append('STRUCTURE=:structure_val')
params['structure_val'] = structure
if(status):
query_conditions.append('STATUS =:status')
params['status'] = status
if query_conditions:
query += " AND ".join(query_conditions)
cursor.execute(query, params)
add them in list and join values with AND

How to insert today's date in SQL select statement using python?

I'm trying to send today variable into SQL but it is not working.
import datetime from date
today = date.today()
stmt = "select agent_email from customer_interaction_fact where to_date(DT) >= + today + ORDER BY CONVERSATION_CREATED_TIME DESC"
You don't have to compute today's date in Python. Just use the PostgreSQL function CURRENT_DATE:
stmt = "SELECT ... WHERE TO_DATE(DT) >= CURRENT_DATE ..."
What database engine you're using? You'd need to convert the python datetime object into string with format accepted by the database.
# In case YYYY-MM-DD
today_str = str(today)
stmt = f"""select agent_email
from customer_interaction_fact
where to_date(DT) >= datetime({today}, "YYYY-MM-DD")
order by CONVERSATION_CREATED_TIME DESC"""
Another solution, assuming the client (your program) is in the same timezone as the database engine, you could use your database engine datetime.now function. In SQLite for instance datetime('now')
try like below
from datetime import date
today = date.today()
stmt = "select agent_email,aht_in_secs,queueid,EFFORTSCORE from facts.public.customer_interaction_fact where agent_email <> 'Bot' and aht_in_secs is not NULL and to_date(DT) >=" + today + "ORDER BY CONVERSATION_CREATED_TIME DESC"

SQLite received a naive datetime while time zone support is active

I'm getting the following error while running my sql in python
/usr/lib/python2.6/site-packages/django/db/backends/mysql/base.py:64: RuntimeWarning: SQLite received a naive datetime (2012-06-22 15:53:43) while time zone support is active.
my query also returns the wrong data.
if i change the time up 2 hours (17:53:43 instead of 15:53:43), my timezone atm is gmt +2 so i think the problem is in the time zone.
how do i change my query to make the sql execute in the way i intend it to?
sql:
sqlQuery = """SELECT w.id, w.serial, w.finishdate, w.weighingtype_id, w.netto, w.bruto, w.deleted
FROM weighing w
LEFT JOIN weighing w1
ON w1.id = w.parent_id
WHERE w.user_id = %(userid)s"""
if date:
sqlQuery = sqlQuery + " AND (w.created = %(date)s OR w.modified > %(date)s)"
edit: added my code for transforming the datetime
data = request.GET.copy()
if 'date' in data:
try:
data['date'] = datetime.datetime.strptime(data['date'], "%Y-%m-%dT%H:%M:%S")
except:
raise error(311)

Categories