|
/*****
PERL : perl-test.pl
******/
#!/usr/bin/perl -w
#
use DBI; # Load the DBI module
my $dbh = DBI->connect( "dbi:mysql:test","root", "", {
PrintError => 0,
RaiseError => 0
} ) or die "Can't connect to the database: $DBI::errstr\n";
### Prepare a SQL statement for execution
my $sth = $dbh->prepare( "SELECT * FROM user_name" )
or die "Can't prepare SQL statement: $DBI::errstr\n";
### Execute the statement in the database
$sth->execute
or die "Can't execute SQL statement: $DBI::errstr\n";
### Retrieve the returned rows of data
my @row;
#my $row_str;
while ( @row = $sth->fetchrow_array( ) ) {
#$row_str=join(",",@row);
print "Row: @row\n";
}
warn "Data fetching terminated early by error: $DBI::errstr\n"
if $DBI::err;
### Disconnect from the database
$dbh->disconnect
or warn "Error disconnecting: $DBI::errstr\n";
exit;
/**********
C : test.c
***********/
#define MAXLEN 50
#include <stdio.h>
#include "mysql.h"
#include "mysqld_error.h"
#include <error.h>
#include <sys/errno.h>
int main(int argc ,char **argv)
{
MYSQL mysql;
MYSQL_RES *mysql_result;
MYSQL_FIELD *field;
MYSQL_ROW row;
int count = 0;
int i= 0;
char sQueue[MAXLEN + 1];
memset(sQueue,0,sizeof sQueue);
strcpy(sQueue,"select * from user_name");
mysql_init(&mysql);
if (!mysql_real_connect (&mysql,"localhost","root","","test",0,NULL,0))
{
printf("Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
exit(-1);
}
if(mysql_real_query(&mysql, sQueue, strlen(sQueue)))
{
printf("Failed to queue to database: Error: %s\n",
mysql_error(&mysql));
exit(-1);
}
mysql_result=mysql_store_result(&mysql);
count=mysql_field_count(&mysql);
while((row=mysql_fetch_row(mysql_result)))
{
for(;i<count;i++)
printf(" %s\t", row);
i = 0;
printf("\n");
}
mysql_close(&mysql);
exit (0);
}
/*****
Makefile FOR C test.c:
*****/
INCL=/usr/include/mysql
CC=gcc
CFLAGS=-DDEBUG -g -c
#TARGET=../
%.o:%.c
$(CC) $(CFLAGS) -I$(INCL) $<
all:test
test:test.o
$(CC) -o $@ $< -lmysqlclient -lm -lpthread -lcurses
rm $@.o
@echo
两个都是简单的显示mysql数据库TEST中表user_name的数据.
[root@localhost perl]# time perl perl-test.pl
Row: wwshao 0
Row: shao 1
Row: wwshao 0
Row: shao 1
Row: shao 0
Row: shaoww 1
Row: shaoww 0
Row: wwshao 1
Row: wwshao 0
real 0m0.537s
user 0m0.350s
sys 0m0.180s
[root@localhost mysql-examp]# time ./test
wwshao 0
shao 1
wwshao 0
shao 1
shao 0
shaoww 1
shaoww 0
wwshao 1
wwshao 0
real 0m0.091s
user 0m0.020s
sys 0m0.050s
以前只知道PERL比C慢,没想到差这么多....
各位兄弟有没有好的建议,是不是DBI模块的原因.我用的是最新的...
先谢过! |
|