Jump to content
  • Advertisement
Sign in to follow this  

Using sqlite3 for python, one method secure, one not, why?

This topic is 1871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

# 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()


Taken from http://docs.python.org/2/library/sqlite3.html


My understanding is a SQL injection is when instead of supplying data or a variable you substitute a SQL command.  My question is why is the second option better then the first.  What makes the second option better then the first?

Edited by Biffenbob

Share this post

Link to post
Share on other sites

The 2nd option already knows the input is a parameter and there's no doubt it can't be part of the command in the statement, so SQL injection is not possible.


In the 1st option, you would have to properly escape the string, and there's the risk that you're not escaping it properly.

Edited by Matias Goldberg

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!