파이썬이 펄의 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
죄송합니다, 당신 말이 맞습니다. –
MySQLdb.cursors.DictCursor가되어야하지만, 좋은 예제로는 여전히 +1입니다. –