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.

Categories: python Tags: , , ,
  1. nnxkdk's avatar
    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 comment

Design a site like this with WordPress.com
Get started