r/sqlite • • Feb 11 '21

Sqlite3 and Python - Part 1

https://youtube.com/watch?v=tVlyIBvuT1M&feature=share
0 Upvotes

5 comments sorted by

3

u/skeeto Feb 11 '21

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

1

u/vanmorrison2 Feb 13 '21

Ok, thanks

0

u/[deleted] Feb 13 '21

You're welcome.

1

u/thinker5555 Feb 11 '21

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

3

u/skeeto Feb 11 '21

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