title: Inserting into a PostgreSQL table using Perl
body: <p>I am trying to auto-populate a table in a PostgreSQL database. Unfortunately I am not getting the insert statement to work.</p>
  
  <p>The first row is an ID which is an integer, and the second column is of numeric data type. I am using a Perl script to achieve this.</p>
  
  <p>I am using the <code>rand</code> function which creates random numbers between 0 and 1. The first argument is the variable <code>$id</code>; the second argument is the variable <code>$count</code>.</p>
  
  <p>Based on the count, the table will populate the ID followed by a random number.</p>
  
  <p>I am using PostgreSQL 9.2.4.</p>
  
  <pre><code>-ID | Random_Number
  -1 |  0.01
  -1 |  0.03
  -2 |  0.0566
  
  #!/usr/bin/perl
  
  use DBI;
  
  my $id = &lt;&gt;;
  my $count = &lt;&gt;;
  
  my $db_host = 'localhost';
  my $db_user = 'postgres';
  my $db_pass = '12345';
  my $db_name = 'postgres';
  
  my $db = "dbi:Pg:dbname=${db_name};host=${db_host}";
  
  $dbh = DBI-&gt;connect($db, $db_user, $db_pass, { RaiseError =&gt; 1, AutoCommit =&gt; 0 })
          || die "Error connecting to the database: $DBI::errstr\n";
  
  for (my $loop = 0; $loop &lt; $count; $loop++) {
      my $random_number = rand();
      my $loop++;
      my $query = "insert into random_table values($loop,$random_number)";
  }
  </code></pre>