On Thu, 8 Aug 2002 10:33:27 -0400, [EMAIL PROTECTED] (Rob) wrote:
>Does any one know of an example script that uses the DBI module, connects
>to a PostgreSql database, query's the db, and inserts a record.
>
>I've been using flat text files for any storage that I've needed in the
>past but would like to move on with the learning process. I've read the
>perldoc for DBI but I think a small working example would help me out a
>great deal.
Make sure postgres is running, put in your username, make sure the db
ztest is created first. This just fills up a products table with random
data.
#!/usr/bin/perl -w
###############################################################################
# insert_product : this script will insert records into the
# products table
###############################################################################
use DBI;
my $database_name = "ztest";
my $database = "dbi:Pg:dbname=$database_name";
my $db_user = "zentara";
my $PRODUCT_TB = "products";
# number of record to insert into the table
my $rec_num = 1000;
###############################################################################
my $dbh = DBI->connect($database,$db_user,"") or
die "Can't connect to database\n";
$dbh->do("CREATE TABLE products (
name varchar(50),
price int4,
description varchar(50),
pic_location varchar(50)
)");
my $sth = $dbh->prepare("INSERT INTO $PRODUCT_TB(name,price,description,
pic_location) VALUES (?,?,?,?)");
for ($i = 1; $i <= $rec_num; $i++){
my $name = "Product $i";
my $price = rand 350;
#my $category = rand 3;
my $desc = "Description of product $i";
my $pic = "images/product/product".$i.".jpg";
$sth->execute($name,$price,$desc,$pic);
}
print "Finish inserting $rec_num records into table $PRODUCT_TB\n";
$dbh->disconnect;
exit();
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]