Perl to Python using MqSQLdb

Daniel Mahoney dan at catfolks.net
Tue Aug 12 09:11:31 EDT 2008


On Tue, 12 Aug 2008 05:51:19 -0700, Mike P wrote:

> Hi All,
> 
> I've been given a Perl script that i'm trying to convert into python.
> The aim of the script links to MqSQL database, but i'm stuck on one
> part
> 
>    my $sth = $dbh->prepare($sql)||
>        die "Could not prepare SQL statement ... maybe invalid?:$!\n$sql
> \n";
>    $sth->execute()||
>        die "Could not execute SQL statement ... maybe invalid?:$!\n$sql
> \n";
> 
>    while (my @row= $sth->fetchrow_array())  {
>       my $combined = join(",", @row);
>       print "$combined\n";
>    }
> 
> I don't know much MySQL or Perl..
> 
> Can anyone help to convert this for me?
> 
> Mike

You could try something like:

cursor=db.cursor()
cursor.execute(sql)
while (1):
	row = cursor.fetchone()
	if row == None:
		break
	combined = ', '.join(row)

This has NOT been tested.

More info available at http://www.kitebird.com/articles/pydbapi.html and
lots of other pages. Googling for "MySQLdb" should get you a decent list.




More information about the Python-list mailing list