473,503 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perl to Python using MqSQLdb

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
Aug 12 '08 #1
6 1114
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.

Aug 12 '08 #2
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
Aug 13 '08 #3
-On [20080812 15:16], Daniel Mahoney (da*@catfolks.net) wrote:
>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)

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
イェルーン ラウフ*ック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
Focus on your one purpose...
Aug 13 '08 #4
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
Aug 13 '08 #5
Jeroen Ruigrok van der Werven wrote:
>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)
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>

Aug 13 '08 #6
Mike P wrote:
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>

Aug 13 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2116
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will...
13
2686
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
42
4028
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
13
1982
by: Zeljko Vrba | last post by:
Now, why would I like to learn Python: because of IronPython port to CLR. Perl port to CLR doesn't seem to be coming soon. First Perl6 for Parrot has to be written, and then other backends.. ...
17
3075
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
31
4724
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
9
4501
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
20
4023
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
13
2066
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.