1

I have the following code in python. I get this error ->tuple indices must be integers, not str

How can I pass these values into the query? I have other examples where this approach works perfectly, i don't understand why it's failling here.

def request_events_json(uei,interval,conn):

    cur = conn.cursor()

    events_query ="""select e.nodeid,n.nodelabel,e.ipaddr,count(*) as total,min(e.eventcreatetime),max(e.eventcreatetime),(regexp_matches (e.eventlogmsg,E': %(.*)'))[1] as msglog
                     from events e, node n where e.eventuei = (%s)  and e.eventcreatetime > now() - interval (%s) and n.nodeid=e.nodeid
                     group by n.nodelabel,e.nodeid,e.ipaddr,msglog
                     order by e.nodeid, count(*) desc limit 10;"""



    try:
       print('## Requesting events ##')
       cur.execute(events_query,('uei.opennms.org/syslogd/cisco/line','5 min'))
       .......
1
  • What postgresql driver are you using? Commented Jul 4, 2012 at 12:10

1 Answer 1

1

With my version of PostgreSQL the round brackets after interval are forbidden.

Update:

It is the percent-sign in the regexp. Double it.

Sign up to request clarification or add additional context in comments.

4 Comments

psycopg2 and the postgres is 8.4.3. If I change the query to events_query ="""select nodeid from events where eventuei=(%s) and eventcreatetime > now() - interval %s limit 10;""" it works...
I took the regex part out and it worked (regexp_matches (e.eventlogmsg,E': %(.*)'))[1] as msglog ...can anyone help me on how to mantain this part and pass the parameters to the query ?
Maybe doubling the % helps in your regex. E': %%(.*)'
Or maybe replacing that percent sign by \\x25 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.