国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python Mysql數據庫操作 Perl操作Mysql數據庫

2019-11-25 18:45:12
字體:
來源:轉載
供稿:網友
首先下載 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","漢字")
#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()
Perl操作Mysql數據庫 網上的比較詳細的方法
一. 安裝DBI模塊
步驟1:
從TOOLS欄目中下載DBI.zip,下載完后用winzip解開到一個temp目錄,共有三個文件:
Readme
DBI.ppd
DBI.tar.gz
步驟2:
在DOS窗口下,temp目錄中運行下面的DOS命令:
ppm install DBI.ppd
如果提示無效命令,可在perl/bin目錄下運行
二. 安裝DBD-Mysql模塊
從軟件下載中下載DBD-Mysql.zip,安裝方法同一.
三. 準備數據庫
啟動mysql,首先創建一個數據庫mydata,然后創建一個表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(40) not null,
-> email varchar(50) not null,
-> telephone int(12) null);
Query OK, 0 rows affected (0.05 sec)
輸入些數據:
mysql> insert into address values (
-> 1,'Nighthawk','nighthawk@163.net',92384092);
Query OK, 1 row affected (0.00 sec)
四. 下面用perl程序來插入若干記錄并做查詢.
use DBI;
#連接數據庫mydata
my $dbh = DBI->connect('DBI:mysql:mydata') or die "無法連接數據庫: " . DBI->errstr;
print "插入若干記錄/n";
my $sth = $dbh->prepare(q{
INSERT INTO address (id, name,email,telephone) VALUES (?, ?, ?, ?)
}) });
print "輸入記錄,回車結束:";
while ($inputdata =<>) {
chop $inputdata;
last unless($inputdata);
my ($id, $name,$email, $tel) = split( /,/, $inputdata);
$sth->execute($id, $name, $email,$tel)
}
# $dbh->commit;
print "下面根據輸入的名字打印出EMAIL地址和電話/n";
my $sth = $dbh->prepare('SELECT * FROM address WHERE name=?')
or die $dbh->errstr;
print "請輸入姓名,回車結束:";
while ($inputname =<>) {
my @data;
chomp $inputname;
last unless($inputname);
$sth->execute($inputname) or die "錯誤: " . $sth->errstr;
while (@data = $sth->fetchrow_array()) {
print "Email:$data[2]/t Telephone:$data[3]/n";
}
}
#斷開連接
$dbh->disconnect;
Nighthawk
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 囊谦县| 长子县| 依兰县| 万盛区| 湘潭县| 金山区| 景泰县| 新泰市| 祁阳县| 铁岭市| 余干县| 永州市| 绥中县| 临泽县| 宕昌县| 田林县| 河南省| 柳河县| 滦南县| 望谟县| 广汉市| 贵溪市| 乐亭县| 天门市| 讷河市| 新蔡县| 会理县| 吉水县| 康保县| 准格尔旗| 水富县| 屯留县| 庄河市| 新昌县| 和平县| 南澳县| 邹平县| 宣城市| 乌拉特前旗| 稷山县| 德庆县|