D
D
Dmitry Kasatkin2022-04-15 11:49:50
Python
Dmitry Kasatkin, 2022-04-15 11:49:50

How to issue an SQL query to PostgreSQL and substitute the value of variables there?

Help compose a sql query to search for a part of a string from the database.
everything worked like clockwork with sqlite, but in postgresql I can't deal with LIKE. How to make a request?
In my query, it only searches for the full word.

cursor.execute('SELECT * FROM tkd WHERE street_str LIKE %s AND  house = %s', (street_str,  house))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Kravchuk, 2022-04-15
@TKDBOT

street_str must contain a search mask
street_str="example" - will search by equality
street_str="example%" - will search starting with example
street_str="%example" - will search ending with example
street_str="%example%" - will search for example anywhere

A
Alexander, 2022-04-15
@shabelski89

cursor.execute("SELECT * FROM tkd WHERE street_str LIKE '%s' AND house = %s", (street_str,  house))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question