3 ms·
something that I tore my hair out over when building my website (gibsonandlily.com) was how to handle input santizing for mysql. I wrote some functions for it
by blhack 17y ago
something that I tore my hair out over when building my website (gibsonandlily.com) was how to handle input santizing for mysql.
I wrote some functions for it in python (and I'm sure that they don't work very well. Yes, I'm sure some of you could destroy my website...it is a hobby, please don't).
I later found out (and I'm in the process of replacing code) that you can have mysqldb handle it for you like this:
cursor.execute("insert into links(url,description) values(%s,%s)", (url,desciption))
where url and description (the second ones) are variable holding the values you want to insert.
this was different than the way that I was doing it before, which was:
insert_query = "insert into links(url,description) values('%s','%s')" % (url,description)
cursor.execute(insert_query)
I'm sure this sounds completely stupid to some of you who make your living writing web apps...but I thought it was really cool :).
- lincolnq 17y agoYes, it's very important to do it the first way! mysqldb has a bunch of Python->MySQL conversions. If you look at the source you can read converters.py -- it can convert lists, strings, numbers, dates and a lot of other types too.