Perl to Python using MqSQLdb

M

Mike P

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
 
D

Daniel Mahoney

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.
 
M

Mike P

Thanks for that Daniel,
I've been able to apply the logic to the rest of the script i'm
converting.

There are only two bits that i don't understand in the Perl script
that i need to convert,

my $sql = shift;

and

my @row = $sth->fetchrow_array;
$$StartDate = $row[0];
$$EndDate = $row[1];
$sth->finish()

can you offer any advise?

Mike
 
J

Jeroen Ruigrok van der Werven

-On [20080812 15:16] said:
cursor=db.cursor()
cursor.execute(sql)
while (1):
row = cursor.fetchone()
if row == None:
break
combined = ', '.join(row)

Why not something like:

for row in cursor.fetchall():
combined = ', '.join(row)
 
M

Mike P

That is a nice piece of code,

I cracked the idea of the shift; problem, my final problem is still
how to convert

my @row = $sth->fetchrow_array;
$$StartDate = $row[0];
$$EndDate = $row[1];
$sth->finish()

into python code as i'm not sure what $$ means

Any help on this final part would be great!

Mike
 
F

Fredrik Lundh

Jeroen said:
Why not something like:

for row in cursor.fetchall():
combined = ', '.join(row)

which can be written as

combined = ', '.join(cursor.fetchall())

but probably won't work, since fetchall() returns a sequence of tuples,
unless I'm mistaken. Try something like this instead:

combined = ', '.join(row[0] for row in cursor.fetchall())

</F>
 
F

Fredrik Lundh

Mike said:
That is a nice piece of code,

I cracked the idea of the shift; problem, my final problem is still
how to convert

my @row = $sth->fetchrow_array;
$$StartDate = $row[0];
$$EndDate = $row[1];
$sth->finish()

into python code as i'm not sure what $$ means

according to a quick google, fetchrow_array is equivalent to Python's
fetchone (read one row from the database into a sequence object), so the
above is simply:

row = cursor.fetchone()
start_date = row[0]
end_date = row[1]

in Python. if you know for sure that the SQL statement only fetches two
columns, you can simply do

start_date, end_date = cursor.fetchone()

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top