什麼是Python的Perl的DBI的等價物,我該如何使用它?更具體地說,以下Perl代碼的Python等價物是什麼?什麼是Perl的DBI的Python等價物?
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");
@JanHudec我在代碼中看不到任何類型的查詢中的字符串插值。謹慎澄清? – shylent 2012-11-04 22:08:25
對不起,你是對的。 – 2012-11-05 09:28:50
應該是MySQLdb.cursors.DictCursor,但仍然是一個很好的示例+1。 – 2012-11-11 16:41:52