I am a Python beginner. I found this code that works perfectly for me (connecting IP21 to Power BI) – just trying to increase the number of tags (information)
import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME='%s'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!
Instead of this, I just want tag1, tag2, … and get data which is a table with date and all those values. How do I change the code? I got an answer that makes the code run, but I think my sequel programming gets messed up. Instead of name being one tag, it makes the search name (tag1,tag2) which is not a valid search name. How do you find multiple things in sequel?
I want to change this as
tag = ' TAG1'
tag2 =' TAG2'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME=('%s,%s)'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, tag2, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!
something like that.
This works but is giving an empty table. Thank you!
If i understand well. You want to have information for some tags. So just adapt the condition for the tag in the query. What you should notice is that there is a %s for each tags. So if you want to add a tag add a %s in the query and add the corresponding variable after % in the brackets. Try to learn who to do sting replacement in python. It is easy.
import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"WHERE NAME in ('%s', '%s')"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!
Tags: exception, pythonpython