Python MySQL database Operations perl operations MySQL database _python

Source: Internet
Author: User
Tags commit prepare create database 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 operations on the MySQL database web
I. Installing the DBI MODULE
Step 1:
Download the dbi.zip from the Tools section, download the WinZip to a temp directory after downloading, a total of 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 for an invalid command, you can run it in the Perl/bin directory
Two. Install Dbd-mysql module
Download the dbd-mysql.zip from the software download and install the same method.
Three. Preparing the database
Start MySQL, first create a database MyData, 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 () is not NULL,
-> email varchar NOT NULL,
-> telephone int () 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. The following Perl program is used to insert several records and make inquiries.
Use DBI;
#连接数据库mydata
My $dbh = Dbi->connect (' DBI:mysql:mydata ') or die "Cannot connect to the database:". dbi->errstr;
Print "insert several records \ n";
My $sth = $dbh->prepare (q{
INSERT into address (ID, name,email,telephone) VALUES (?,?,?,?)
}) });
Print "Enter record, carriage return end:";
while ($inputdata =<>) {
Chop $inputdata;
Last unless ($inputdata);
My ($id, $name, $email, $tel) = Split (/,/, $inputdata);
$sth->execute ($id, $name, $email, $tel)
}
# $DBH->commit;
Print "Prints out the email address and phone number according to the name entered";
My $sth = $dbh->prepare (' SELECT * to address WHERE name=? ')
Or Die $DBH->errstr;
Print "Please enter name, carriage 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.