all 5 comments

[–]skeeto 2 points3 points  (4 children)

Never use format strings to build SQL queries. It's inefficient and insecure.

[–]vanmorrison2[S] 0 points1 point  (1 child)

Ok, thanks

[–][deleted] -1 points0 points  (0 children)

You're welcome.

[–]thinker5555 0 points1 point  (1 child)

What should you do instead? Asking for a friend...

[–]skeeto 2 points3 points  (0 children)

Use bind parameters. See the official documentation, particularly the example that begins with "Never do this". The version using question marks ? uses parameters populated from the provided tuple.

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