How to use variables in SQL statement in Python?

前端 未结 4 1122
后悔当初
后悔当初 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:58

    Many ways. DON'T use the most obvious one (%s with %) in real code, it's open to attacks.

    Here copy-paste'd from pydoc of sqlite3:

    # Never do this -- insecure!
    symbol = 'RHAT'
    c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
    
    # Do this instead
    t = ('RHAT',)
    c.execute('SELECT * FROM stocks WHERE symbol=?', t)
    print c.fetchone()
    
    # Larger example that inserts many records at a time
    purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
                 ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
                 ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
                ]
    c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
    

    More examples if you need:

    # Multiple values single statement/execution
    c.execute('SELECT * FROM stocks WHERE symbol=? OR symbol=?', ('RHAT', 'MSO'))
    print c.fetchall()
    c.execute('SELECT * FROM stocks WHERE symbol IN (?, ?)', ('RHAT', 'MSO'))
    print c.fetchall()
    # This also works, though ones above are better as a habit as it's inline with syntax of executemany().. but your choice.
    c.execute('SELECT * FROM stocks WHERE symbol=? OR symbol=?', 'RHAT', 'MSO')
    print c.fetchall()
    # Insert a single item
    c.execute('INSERT INTO stocks VALUES (?,?,?,?,?)', ('2006-03-28', 'BUY', 'IBM', 1000, 45.00))
    

提交回复
热议问题