Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1144384
  • 博文数量: 53
  • 博客积分: 10025
  • 博客等级: 上将
  • 技术积分: 1640
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-15 17:05
文章分类

全部博文(53)

文章存档

2011年(1)

2010年(3)

2009年(25)

2008年(24)

我的朋友

分类:

2008-06-06 15:07:41

    好久好久没来blog上写文章了,公司最近真很忙。
    稍微安顿下来了,客户的一些通讯资料和我的破手机成了矛盾,只能存储200个号码,而且在SIM卡中,慢死不说,装不下呀,无奈,花了个下午,弄个能支持任意字段的通讯录出来玩玩,其实这种款式的通讯录,命令行下的,以前也设想过了呢:)

    程序叫做 dcontacts,是 Perl写的,使用 MySQL数据库,要调用 Perl的 DBI连接数据库,如果要使用,需要自己准备安装好这些。

    看下程序的使用帮助

dorainm@thinkpad/~ $ dcontacts --help
dcontacts 0.2
Copyright (C) 2008 dorainm <dorainm@gmail.com>
Usage :
Help Info. : dcontacts --help
Re/Install : dcontacts INSTALL
Backup Data : dcontacts BACKUP
Create Name : dcontacts newname
Update Name : dcontacts oldname newname
Remove Name : dcontacts oldname NULL
Create Item : dcontacts name kind newitem
Update Item : dcontacts name kind olditem newitem
Remove Item : dcontacts name kind olditem NULL
AND Search : dcontacts AND keyword,...
OR Search : dcontacts OR keyword,...
dorainm@thinkpad/~ $


    嗯,这个是 --help的屏幕,打开程序源码,修改%conf里面,数据库配置,dbhost表示数据库服务器、dbbase数据库名、dbuser用户名、dbpswd连接密码,现在让程序安装数据库表吧。

dorainm@thinkpad/~ $ dcontacts INSTALL
INSTALL operation will CLEAR all the DATA if it is
Are you SURE (yes/NO) ? yes
Done
dorainm@thinkpad/~ $


    有行提醒,安装数据库表,会导致以前存在的表的数据丢失,因为程序是 DROP旧表后,CREATE新的表结构的。
    现在来添加资料,看示范和命令吧

//新建一个姓名
dorainm@thinkpad/~ $ dcontacts dorainm

//添加一个email,用户可以自定义类型
dorainm@thinkpad/~ $ dcontacts dorainm email dorainm@gmail.com

//当然,相同类型可以存多个项
//比如再存一个email,要dorainm@funtin.com
dorainm@thinkpad/~ $ dcontacts dorainm email dorainm@funtin.com

//添加一个相同的电子邮箱为dorainm,会被提示错误
dorainm@thinkpad/~ $ dcontacts dorainm email dorainm@gmail.com
it already has the item as 'dorainm@gmail.com'

//添加一个blog地址
dorainm@thinkpad/~ $ dcontacts dorainm blog dorainm.cublog.cn

//直接添加项,如果发现没有这个名字,程序会自动添加
dorainm@thinkpad/~ $ dcontacts edison email edison@funtin.com

//直接添加项,如果发现没有这个类型,程序也会自动添加
dorainm@thinkpad/~ $ dcontacts david mobilephone 12345678901


dorainm@thinkpad/~ $ dcontacts edison mobilephone 98765432109

//添加姓名
dorainm@thinkpad/~ $ dcontacts fentin

//不小心写错名字了,直接修改
dorainm@thinkpad/~ $ dcontacts fentin funtin

//删除名字的话,就把名字重命名为NULL
dorainm@thinkpad/~ $ dcontacts funtin NULL

//添加一个已经存在的名字,就会显示改名字的详细资料
dorainm@thinkpad/~ $ dcontacts dorainm
-[ dorainm ]-------------------------------------------------(2)--
[2008-06-06 14:42:37] email : dorainm@gmail.com
[2008-06-06 14:42:42] email : dorainm@funtin.com
[2008-06-06 14:43:00] blog : dorainm.cublog.cn
-----------------------------------------------------------------

//添加一个homepage
dorainm@thinkpad/~ $ dcontacts dorainm homepage www.dorainm.org
dorainm@thinkpad/~ $ dcontacts dorainm
-[ dorainm ]-------------------------------------------------(3)--
[2008-06-06 14:42:37] email : dorainm@gmail.com
[2008-06-06 14:42:42] email : dorainm@funtin.com
[2008-06-06 14:43:00] blog : dorainm.cublog.cn
[2008-06-06 14:45:26] homepage : www.dorainm.org
-----------------------------------------------------------------

//如果要修改某个项的值,也是这么样子直接修改
dorainm@thinkpad/~ $ dcontacts dorainm homepage www.dorainm.org dorainm.org
//看,修改好了
dorainm@thinkpad/~ $ dcontacts dorainm
-[ dorainm ]-------------------------------------------------(3)--
[2008-06-06 14:42:37] email : dorainm@gmail.com
[2008-06-06 14:42:42] email : dorainm@funtin.com
[2008-06-06 14:43:00] blog : dorainm.cublog.cn
[2008-06-06 14:45:33] homepage : dorainm.org
-----------------------------------------------------------------

//删除的话,也直接重命名为保留字NULL
dorainm@thinkpad/~ $ dcontacts dorainm homepage www.dorainm.org NULL
there are no such item as ''
dorainm@thinkpad/~ $ dcontacts dorainm homepage dorainm.org NULL

//AND 和 OR 是搜索用的,后面加关键字
dorainm@thinkpad/~ $ dcontacts AND son
-[ edison ]------------------------------------------------------
[2008-06-06 14:43:18] email : edison@funtin.com
[2008-06-06 14:44:04] mobilephone : 98765432109

//AND 搜索即存在 cublog又存在 dorainm的姓名、类型、项
dorainm@thinkpad/~ $ dcontacts AND cublog dorainm
-[ dorainm ]------------------------------------------------------
[2008-06-06 14:43:00] blog : dorainm.cublog.cn

//OR 搜索存在 do或者 da的姓名、类型、项
dorainm@thinkpad/~ $ dcontacts OR do da
-[ dorainm ]------------------------------------------------------
[2008-06-06 14:42:37] email : dorainm@gmail.com
[2008-06-06 14:42:42] email : dorainm@funtin.com
[2008-06-06 14:43:00] blog : dorainm.cublog.cn
-[ david ]------------------------------------------------------
[2008-06-06 14:43:51] mobilephone : 12345678901

dorainm@thinkpad/~ $


    呃,就这么用,AND 和 OR还是好用的,而且在姓名、类型、项值里面,全部LIKE '%$keyword%'来搜索,比较有感觉,嘻~
    只是条件搜索目前还不能组合使用,不太会用参数表达复式的组合,用波兰表达式读取类似四则混合运算的参数?!

    程序还有一个备份功能,备份数据库,程序会调用mysqldump命令,在 ~/.dcontact/backup目录下,以当前的日期、时间为文件名,备份一个 .sql的数据库文件,供用户恢复时候使用。

dorainm@thinkpad/~ $ dcontacts BACKUP
dorainm@thinkpad/~ $ ls -l ~/.dcontacts/backup
total 12
-rw-r--r-- 1 dorainm master 4965 Jun 6 14:59 2008-6-14:59:42.sql
dorainm@thinkpad/~ $


   
    嗯,就介绍到此,半天弄的代码,难看,有意向的拿着用拿着改吧:)

#!/usr/bin/perl


#use strict;

use DBI;

my %conf = (
    'dbhost' => "mysql host",
    'dbbase' => "mysql database",
    'dbuser' => "mysql username",
    'dbpswd' => "mysql password",
    'dbprefix' => "contact_",
    'name' => "dcontacts",
    'version' => "0.2",
    'author' => "dorainm",
    'email' => "dorainm\@gmail.com",
);

my $dargv = @ARGV;

if( $ARGV[0] eq "--help" ) {
    &display_usage();
}
## Re/Install the database

elsif( $ARGV[0] eq "INSTALL" ) {
    printf( "INSTALL operation will CLEAR all the DATA if it is EXIST!\n" );
    printf( "Are you SURE (yes/NO) ? " );
    $answer = <STDIN>;
    chomp $answer;
    if( $answer eq "yes" ) {
    &dtable_reinstall();
    printf "Done\n";
    } else {
    printf "Cancel\n";
    }
}
## backup the databases

elsif( $ARGV[0] eq "BACKUP" ) {
    &dcontacts_backup();
}
## search AND

elsif( $ARGV[0] eq "AND" ) {
    local ($rows,$cols,@retres) = &dcontacts_search( "AND" );
    local $prename;

    for( $i=0; $i<$rows; $i++ ) {
    if( $prevname ne $retres[$i][0] ) {
     $prevname = $retres[$i][0];
     printf "-[ $prevname ]------------------------------------------------------\n";
    }
    printf "[%s] %12s : %s\n", $retres[$i][3], $retres[$i][1], $retres[$i][2];
    }
}
## search OR

elsif( $ARGV[0] eq "OR" ) {
    local ($rows,$cols,@retres) = &dcontacts_search( "OR" );
    local $prename;

    for( $i=0; $i<$rows; $i++ ) {
    if( $prevname ne $retres[$i][0] ) {
     $prevname = $retres[$i][0];
     printf "-[ $prevname ]------------------------------------------------------\n";
    }
    printf "[%s] %12s : %s\n", $retres[$i][3], $retres[$i][1], $retres[$i][2];
    }
}
## add a new name

elsif( $dargv == 1 )
{
    local $newname = $ARGV[0];
    if( &dname_exist( $newname ) ) {
    ## display the information about the name

    #printf "error: the name '$newname' has been already exist\n";

    local $rows, $cols, @retres;
    ($rows,$cols,@retres) = &dcontacts_query( $newname );

    printf "-[ $newname ]-------------------------------------------------($rows)--\n";
    for( $i=0; $i<$rows; $i++ ) {
     printf "[%s] %12s : %s\n", $retres[$i][2], $retres[$i][0], $retres[$i][1];
    }
    printf "-----------------------------------------------------------------\n";
    } else {
    &dname_create( $newname );
    }
}
## update the name

elsif( $dargv == 2 )
{
    local $oldname = $ARGV[0];
    local $newname = $ARGV[1];
    if( ! &dname_exist( $oldname ) ) {
    printf "error: the old name '$newname' has NOT been exist\n";
    } elsif( $newname eq "NULL" ) {
    ## NULL : delete the name

    &dname_remove( $oldname );
    } elsif( &dname_exist( $newname ) ) {
    printf "error: the new name '$newname' has been already exist\n";
    } else {
    &dname_update( $oldname, $newname )
    }
}
## add a new item

elsif( $dargv == 3 )
{
    local $dname = $ARGV[0];
    local $dkind = $ARGV[1];
    local $ditem = $ARGV[2];

    if( ! &dname_exist( $dname ) ) {
    &dname_create( $dname );
    }
    if( ! &dkind_exist( $dkind ) ) {
    &dkind_create( $dkind );
    }
    if( &ditem_exist( $dname, $dkind, $ditem ) ) {
    printf "it already has the item as '$ditem'\n";
    } else {
    &ditem_create( $dname, $dkind, $ditem );
    }

}
elsif( $dargv == 4 )
{
    local $dname = $ARGV[0];
    local $dkind = $ARGV[1];
    local $dolditem = $ARGV[2];
    local $dnewitem = $ARGV[3];

    if( ! &dname_exist( $dname ) ) {
    printf "nobody has the name '$dname'\n";
    } elsif( ! &dkind_exist( $dkind ) ) {
    printf "there are no kind named '$dname' of '$dname'\n";
    } elsif( ! &ditem_exist( $dname, $dkind, $dolditem ) ) {
    printf "there are no such item as '$dolditem'\n";
    } elsif( $dnewitem eq "NULL" ) {
    ## remove item

    &ditem_remove( $dname, $dkind, $dolditem );
    } elsif( &ditem_exist( $dname, $dkind, $dnewitem ) ) {
    printf "it already has the item as '$dnewitem'\n";
    } else {
    &ditem_update( $dname, $dkind, $dolditem, $dnewitem );
    }
}
else {
    &display_usage();
}

exit 0;





sub dname_exist()
{
    return ( &dmysql_value( $conf{'dbprefix'} . "names", "n_name", "`n_name`='$_[0]'" ) );
}

sub dname_create()
{
    local $sql = "INSERT INTO `$conf{'dbprefix'}names` VALUE ('','$_[0]');";
    &dmysql_execute( $sql );
}

## dname_update old_name, new_name

sub dname_update()
{
    local $sql = "UPDATE `$conf{'dbprefix'}names` SET `n_name`='$_[1]' WHERE `n_name`='$_[0]';";
    &dmysql_execute( $sql );
}

sub dname_remove()
{
    local $sql = "DELETE FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]';";
    &dmysql_execute( $sql );
}

sub dcontacts_backup()
{
    if( $conf{'dbpswd'} ) {
    $cmd = "mysqldump -h$conf{'dbhost'} -u$conf{'dbuser'} -p$conf{'dbpswd'} --databases $conf{'dbbase'} > ~/.dcontacts/backup/" . &ddatetime . ".sql";
    } else {
    $cmd = "mysqldump -h$conf{'dbhost'} -u$conf{'dbuser'} --databases $conf{'dbbase'} > ~/.dcontacts/backup/" . &ddatetime . ".sql";
    }
    `mkdir -p ~/.dcontacts/backup`;
    `$cmd`;
}

sub dcontacts_query()
{
    local $rows, $cols, @retres;
#    local $sql = "SELECT `$conf{'dbprefix'}names`.`n_name`,`$conf{'dbprefix'}kinds`.`k_caption`,`$conf{'dbprefix'}items`.`i_content` FROM `$conf{'dbprefix'}names`,`$conf{'dbprefix'}kinds`,`$conf{'dbprefix'}items` WHERE `$conf{'dbprefix'}items`.`n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]') AND `$conf{'dbprefix'}names`.`n_id`=`$conf{'dbprefix'}items`.`n_id` AND `$conf{'dbprefix'}kinds`.`k_id`=`$conf{'dbprefix'}items`.`k_id`;";

    local $sql = "SELECT `$conf{'dbprefix'}kinds`.`k_caption`,`$conf{'dbprefix'}items`.`i_content`,`$conf{'dbprefix'}items`.`i_date` FROM `$conf{'dbprefix'}names`,`$conf{'dbprefix'}kinds`,`$conf{'dbprefix'}items` WHERE `$conf{'dbprefix'}items`.`n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]') AND `$conf{'dbprefix'}names`.`n_id`=`$conf{'dbprefix'}items`.`n_id` AND `$conf{'dbprefix'}kinds`.`k_id`=`$conf{'dbprefix'}items`.`k_id`;";

    ( $rows, $cols, @retres ) = &dmysql_query( $sql );
    return ($rows,$cols,@retres);
}

# dcontacts_search $type

sub dcontacts_search()
{
    local $rows, $cols, @retres;
    local $sql = "SELECT `$conf{'dbprefix'}names`.`n_name`,`$conf{'dbprefix'}kinds`.`k_caption`,`$conf{'dbprefix'}items`.`i_content`,`$conf{'dbprefix'}items`.`i_date` FROM `$conf{'dbprefix'}names`,`$conf{'dbprefix'}kinds`,`$conf{'dbprefix'}items` WHERE `$conf{'dbprefix'}names`.`n_id`=`$conf{'dbprefix'}items`.`n_id` AND `$conf{'dbprefix'}kinds`.`k_id`=`$conf{'dbprefix'}items`.`k_id`";

    
    if( $dargv > 1 ) {
    if( $_[0] eq "AND" ) {
     $sql .= " AND ( 1";
     for( $i=1; $i<@ARGV; $i++ ) {
        local $keyword = $ARGV[$i];
        $sql .= " AND ( `$conf{'dbprefix'}items`.`n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name` LIKE '%$keyword%') OR `$conf{'dbprefix'}items`.`k_id`=(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption` LIKE '%$keyword%') OR `$conf{'dbprefix'}items`.`i_content` LIKE '%$keyword%' )";
     }
     $sql .= " );";
    } else {
     $sql .= " AND ( 0";
     for( $i=1; $i<@ARGV; $i++ ) {
        local $keyword = $ARGV[$i];
        $sql .= " OR ( `$conf{'dbprefix'}items`.`n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name` LIKE '%$keyword%') OR `$conf{'dbprefix'}items`.`k_id`=(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption` LIKE '%$keyword%') OR `$conf{'dbprefix'}items`.`i_content` LIKE '%$keyword%' )";
     }
     $sql .= " );";

    }
    }

    ( $rows, $cols, @retres ) = &dmysql_query( $sql );
    return ($rows,$cols,@retres);
}

sub dtable_reinstall()
{
    local $sql;
    &dmysql_execute( "DROP TABLE IF EXISTS `$conf{'dbprefix'}names`;" );
    $sql = "CREATE TABLE `$conf{'dbprefix'}names` (";
    $sql .= "    `n_id` mediumint unsigned NOT NULL auto_increment,";
    $sql .= "    `n_name` varchar(20) NOT NULL,";
    $sql .= "    PRIMARY KEY (`n_id`)";
    $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    &dmysql_execute( $sql );

    &dmysql_execute( "DROP TABLE IF EXISTS `$conf{'dbprefix'}kinds`;\n" );
    $sql = "CREATE TABLE `$conf{'dbprefix'}kinds` (\n";
    $sql .= "    `k_id` smallint unsigned NOT NULL auto_increment,\n";
    $sql .= "    `k_caption` varchar(20) NOT NULL,\n";
    $sql .= "    PRIMARY KEY (`k_id`)\n";
    $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8;\n";
    &dmysql_execute( $sql );

    &dmysql_execute( "DROP TABLE IF EXISTS `$conf{'dbprefix'}items`;\n" );
    $sql = "CREATE TABLE `$conf{'dbprefix'}items` (\n";
    $sql .= "    `i_id` bigint unsigned NOT NULL auto_increment,\n";
    $sql .= "    `n_id` mediumint unsigned NOT NULL,\n";
    $sql .= "    `k_id` smallint unsigned NOT NULL,\n";
    $sql .= "    `i_content` varchar(100) NOT NULL,\n";
    $sql .= "    `i_date` datetime NOT NULL,\n";
    $sql .= "    PRIMARY KEY (`i_id`)\n";
    $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8;\n";
    &dmysql_execute( $sql );
}

sub dkind_exist()
{
    return ( &dmysql_value( $conf{'dbprefix'} . "kinds", "k_caption", "`k_caption`='$_[0]'" ) );
}

sub dkind_create()
{
    local $sql = "INSERT INTO `$conf{'dbprefix'}kinds` VALUE ('','$_[0]');";
    &dmysql_execute( $sql );
}

## dkind_update old_kind, new_kind

sub dkind_update()
{
    local $sql = "UPDATE `$conf{'dbprefix'}kinds` SET `k_caption`='$_[1]' WHERE `k_caption`='$_[0]';";
    &dmysql_execute( $sql );
}

sub dkind_remove()
{
    local $sql = "DELETE FROM `$conf{'dbprefix'}kinds` WHERE `k_caption`='$_[0]';";
    &dmysql_execute( $sql );
}



sub ditem_exist()
{
    local $rows, $cols, @retres;
    local $sql = "SELECT `i_id` FROM `$conf{'dbprefix'}items` WHERE `n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]') AND `k_id`=(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption`='$_[1]') AND `i_content`='$_[2]';";
    ($rows, $cols, @retres) = &dmysql_query( $sql );
    return $rows;
}

## ditem_create name kind item

sub ditem_create()
{
    local $sql = "INSERT INTO `$conf{'dbprefix'}items` VALUE ('',(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]'),(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption`='$_[1]'),'$_[2]','" . &ddate . "');";
# printf $sql;

    &dmysql_execute( $sql );
}

## ditem_update name kind old_item new_item

sub ditem_update()
{
    local $sql = "UPDATE `$conf{'dbprefix'}items` SET `i_content`='$_[3]',`i_date`='" . &ddate . "' WHERE `n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]') AND `k_id`=(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption`='$_[1]') AND `i_content`='$_[2]';";
    &dmysql_execute( $sql );
}

sub ditem_remove()
{
    local $sql = "DELETE FROM `$conf{'dbprefix'}items` WHERE `n_id`=(SELECT `n_id` FROM `$conf{'dbprefix'}names` WHERE `n_name`='$_[0]') AND `k_id`=(SELECT `k_id` FROM `$conf{'dbprefix'}kinds` WHERE `k_caption`='$_[1]') AND `i_content`='$_[2]';";
    &dmysql_execute( $sql );
}




sub display_usage()
{
    printf "$conf{'name'} $conf{'version'}\n";
    printf "Copyright (C) 2008 $conf{'author'} <$conf{'email'}>\n";
    printf "Usage :\n";
    printf "Help Info. : $conf{'name'} --help\n";
    printf "Re/Install : $conf{'name'} INSTALL\n";
    printf "Backup Data : $conf{'name'} BACKUP\n";
    printf "Create Name : $conf{'name'} newname\n";
    printf "Update Name : $conf{'name'} oldname newname\n";
    printf "Remove Name : $conf{'name'} oldname NULL\n";
    printf "Create Item : $conf{'name'} name kind newitem\n";
    printf "Update Item : $conf{'name'} name kind olditem newitem\n";
    printf "Remove Item : $conf{'name'} name kind olditem NULL\n";
    printf "AND Search : $conf{'name'} AND keyword,...\n";
    printf "OR Search : $conf{'name'} OR keyword,...\n";
    return;
}


sub ddate()
{
    local ($sec,$min,$hour,$mday,$month,$year,$yday,$isdst) = localtime(time());
    $year += 1900;
    $month += 1;
    
    return ("$year-$month-$mday $hour:$min:$sec");
}

sub ddatetime()
{
    local ($sec,$min,$hour,$mday,$month,$year,$yday,$isdst) = localtime(time());
    $year += 1900;
    $month += 1;
    
    return ("$year-$month-$mday_$hour:$min:$sec");
}


sub dmysql_query
{
    local @retres;
    local @ref;
    local $dbh = DBI->connect("DBI:mysql:database=$conf{'dbbase'};host=$conf{'dbhost'}",$conf{'dbuser'},$conf{'dbpswd'});
    local $sth = $dbh->prepare( $_[0] );
    $sth->execute();

    @ref=$sth->fetchrow_array();
    @retres[0] = [@ref];

    local $rows = $sth->rows;
    local $cols = $#ref+1;

    local $i=1;

    if( $rows ) {
    while( @ref=$sth->fetchrow_array() )
    {
     @retres[$i] = [@ref];
     $i++;
    };
    } else {
    $cols = 0;
    @retres = "";
    }

    $sth->finish();
    $dbh->disconnect();

    ($rows, $cols, @retres);
}

sub dmysql_value
{
    local $retres;
    local @ref;
# local $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",$dbuser,$dbcode);

    local $dbh = DBI->connect("DBI:mysql:database=$conf{'dbbase'};host=$conf{'dbhost'}",$conf{'dbuser'},$conf{'dbpswd'});

    local $sql = "SELECT `" . $_[1] . "` FROM `" . $_[0] . "`" . ($_[2]?" WHERE ".$_[2]:"") . ";";

    local $sth = $dbh->prepare( $sql );
    $sth->execute();
    @ref=$sth->fetchrow_array();
    $retres=$ref[0];
    
    $sth->finish();
    $dbh->disconnect();

    $retres;
}


############

# dmysql_query ( sql as string ) as boolean

#

sub dmysql_execute
{
# my $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",$dbuser,$dbcode);

    local $dbh = DBI->connect("DBI:mysql:database=$conf{'dbbase'};host=$conf{'dbhost'}",$conf{'dbuser'},$conf{'dbpswd'});

    my $retval = $dbh->do( $_[0] );
    $dbh->disconnect();

    if(!defined($retval))
    {
    0;
    }
    else
    {
    1;
    };
};

1;


阅读(2139) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~