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)
curs.execute(cmd)

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.

About these ads
Categories: python Tags: , , ,
  1. nnxkdk
    January 3, 2013 at 11:17 | #1

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

    • January 3, 2013 at 11:43 | #2

      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

Fill in your details below or click an icon to log in:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 30 other followers

%d bloggers like this: