How to use variables in SQL statement in Python?

前端 未结 4 1118
后悔当初
后悔当初 2020-11-21 05:36

Ok so I\'m not that experienced in Python.

I have the following Python code:

cursor.execute(\"INSERT INTO table VALUES var1, var2, var3,\")
<         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 05:46

    http://www.amk.ca/python/writing/DB-API.html

    Be careful when you simply append values of variables to your statements: Imagine a user naming himself ';DROP TABLE Users;' -- That's why you need to use sql escaping, which Python provides for you when you use the cursor.execute in a decent manner. Example in the url is:

    cursor.execute("insert into Attendees values (?, ?, ?)", (name,
    seminar, paid) )
    

提交回复
热议问题