私はLIKE LOWER('% %')
コマンドとのパターンマッチングを試みていますが、私が%sでPython変数を使用しているという事実は、それを混乱させていると思います。私はパーセンテージ記号のエスケープ文字を見つけられないようで、私のプログラムはエラーを出さない。これが問題なのですか、それとも私が紛失しているものがありますか?私がちょうどLIKE %s
を実行した場合には機能しますが、私は等しくないように検索する必要があります。別の%
複数のパーセンテージ(%)記号を使用して、PostgreSQLとPythonのパターンマッチングを使用する方法は?
>>> test = 'test'
>>> a = 'LIKE %%s%'
>>> a % test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>>
>>> a = 'LIKE %%%s%%'
>>> a % test
'LIKE %test%'
P.S.と
# Ask for the database connection, and get the cursor set up
conn = database_connect()
if(conn is None):
return ERROR_CODE
cur = conn.cursor()
print("search_term: ", search_term)
try:
# Select the bays that match (or are similar) to the search term
sql = """SELECT fp.name AS "Name", fp.size AS "Size", COUNT(*) AS "Number of Fish"
FROM FishPond fp JOIN Fish f ON (fp.pondID = f.livesAt)
WHERE LOWER(fp.name) LIKE LOWER('%%s%') OR LOWER(fp.size) LIKE LOWER('%%s%')
GROUP BY fp.name, fp.size"""
cur.execute(sql, (search_term,))
rows = cur.fetchall()
cur.close() # Close the cursor
conn.close() # Close the connection to the db
return rows
except:
# If there were any errors, return a NULL row printing an error to the debug
print("Error with Database - Unable to search pond")
cur.close() # Close the cursor
conn.close() # Close the connection to the db
return None
裸の 'except:'を使用しないでください。それはすべてを捕まえることになり、さらに 'MemoryError'も捕まえます。少なくとも例外を使用してください: '。 – Bakuriu