My code using SQLite module with Perl

Source: Internet
Author: User

1. Common Ways which use SQLite

#!/usr/ bin/perl -wuse 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 datamy %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 -wuse 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();##
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.