There are many problems with your code. See below.

On Wed, Nov 11, 2009 at 12:50 AM, Akash Rao <mrcool...@gmail.com> wrote:
..
> I have a perl script that add numbers 1-1000 into a db.
>
> Here is the code:
> =========================
> use DBI;
>
> my $number;
>
> for ($number=0;$number <= 1000;$number++)
>  {
> my $dbh = DBI->connect("dbi:SQLite:dbname=/opt/test.db","","",
>                      {RaiseError => 1, AutoCommit => 0});
>
>   $dbh -> do("INSERT INTO test1 VALUES('$number')");
>

You don't want to connect to the database on every iteration of the
loop. Kinda defeats the purpose of a db connection.

>   $dbh -> commit();
>   $dbh->disconnect();
>  };
> ============================
>
..

Use the following, more perlish, code --


use DBI;

# The db connection has been moved out of the loop, and is now
# created only once and reused.
my $dbh = DBI->connect("dbi:SQLite:dbname=/opt/test.db","","",
                     {RaiseError => 1, AutoCommit => 0});

# Prepare a statement with bind vars and reuse it in the loop
my $sth = $dbh->prepare("INSERT INTO test1 VALUES (?)");

for my $number (0 .. 1000) {
     $sth->execute($number);
};

# Commit and disconnect outside the loop
$dbh -> commit();
$dbh->disconnect();


-- 
Puneet Kishor http://www.punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
-----------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
=======================================================================
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to