[Tutor] Understanding DBAPI cursor.execute

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Aug 1 10:39:42 CEST 2004

> thanks I think I understand the quoting issue.  But you suggested
that I
> should follow  "MySQLdb requirements in formating the parameters.".
Where do
> I find such information

In the MySql documentation.

> and this implies that not all DBAPI modules are NOT
> the same.

NO it implies that the SQL formatting requirements used by each DB
is slightly different. That's why you should use the DBAPI to do the

>  I thought the purpose of the DBAPI was to create a standard
> interface???  I'm guessing it failed or the params issue was not

It does. But when you use Python string formatting you are bypassing
the module and taking responsibility for formatting upon yourself.

cursor.execute("SELECT FOO FROM %s" % 'BAR')

Is exactly the same to the DBAPI as

cursor.execute("SELECT FOO FROM BAR")

ie a hard coded string. If you want the DBAPI module to do its stuff
you must pass the data in as parameters:

cursor.execute("SELECT FOO FROM %s", 'BAR')

By using Python formatting for the SQL string you are effectively
bypassing all the DBAPI formatting code.

Alan G.

More information about the Tutor mailing list