1. 程式人生 > >perl--DBI 模組中提取資料的fetch方法彙總(三)

perl--DBI 模組中提取資料的fetch方法彙總(三)

perl下十種fetch資料的區別以及返回資料結構:

1.fetchrow_array 提取下一行資料並將欄位儲存在陣列中返回:

[codesyntax lang="perl"]

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd") or die DBI->errstr;

$sql="select * from 115_users limit 10";

$sth=$dbh->prepare($sql);

$rv=$sth->execute;

while(@row_ary=$sth->fetchrow_array){

dump(@row_ary);

print "password: " .$row_ary[2]."\n";

}

##輸出內容:

(

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

)

password: Y9MoErtE+iZG5PkYHMJobhij58E

(

2,

"auto-gre-2\@ttlsa.com",

"UqHxPoLmKY7ClyCZaXPdHepjUOo",

"auto-gre-2",

)

password: UqHxPoLmKY7ClyCZaXPdHepjUOo

2.fetchrow_arrayref 提取下一行資料並返回一個包含欄位值的引用陣列

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd") or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$sth=$dbh->prepare($sql);

$rv=$sth->execute;

while($ary_ref=$sth->fetchrow_arrayref){

dump($ary_ref);

print "email: ".$$ary_ref[1]."\n";

}

##輸出內容:

[

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

]

email: [email protected]

[

2,

"auto-gre-2\@ttlsa.com",

"UqHxPoLmKY7ClyCZaXPdHepjUOo",

"auto-gre-2",

]

email: [email protected]

3.fetchrow_hashref 提取下一行資料並返回一個包含欄位名和欄位值對的雜湊引用

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd") or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$sth=$dbh->prepare($sql);

$rv=$sth->execute;

while ($hash_ref=$sth->fetchrow_hashref) {

dump($hash_ref);

print "password: " . $$hash_ref{'passwd'} . "\n";

}

##輸出內容:

{

email => "auto-gre-1\@ttlsa.com",

passwd => "Y9MoErtE+iZG5PkYHMJobhij58E",

user_id => 1,

user_name => "auto-gre-1",

}

password: Y9MoErtE+iZG5PkYHMJobhij58E

{

email => "auto-gre-2\@ttlsa.com",

passwd => "UqHxPoLmKY7ClyCZaXPdHepjUOo",

user_id => 2,

user_name => "auto-gre-2",

}

password: UqHxPoLmKY7ClyCZaXPdHepjUOo

4.fetchall_arrayref 取出所有行內容並返回包含每行欄位值的引用陣列

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd") or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$sth=$dbh->prepare($sql);

$rv=$sth->execute;

$ary_ref=$sth->fetchall_arrayref;

dump($ary_ref);

print "@$ary_ref\n";

foreach (@$ary_ref) {

print "$_->[2]\n";

}

[/codesyntax]

輸出內容:

[

[

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

],

[

2,

"auto-gre-2\@ttlsa.com",

"UqHxPoLmKY7ClyCZaXPdHepjUOo",

"auto-gre-2",

],

]

ARRAY(0x9e7f5d8) ARRAY(0x9e7f578)

password: Y9MoErtE+iZG5PkYHMJobhij58E

password: UqHxPoLmKY7ClyCZaXPdHepjUOo

5.fetchall_hashref($key_field) 取出所有內容並返回每行欄位名和欄位值對的雜湊引用

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd") or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$sth=$dbh->prepare($sql);

$rv=$sth->execute;

$hash_ref=$sth->fetchall_hashref(user_id);

dump($hash_ref);

print '-' x 20 ."\n";

print $hash_ref->{'10'}->{'email'}."\n\n\n";

while (($user_id,$value)=each %$hash_ref){

print "\$user_id: $user_id\n";

while (($key,$value1)=each %$value) {

print "$key ==> $value1\n";

}

}

##輸出內容:

{

1 => {

email => "auto-gre-1\@ttlsa.com",

passwd => "Y9MoErtE+iZG5PkYHMJobhij58E",

user_id => 1,

user_name => "auto-gre-1",

},

2 => {

email => "auto-gre-2\@ttlsa.com",

passwd => "UqHxPoLmKY7ClyCZaXPdHepjUOo",

user_id => 2,

user_name => "auto-gre-2",

},

}

--------------------

[email protected]

$user_id: 6

passwd ==> ZWCagapChduSFnB2nJcQ3vOCYI4

email ==> [email protected]

user_id ==> 6

user_name ==> auto-gre-6

$user_id: 3

passwd ==> 9YiNWIw1bjqEMrCOmbtwFBBqc3U

email ==> [email protected]

user_id ==> 3

user_name ==> auto-gre-3

融合prepare( ),execute( )和fetchrow_arrayref( )方法的操作:

6.selectrow_array($statement) 返回一行資料的陣列

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd",{ PrintError=>0, RaiseError=>1 }) or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

@row_ary=$dbh->selectrow_array($sql);

dump(@row_ary);

my ($count,$max)[email protected]_ary=$dbh->selectrow_array("select count(*),max(user_id) from ttlsa_user_00");

print "number: $count; max: $max\n";

##輸出內容:

(

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

)

number: 10; max: 10

7.selectrow_arrayref($statement) 返回一行資料的引用陣列

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd",{ PrintError=>0, RaiseError=>1 }) or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$ary_ref=$dbh->selectrow_arrayref($sql);

dump($ary_ref);

$ary_ref=$dbh->selectrow_arrayref("select count(*),max(user_id) from ttlsa_user_00");

my ($count,$max)[email protected]$ary_ref;

print "number: $count; max: $max\n";

##輸出內容:

[

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

]

number: 10; max: 10

8.selectrow_hashref($statement) 返回一行資料的欄位名與欄位值的雜湊引用

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd",{ PrintError=>0, RaiseError=>1 }) or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 10";

$hash_ary=$dbh->selectrow_hashref($sql);

dump($hash_ary);

$hash_ary=$dbh->selectrow_hashref("select count(*) as count ,max(user_id) as max from ttlsa_user_00");

print "number: $$hash_ary{'count'}; max: $$hash_ary{'max'}\n";

[/codesyntax]

##輸出內容:

{

email => "auto-gre-1\@ttlsa.com",

passwd => "Y9MoErtE+iZG5PkYHMJobhij58E",

user_id => 1,

user_name => "auto-gre-1",

}

number: 10; max: 10

9.selectall_arrayref($statement) 取出所有行並返回包含所有欄位值的引用陣列

#!/usr/bin/perl

use DBI;

use Data::Dump qw(dump);

$driver="DBI:mysql";

$host="localhost:3306";

$dbname="test";

$user="root";

$passwd="123456";

$dbh=DBI->connect("$driver:$dbname:$host","$user","$passwd",{ PrintError=>0, RaiseError=>1 }) or die DBI->errstr;

$sql="select * from ttlsa_user_00 limit 2";

$array_ary=$dbh->selectall_arrayref($sql);

dump($array_ary);

print "------------------------------\n";

foreach $row (@$array_ary){

dump($row);

print "------------------------------\n";

foreach $element (@$row) {

print "'$element',";

}

print "\n";

}

##輸出內容:

[

[

1,

"auto-gre-1\@ttlsa.com",

"Y9MoErtE+iZG5PkYHMJobhij58E",

"auto-gre-1",

],

[

2,

"auto-gre-2\@ttlsa.com",

"UqHxPoLmKY7ClyCZaXPdHepjUOo",

"auto-gre-2",

],

]

------------------------------

[

1,

相關推薦

perl--DBI 模組提取資料fetch方法彙總

perl下十種fetch資料的區別以及返回資料結構: 1.fetchrow_array 提取下一行資料並將欄位儲存在陣列中返回: [codesyntax lang="perl"] #!/usr/bin/perl use DBI; use Data::Dump

Python3常用資料結構及方法介紹——字串

三.字串 特點:不可更改 1.基本操作(同其他序列) ①索引 >>> 'python'[2] 't' ②分片 >>> 'beauty'[0:2] 'be' >>> 'beauty'[::2] 'bat' ③相加/相乘

Unity 的滑鼠事件方法彙總物體,UGUI

本文將從遊戲物體(Gameobject),和UGUI,講解Unity—PC端開發中,滑鼠事件的常見功能實現 本文將幫你解決Unity中如下或者類似的事件響應問題: 遊戲物體篇 點選遊戲物體,物體消失; 滑鼠懸停在遊戲物體上,物體旋轉; 移入遊戲物體,遊戲物體變

python資料型別——元組tuple

列表:打了激素的陣列 元組:帶了緊箍咒的列表;不可變資料型別,沒有增刪改查;可以儲存任意資料型別 元組的建立 In [1]: t = (1,1.2,True,'westos') In [2

計算機視覺的變分方法-擴散Diffusion

最近在看一個計算機視覺中的變分方法系列的視訊,是德國慕尼黑工大出的,講課老師是LSD-SLAM的作者Daniel Cremers,老師講得很清楚,看了還是很有收穫的。我已經變成Cremers大神的腦殘粉了,有興趣看視訊的戳這裡Variational Methods for Computer Visi

市場研究資料分析知識整理 -聯合分析

聯合分析 聯合分析所要解決的是,在研究的產品或服務中,具有哪些特徵的產品最能得到消費者的歡迎。一件產品通常擁有許多特徵如價格、顏色、款式以及產品的特有功能等,那麼在這些特性之中,每個特性對消費者的重要程度如何?在同樣的(機會)成本下,產品具有哪些特性最能

linux核心分析--核心資料結構之佇列

核心中的佇列是以位元組形式儲存資料的,所以獲取資料的時候,需要知道資料的大小。 如果從佇列中取得資料時指定的大小不對的話,取得資料會不完整或過大。 核心中關於佇列定義的標頭檔案位於:<linux/kfifo.h> include/linux/kfifo.h 標頭檔案中定義的函

MongoDB匯入資料命令的使用mongoimport

MongoDB中匯入資料命令的使用(mongoimport) 製作人:全心全意 語法: mongoimport <options> <file> 介紹: 該命令可以將CSV,TSV或JSON檔案資料匯入MongoDB, 如果沒有提供檔案,則mongoimport從std

iOSOC呼叫js方法簡述

從iOS7開始 蘋果公佈了JavaScriptCore.framework 它使得JS與OC的互動更加方便了。 下面我們就簡單瞭解一下這個框架 首先我匯入framework 方法如下: 點選Linked Frameworks and Libraries 的

win環境下把MySql資料匯入到Elasticsearch

環境問題參考我上文: 環境問題已經好了,接下來,我們講實戰。 該壓縮包幫助mysql與其他平臺連線。看到很多資源都要積分,不能選0分,所以選1分了 在bin目錄下建立jdbc.config 根據需求改連線,賬號,密碼,名字,埠等等。 input {

iOS之獲取UITableViewCellUITextField的值方法總結

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteg

引數化之從資料庫提取資料元件詳細解析

1)“配置元件”->“JDBC Connection Configuration” 精簡版: Variable Name:連線池名稱 Database URL:jdbc:mysql://host:port/db(jdbc:mysql://ip地址:埠號/資料庫

Oracle採購模組的多組織訪問控制MOAC

d. 一個新欄位名為‘經營單位’包含在標準SRS介面。對於每一個採購報表或者併發請求使用者,將會被要求去選擇一個經營單位作為引數。這和‘接收報表’(不需要提供‘經營單位’)是不一樣的情況。 下列是採購和接收報表列表(需要‘經營單位’作為必須輸入項)。輸出將會有經營單位名稱的列印。 Buyer Listing

在 Windows Server Container 運行 Azure Storage Emulator:運行在容器

們的 x86 有關 doc fault ps1 count1 ora isn   上一節中,我們已經準備好了 SQL Server,那麽接下來,我們要把 ASE 放到容器裏了。   首先,新建 Start.ps1,內容如下: 1 param( 2 [Param

圖像的傅立葉變換

輸入 rac 公式 矩陣 中心 line 傅立葉變換 sqrt 分享圖片 在之前的文章中,我們介紹了傅立葉變換的本質和很多基本性質,現在,該聊聊代碼實現的問題了。 為了方便起見,本文采用的編程語言是 Python3,矩陣處理用 numpy,圖像處理則使用 OpenCV3。

linux 核心模組程式設計之編譯多個原始檔

編譯擁有多個原始檔的核心模組的方式和編譯一個原始檔的方式差不多,我們先來看下我們需要的檔案都有哪些。 首先是main.c檔案 #include <linux/module.h> #include <linux/init.h> MODULE_LICENSE

依賴注入的兩種常用方式(構造器和Setter)與注入內容(裝配資料)——Spring IOC/DI

本章主要講解一下Spring中依賴注入方式,接上一章依賴注入原理和方式: https://blog.csdn.net/qq_34598667/article/details/83315669 依賴注入常用的兩種方式以及注入的內容(裝配資料) Spring通過DI(依賴注入)實現I

DispNetCaffe自定義層解讀—— GenerateAugmetationParamters

DispNet中Caffe自定義層解讀(三)—— GenerateAugmetationParamters 這一系列博文記錄了博主在學習DispNet過程中遇到的自定義Caffe層的筆記。這一部分是GenerateAugmentationParameters層,其主要功能是: 。

資料Hadoop學習筆記

1.HDFS架構講解 2.NameNode啟動過程 3.YARN架構組建功能詳解 4.MapReduce 程式設計模型 HDFS架構講解 源自谷歌的GFS論文 HDFS: *抑鬱擴充套件的分散式系統 *執行在大量普通的鏈家機器上,提供容錯機制 *為

工廠兄弟之工廠方法模式

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!