Inserting rows requires a handle to an open connection. The simplest method is to use a parameterized INSERT statement, meaning that question marks are used as place holders for values. The statement is first prepared, and then executed once per new row. The new row values are supplied as parameters to the execute method.
The following sample program inserts two new customers. Although the row values appear as literal strings, you may want to read the values from a file.
#!/usr/local/bin/perl -w # use DBI; use strict; my $database = "demo"; my $data_src = "DBI:SQLAnywhere:ENG=$database;DBN=$database"; my $uid = "DBA"; my $pwd = "sql"; my $ins_stmt = "INSERT INTO Customers (ID, GivenName, Surname, Street, City, State, Country, PostalCode, Phone, CompanyName) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; my %defaults = ( AutoCommit => 0, # Require explicit commit or rollback. PrintError => 0 ); my $dbh = DBI->connect($data_src, $uid, $pwd, \%defaults) or die "Can't connect to $data_src: $DBI::errstr\n"; &db_insert($ins_stmt, $dbh); $dbh->commit; $dbh->disconnect; exit(0); sub db_insert { my($ins, $dbh) = @_; my($sth) = undef; my @rows = ( "801,Alex,Alt,5 Blue Ave,New York,NY,USA,10012,5185553434,BXM", "802,Zach,Zed,82 Fair St,New York,NY,USA,10033,5185552234,Zap" ); $sth = $dbh->prepare($ins); my $row = undef; foreach $row ( @rows ) { my @values = split(/,/, $row); $sth->execute(@values); } } __END__ |
Discuss this page in DocCommentXchange. Send feedback about this page using email. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |