Jump to content

View more

Image of the Day

雑魚は多めにして、爽快感重視にしつつ・・・(´・ω・`)
早いとこ、ベースを作って、完成にもっていかないとね。
タイトルもまだ迷ってるだよなぁ。 
#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

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

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 Biffenbob   Members   

105
Like
0Likes
Like

Posted 08 April 2013 - 01:05 PM

# 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, 08 April 2013 - 01:07 PM.


#2 Álvaro   Members   

20913
Like
3Likes
Like

Posted 08 April 2013 - 02:02 PM

I guess the second version performs SQL escaping for you.

#3 Matias Goldberg   Members   

9478
Like
4Likes
Like

Posted 08 April 2013 - 03:15 PM

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, 08 April 2013 - 03:16 PM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.