Ich versuche, Muster mit dem LIKE LOWER('% %')
Befehl Muster aber ich denke, die Tatsache, dass ich eine Python-Variable mit% s verwende, muckt es auf. Ich kann keine Escape-Zeichen für das Prozentzeichen finden und mein Programm gibt mir keine Fehler. Ist das das Problem oder gibt es noch etwas, das mir fehlt? Es funktioniert, wenn ich nur LIKE %s
ausführen, aber ich muss in der Lage sein, wie nicht gleich zu suchen.Wie verwendet man Mustervergleich mit PostgreSQL und Python mit mehreren Prozent (%) Symbolen?
# 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
Niemals ein leeres 'except:' verwenden. Es wird alles fangen, sogar 'MemoryError'. Zumindest verwenden Sie 'exception Exception: '. – Bakuriu