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 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.
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
-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...
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
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>
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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....
|
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..
...
|
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
| |
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...
|
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...
|
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:
...
|
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;
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |