Perl to Python using MqSQLdb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    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$s ql
    \n";
    $sth->execute()||
    die "Could not execute SQL statement ... maybe invalid?:$!\n$s ql
    \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
  • Daniel Mahoney

    #2
    Re: Perl to Python using MqSQLdb

    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$s ql
    \n";
    $sth->execute()||
    die "Could not execute SQL statement ... maybe invalid?:$!\n$s ql
    \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.curso r()
    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.

    Comment

    • Mike P

      #3
      Re: Perl to Python using MqSQLdb

      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

      Comment

      • Jeroen Ruigrok van der Werven

        #4
        Re: Perl to Python using MqSQLdb

        -On [20080812 15:16], Daniel Mahoney (dan@catfolks.n et) wrote:
        >cursor=db.curs or()
        >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...

        Comment

        • Mike P

          #5
          Re: Perl to Python using MqSQLdb

          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

          Comment

          • Fredrik Lundh

            #6
            Re: Perl to Python using MqSQLdb

            Jeroen Ruigrok van der Werven wrote:
            >cursor=db.curs or()
            >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.f etchall())

            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>

            Comment

            • Fredrik Lundh

              #7
              Re: Perl to Python using MqSQLdb

              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>

              Comment

              Working...