Psycopg2 using wildcard causes TypeError

后端 未结 2 749
轮回少年
轮回少年 2020-12-19 08:48

Currently I am attempting to search a database to grab certain events. My query is as such

SELECT * FROM events WHERE summary ILIKE E\'%test%\' AND start_tim         


        
相关标签:
2条回答
  • 2020-12-19 09:04

    Not sure if this is the full root of your problem, but I think you need to escape your wildcards or the parameterization logic will get confused.

    SELECT * FROM events WHERE summary ILIKE E'%%test%%' AND start_time > %(begin)s 
    

    I think %% is the correct escaping, but it could be \%

    0 讨论(0)
  • 2020-12-19 09:17

    my guess is something about your "%"'s is confusing python. in psycopg2 i do my wildcard "like" queries like this:

    
    #!/usr/bin/python
    
    import sys,os.path,psycopg2
    db=psycopg2.connect("dbname=music")
    
    for line in sys.argv[1::]:
        cursor=db.cursor()
        key="%"+line+"%"
        cursor.execute("select count(*) from pool where path like %s",(key,))
        if cursor.fetchone()[0] != 1:
            sys.stderr.write("ambiguous stem or no such song")
            sys.exit(-1)
        cursor.execute("insert into spool select path from pool where path like %s",(key,))
        cursor.close()
        db.commit()
    db.close()
    

    with user-supplied search strings like in this script you would probably want to escape out any "%"'s in them, which i suspect would otherwise be legit wildcards in the query, but i haven't gotten that far yet

    0 讨论(0)
提交回复
热议问题