Take what I want to use as the condition of the WHERE statement as an argument, shape it and store it in wheres.
I want to execute a SELECT statement using the wheres as a conditional expression, but SyntaxError occurs.
import psycopg2 import config def reference(formMemberList): dbusers ='postgres' dbnames ='CRM' passwords ='taku0217' conn = psycopg2.connect(" user=" + dbusers +" dbname=" + dbnames +" password=" + passwords) cur = conn.cursor() wheres = "" for menber in formMemberList: if(formMemberList.index(menber)!=0): wheres += "AND " wheres += menber cur.execute("SELECT * FROM customer WHERE'%s'" %wheres) rows = cur.fetchall() cur.execute("COMMIT") cur.close() conn.close() return rows formMemberList = ["name:'takumi'", "birth:'1995-02-17'"] rows = reference(formMemberList)
Answer # 1
There seem to be two places like that.Single quote at joint
Contents of formMemberList
cur.execute("SELECT * FROM customer WHERE'%s'" %wheres)
This line encloses %s in single quotes, so the part after WHERE
'Will be surrounded by.
"name:'takumi'"Therefore, it cannot be embedded in SQL as it is.
This time, for the time being
=I replaced it with and matched the shape.
If you put "1 = 1" after where,Snake foot 2
ANDIt's convenient because you don't need to judge whether or not to insert.
The sample code uses it to make things easier.
The member is menber, but I leave it as it is.Conclusion
I cut out the necessary parts and wrote the minimum code. Please refer to the following.
Thank you yymmt for pointing this out.
def reference(formMemberList): wheres = "1 = 1" for menber in formMemberList: wheres += "AND " wheres += menber.replace(":", "=") print("SELECT * FROM customer WHERE %s" %wheres) return None formMemberList = ["name:'takumi'", "birth:'1995-02-17'"] rows = reference(formMemberList)
- python - i want to make the result obtained by the for statement a two-dimensional array
- infinite series using for statement in python
- Python use %s, %d, %f in sql statement
- Python solves the problem of mysql where in pair (list,, array)
- position of python for statement
- about python if statement
- python - == variable that matches for all
- i don't understand the python if statement
- python - variables are arbitrarily updated by for statement
- python - extraction of rows where two are true by setting conditions for 3 columns of dataframe
- python - add the answers (numbers) found in the for statement
- python/for statement causes memory leak and crashes seeking a smart solution
- i want to eliminate the line break in the for statement in python
- python - the while statement cannot resolve the invalid syntax error
- python - unable to specify pytorch variable layer to gpu
- python (tkinter): i'm using grid, where tkinter's dialog doesn't look as expected
- [python] i want to put the data scraped using the for statement in the spreadsheet cell
- Python novice how to bind variable operation in closure
- python - about referencing the file containing the loop variable in the for statement