my code using SQlite module with perl

1. Common ways which use SQlite

#!/usr/ bin/perl -w
use warnings;
use constant LIB_DIR =>'/home/xialeizhou/perl5/lib/perl5';
use lib LIB_DIR;
use DBI;


#my $dbargs = { AutoCommit => 0, PrintError => 1 };
#my $dbh = DBI->connect( "dbi:SQLite:dbname=./record.db", "", "", $dbargs );
my $dbh = DBI->connect( "dbi:SQLite:dbname=./record.db" );


my $creat_sth  = $dbh->do( "create table idClassString( site_id, class)" );


#my $index_sth = $dbh->do( "create index idx_site_id ON idClassString( site_id )" );
#   $index_sth = $dbh->do( "create index idx_class ON idClassString( class )" );


# insert data
my %hash;
for (0..1000){
		$hash{"site_$_"} = "test_strings_is_$_";
}


while (my ($key, $valule) = each %hash) {
#print $key, $hash{$key}, "\n";
				$dbh->do( qq[insert into idClassString values( "$key","$hash{$key}" )] );
}


#my $insert_sth = $dbh->do( "insert into idClassString values( 12, 'hello world' )" );


## debug messages
#DBI->trace( 1, 'dbtrace.log');
#
#$dbh->{ AutoCommit } = 0;
#
## update data
#my $update_sth = $dbh->do( "update idClassString set class='foo neel' where site_id='12'");
#
## delete data
#my $delete_sth = $dbh->do( "delete from idClassString where site_id='12'" );
#
my $sql = "SELECT * FROM idClassString";
my $dataCenter = $dbh->prepare($sql);
   $dataCenter->execute();


my (@row_array, $out_id, $out_str);
while (@row_array = $dataCenter->fetchrow_array){
#		  ( $out_id, $out_str ) = @row_array;
				print @row_array, "\n";
}
#$dbh->do( "delete from idClassString" );
$dbh->disconnect();


2. Read data from a db file

#!/usr/ bin/perl -w
use warnings;
use constant LIB_DIR =>'/home/xialeizhou/perl5/lib/perl5';
use lib LIB_DIR;
use DBI;

#my $dbargs = { AutoCommit => 0, PrintError => 1 };
my $dbh = DBI->connect( "dbi:SQLite:dbname=./record.db" );

#my $creat_sth  = $dbh->do( "create table idClassString( site_id, class)" );

#my $index_sth = $dbh->do( "create index idx_site_id ON idClassString( site_id )" );
#   $index_sth = $dbh->do( "create index idx_class ON idClassString( class )" );

# insert data
#my %hash;
#for (0..100){
#		$hash{"site_$_"} = "test_strings_is_$_";
#}
##
#while (my ($key, $value) = each %hash) {
###print $key, $hash{$key}, "\n";
#				$dbh->do( qq[insert into idClassString values("$key", "$hash{$key}")] );
#}

#my $insert_sth = $dbh->do( "insert into idClassString values( 12, 'hello world' )" );

## debug messages
#DBI->trace( 1, 'dbtrace.log');
#
#$dbh->{ AutoCommit } = 0;
#
## update data
#my $update_sth = $dbh->do( "update idClassString set class='foo neel' where site_id='12'");
#
## delete data
#my $delete_sth = $dbh->do( "delete from idClassString where site_id='12'" );
#

my $sql = "SELECT * FROM idClassString";
my $dataCenter = $dbh->prepare($sql);
   $dataCenter->execute();

my (@row_array, $out_id, $out_str);
while (@row_array = $dataCenter->fetchrow_array){
				print @row_array;
				print "\n";
}
##$dbh->do( "delete from idClassString" );
$dbh->disconnect();
#
#


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值