Home > python > SQLite: prevent SQL injection

SQLite: prevent SQL injection

DON’T do this:

cmd = "update people set name='{0}' where id='{1}'".format(name, id)

DO this instead:

cmd = "update people set name=? where id=?"
curs.execute(cmd, (name, id))

If you are using MySQL or PostgreSQL, use %s (even for numbers and other non-string values!) and if you are using SQLite, use ?.

Tip from here.

Categories: python Tags: , , ,
  1. nnxkdk
    January 3, 2013 at 11:17

    how about using a good orm like sqlalchemy, or django orm?

    • January 3, 2013 at 11:43

      It’s like shooting a sparrow with a cannon. I need sqlite for small scripts. Leave sqlalchemy and orm’s for large(r) projects.

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: