Perl實現檔案及資料庫訪問

2021-08-27 16:47:24 字數 1400 閱讀 3856

寫了乙個perl指令碼,使用了迴圈、控制台輸出、訪問檔案以及訪問資料庫等特性,實現從文字檔案中,讀入多條已格式化的記錄,按規定的分隔符解析,並插入到mysql資料庫中。

[b]一、預備條件:[/b]

0、安裝perl直譯器(廢話);

1、安裝有資料庫(本人使用的是mysql);

2、perl已安裝dbi模組。(注意:使用訪問資料庫功能時,須先安裝相應的模組,即dbi模組,安裝方法如鏈結所示:[url]

[b]二、準備工作:[/b]

[quote]20120507|192.168.10.1|50

20131222|172.18.33.55|24

20130706|202.116.60.5|61[/quote]

create database testdb;

use testdb;

create table test(createtime datetime not null default '19000101', ipaddr varchar(20) not null default '', counter int not null default 0);

[b]三、perl指令碼**:[/b]

#!/usr/bin/perl

use dbi;

#use dbd::mysql;

my $maillog = "./test.log";

my ($datasourename) = "dbi:mysql:test:localhost"; #data source name

my ($username) = "root"; # user name

my ($password) = ""; # password

my ($dbh, $sth); #database and statement handles

my $dbh = dbi->connect($datasourename, $username, $password, );

open(ff, $maillog) || die "error:$!";

while()

close(ff);

$dbh->commit();

$dbh->disconnect();

sub insert_test();

if ($@)

}

[b]四、附件介紹:[/b]

0、test.pl是perl指令碼,test.log是整理好準備要插入的資料檔案,test.sql是在mysql中建表的指令碼。

1、先執行test.sql,在mysql中建表。

2、把test.log與test.pl放在同乙個目錄下。

3、執行test.pl可插入資料。

訪問資料庫 訪問資料庫

程式執行的時候,資料都是在記憶體中的。當程式終止的時候,通常都需要將資料儲存到磁碟上,無論是儲存到本地磁碟,還是通過網路儲存到伺服器上,最終都會將資料寫入磁碟檔案。而如何定義資料的儲存格式就是乙個大問題。如果我們自己來定義儲存格式,比如儲存乙個班級所有學生的成績單 名字成績 michael99 bo...

檔案操作 訪問資料庫

print 建立資料庫test import pymysql 建立資料庫並開啟 db pymysql.connect host localhost port 3306 user root passwd 12345 db test charset utf8 cursor db.cursor 使用 ex...

訪問資料庫 資料庫並行訪問

作者姓名 neo chen 陳景峰 暱稱 netkiller 呼號 bg7nyt 手機 86 13113668890 多維度架構 知乎 www.zhihu.com 這裡主要講述有關開發中遇到的資料庫並行問題 防止並行顯示背景 我們有乙個order訂單表,工作流如下 建立訂單 訂單分配 訂單審核 批准...