Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 875233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:14:06+00:00 2026-05-15T11:14:06+00:00

What is Python’s equivalent of Perl’s DBI and how do I use it? More

  • 0

What is Python’s equivalent of Perl’s DBI and how do I use it? More specifically, what is the Python equivalent of the following Perl code?

use DBI;

# connect to a MySQL database
my $dbh = DBI->connect("dbi:mysql:database=$database; host=localhost; port=3306", $user, $pass);

# select and read a few rows
my $sth = $dbh->prepare("SELECT id, name FROM table WHERE id <= ?;");
$sth->execute(321);
while (my @row = $sth->fetchrow_array) {
  my $id = $row[0];
  my $name = $row[1];
  print "$id. $name\n";
}

# write to the database
$sth = $dbh->prepare("INSERT INTO table (id, name) VALUES (?, ?);");
$sth->execute(123, "foo");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:14:07+00:00Added an answer on May 15, 2026 at 11:14 am
    import MySQLdb.cursors
    
    db = MySQLdb.connect(db=database, host=localhost,
                         port=3306, user=user, passwd=pass,
                         cursorclass=MySQLdb.cursors.DictCursor)
    cur = db.cursor()
    
    #this is not string interpolation, everything is quoted for you automatically
    cur.execute("select id, name from table where id = %s", (321,))
    
    for row in cur.fetchall():
        print "%s. %s" % (row['id'], row['name'])
    
    cur.execute("insert into table (id, name) values (%s, %s)", (123, 'foo'))
    db.commit() # required, because autocommit is off by default
    

    Python database API use a common convention, that is pretty much the same across different databases (but not quite!). You can read the MySQLdb documentation here.

    There is also a more feature-rich interface to mysql, called oursql. It has real parametrization (not just glorified string interpolation), server-side cursors, data streaming and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python, Perl and PHP, all support TCP stream sockets . But exactly how do
python newbie here. I'm writing the code to control an experiment that has multiple
Python and django newbie question, here is code: class Client(User) #some fields client=Client() client.save()
Python extension modules written in C are faster than the equivalent programs written in
Python decorators are fun to use, but I appear to have hit a wall
python manage.py dumpdata modelName > file.json . created an empty database for a user
Python 2.5 won't let me use this syntax: try: code_that_raises_exception() except Exception as e:
Python's lack of static typing makes it possible to use and rely on classes
Python is pretty clean, and I can code neat apps quickly. But I notice
Python is simply bringing up another prompt when I enter the following piece of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.