Python & MySQLdb - Part 1: The Absolute Basics
mikegrouchy.com
mikegrouchy.com
* You can easily change database engines without rewriting all your queries
* SQLAlchemy has built-in protection from SQL injection attacks.
* You can define your database tables and relations once (in SQLAlchemy) and it will create the database for you if it doesn't already exist.
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 :).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.
in response to lincolnq below: you are correct, if the engine does support autocommit it is turned on automatically. http://www.python.org/dev/peps/pep-0249/