Python MySQL database Operation Perl operation MySQL Database

Source: Internet
Author: User
Tags python mysql
First download MySQLdb
#encoding =GBK
Import MySQLdb
#import SYS
#
#reload (SYS)
#sys. setdefaultencoding (' Utf-8 ')
print ' Connection ... '
Host= ' 192.168.1.77 '
User= ' Root '
Passwd= ' 123456 '
db= ' Test '
conn = MySQLdb.connect (host,user,passwd,db,charset= ' GBK ')
print ' Connection success '
cursor = Conn.cursor ()
#query = "INSERT INTO Test (Id,name) values (%s,%s)"
#param = ("1", "kanji")
#cursor. Execute (Query,param)
#
#conn. Commit ()
Cursor.execute (' SELECT * from Test ')
rows = Cursor.fetchall ()
For row in rows:
Print Row[1]
Cursor.close ()
Conn.close ()
A more detailed approach to Perl operating MySQL database online
I. Installing the DBI MODULE
Step 1:
Download the dbi.zip from the Tools section and unpack to a temp directory with WinZip after download, total three files:
Readme
Dbi.ppd
DBI.tar.gz
Step 2:
Under the DOS window, run the following DOS command in the TEMP directory:
PPM Install DBI.PPD
If you are prompted with an invalid command, you can run it in the Perl/bin directory
Two. Installing the Dbd-mysql module
Download the dbd-mysql.zip from the software download and install the same method.
Three. Prepare the database
Start MySQL, create a database MyData first, and then create a table address
mysql> CREATE DATABASE MyData;
Query OK, 1 row Affected (0.00 sec)
mysql> use MyData;
Database changed
Mysql> CREATE TABLE Address (
-ID Int (5) NOT NULL,
, name varchar (+) NOT NULL,
Email varchar (+) NOT NULL,
-Telephone int (n) null);
Query OK, 0 rows affected (0.05 sec)
Enter some data:
mysql> INSERT into address values (
1, ' Nighthawk ', ' nighthawk@163.net ', 92384092);
Query OK, 1 row Affected (0.00 sec)
Four. Use the Perl program below to insert several records and make queries.
Use DBI;
#连接数据库mydata
My $dbh = Dbi->connect (' DBI:mysql:mydata ') or Die "Unable to connect to database:". dbi->errstr;
Print "insert several records \ n";
My $sth = $dbh->prepare (q{
INSERT into address (ID, name,email,telephone) VALUES (?,?,?,?)
}) });
Print "Input record, enter end:";
while ($inputdata =<>) {
Chop $inputdata;
Last unless ($inputdata);
My ($id, $name, $email, $tel) = Split (/,/, $inputdata);
$sth->execute ($id, $name, $email, $tel)
}
# $DBH->commit;
Print "The e-mail address and phone number are printed under the name entered";
My $sth = $dbh->prepare (' SELECT * FROM address WHERE name=? ')
Or Die $DBH->errstr;
Print "Please enter name, return end:";
while ($inputname =<>) {
My @data;
Chomp $inputname;
Last unless ($inputname);
$sth->execute ($inputname) or die "error:". $sth->errstr;
while (@data = $sth->fetchrow_array ()) {
Print "Email: $data [2]\t Telephone: $data [3]\n";
}
}
#断开连接
$DBH->disconnect;
Nighthawk
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.