Python Mysql數(shù)據(jù)庫操作 Perl操作Mysql數(shù)據(jù)庫
更新時間:2009年01月12日 19:59:22 作者:
python對mysql數(shù)據(jù)庫的一些操作實現(xiàn)代碼
首先下載 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數(shù)據(jù)庫 網(wǎng)上的比較詳細的方法
一. 安裝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,安裝方法同一.
三. 準備數(shù)據(jù)庫
啟動mysql,首先創(chuàng)建一個數(shù)據(jù)庫mydata,然后創(chuàng)建一個表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)
輸入些數(shù)據(jù):
mysql> insert into address values (
-> 1,'Nighthawk','nighthawk@163.net',92384092);
Query OK, 1 row affected (0.00 sec)
四. 下面用perl程序來插入若干記錄并做查詢.
use DBI;
#連接數(shù)據(jù)庫mydata
my $dbh = DBI->connect('DBI:mysql:mydata') or die "無法連接數(shù)據(jù)庫: " . DBI->errstr;
print "插入若干記錄\n";
my $sth = $dbh->prepare(q{
INSERT INTO address (id, name,email,telephone) VALUES (?, ?, ?, ?)
}) });
print "輸入記錄,回車結(jié)束:";
while ($inputdata =<>) {
chop $inputdata;
last unless($inputdata);
my ($id, $name,$email, $tel) = split( /,/, $inputdata);
$sth->execute($id, $name, $email,$tel)
}
# $dbh->commit;
print "下面根據(jù)輸入的名字打印出EMAIL地址和電話\n";
my $sth = $dbh->prepare('SELECT * FROM address WHERE name=?')
or die $dbh->errstr;
print "請輸入姓名,回車結(jié)束:";
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
#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數(shù)據(jù)庫 網(wǎng)上的比較詳細的方法
一. 安裝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,安裝方法同一.
三. 準備數(shù)據(jù)庫
啟動mysql,首先創(chuàng)建一個數(shù)據(jù)庫mydata,然后創(chuàng)建一個表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)
輸入些數(shù)據(jù):
mysql> insert into address values (
-> 1,'Nighthawk','nighthawk@163.net',92384092);
Query OK, 1 row affected (0.00 sec)
四. 下面用perl程序來插入若干記錄并做查詢.
use DBI;
#連接數(shù)據(jù)庫mydata
my $dbh = DBI->connect('DBI:mysql:mydata') or die "無法連接數(shù)據(jù)庫: " . DBI->errstr;
print "插入若干記錄\n";
my $sth = $dbh->prepare(q{
INSERT INTO address (id, name,email,telephone) VALUES (?, ?, ?, ?)
}) });
print "輸入記錄,回車結(jié)束:";
while ($inputdata =<>) {
chop $inputdata;
last unless($inputdata);
my ($id, $name,$email, $tel) = split( /,/, $inputdata);
$sth->execute($id, $name, $email,$tel)
}
# $dbh->commit;
print "下面根據(jù)輸入的名字打印出EMAIL地址和電話\n";
my $sth = $dbh->prepare('SELECT * FROM address WHERE name=?')
or die $dbh->errstr;
print "請輸入姓名,回車結(jié)束:";
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
您可能感興趣的文章:
- python Django連接MySQL數(shù)據(jù)庫做增刪改查
- Python操作MySQL數(shù)據(jù)庫9個實用實例
- python連接mysql數(shù)據(jù)庫示例(做增刪改操作)
- Python連接mysql數(shù)據(jù)庫的正確姿勢
- python備份文件以及mysql數(shù)據(jù)庫的腳本代碼
- python3使用PyMysql連接mysql數(shù)據(jù)庫實例
- linux下python3連接mysql數(shù)據(jù)庫問題
- win7上python2.7連接mysql數(shù)據(jù)庫的方法
- python操作MySQL數(shù)據(jù)庫具體方法
- Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實例
- Python操作mysql數(shù)據(jù)庫實現(xiàn)增刪查改功能的方法
相關(guān)文章
PyCharm運行Python代碼時出現(xiàn)"未找到模塊"錯誤解決步驟
在使用python的過程中經(jīng)常會遇到一個問題,就是叫什么名字的模塊未發(fā)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于PyCharm運行Python代碼時出現(xiàn)"未找到模塊"錯誤的解決步驟,需要的朋友可以參考下2023-11-11用Python從0開始實現(xiàn)一個中文拼音輸入法的思路詳解
中文輸入法是一個歷史悠久的問題,但也實在是個繁瑣的活,不知道這是不是網(wǎng)上很少有人分享中文拼音輸入法的原因,接下來通過本文給大家分享使用Python從0開始實現(xiàn)一個中文拼音輸入法,需要的朋友可以參考下2019-07-07python使用OS模塊操作系統(tǒng)接口及常用功能詳解
os是?Python?標準庫中的一個模塊,提供了與操作系統(tǒng)交互的功能,在本節(jié)中,我們將介紹os模塊的一些常用功能,并通過實例代碼詳細講解每個知識點2023-06-06Python調(diào)用win10toast框架實現(xiàn)定時調(diào)起系統(tǒng)通知
win10toast是一個windows通知的出發(fā)框架,使用它可以輕松的調(diào)起系統(tǒng)通知。通過它可以很方便的做一個定時通知的功能應(yīng)用。本文將調(diào)用win10toast實現(xiàn)定時調(diào)起系統(tǒng)通知功能,需要的可以參考一下2022-01-01win32com操作word之Application&Documents接口學習
這篇文章主要為大家介紹了win32com操作word之Application&Documents接口學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01python正則表達式re.sub各個參數(shù)的超詳細講解
Python 的 re 模塊提供了re.sub用于替換字符串中的匹配項,下面這篇文章主要給大家介紹了關(guān)于python正則表達式re.sub各個參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07