Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion Video
internet.com

» HOME
» NEWS
» VIDEO
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner


















Microsoft Issues Warnings on IE 8 Beta 2

Comcast Pushes Ahead on Broadband Throttling

The Microsoft-Novell Deal and Trust in Princes

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers



Senior Software Developer
Professional Technical Resources
US-CA-Santa Clara

Justtechjobs.com Post A Job | Post A Resume
MS Access
September 13, 1999
Dabbling in Live Databases: MSAccess
By Aaron Weiss

A Simple Query

"Please list all client names and their e-mail addresses" -- that is the first request we aim to please, premised on the sample clients database created earlier in this article. We begin our Perl program by including the DBI module:

#Windows-based Perl/DBI/MS Access example
	use DBI;

Next, we use the DBI connect method to open a connection to the MS Access database:

#open connection to Access database
	$dbh = DBI->connect('dbi:ODBC:Clients');

The variable $dbh represents the database handle (of course you can use any variable name you wish). We will soon need to access the database handle to make more specific requests of the database. The parameter passed to connect specifies which database module to use (ODBC) and the registered name of the database to open (Clients). If our database was password protected then we'd need to supply those additional parameters as well:

$dbh = DBI->connect('dbi:ODBC:Clients',username,password);

The database is now standing at attention and waiting to serve. Next we construct the SQL statement, tell DBI to "prepare" the statement for execution (sort of like compiling the statement), and finally to go ahead an execute the statement.

#construct SQL statement
$sqlstatement="SELECT ClientName,ClientEmail FROM billing";

#prepare and execute SQL statement
$sth = $dbh->prepare($sqlstatement);
$sth->execute || 
      die "Could not execute SQL statement, maybe invalid?";

The SQL statement string is assigned to the variable $sqlstatement. Another variable, $sth or statement handle, calls a method of the database handle, to prepare the statement. Exactly what preparation consists of is not our concern -- that knowledge lives within the DBD:ODBC module, or whatever database module you are using. Finally, the statement handle is instructed to execute the SQL statement.

At this point, the database now takes the SQL statement into its own hands. It will parse the statement using its own knowledge of SQL and, if valid, return results from the database. If the database cannot execute the SQL statement -- perhaps the syntax is wrong or there is a system problem (lack of memory) -- the execute method will return a false value and, in our example, the die clause will dump an error message to the screen.

Results don't grow on trees, or fall off them either. The database may be ready with its answer, but we on the Perl side must actively stick out our arms and retrieve these results. We recommend you retrieve these results with DBI's fetchrow_array method. When we call this method the database will return the first row of results, where each returned field is an element in the array.

@row=$sth->fetchrow_array;

Because our query requested two returned fields -- ClientName and ClientEmail -- the array @row should contain two elements, $row[0] and $row[1]. A cute little Perl trick as well as a more efficient way to receive these fields would be:

($ClientName,$ClientEmail)=$sth->fetchrow_array;

Remember that each call of fetchrow_array yields the next row of results, until there are no more results and the call yields a value of undef. Thus, we can use a while loop to retrieve and, for example, print out each row of results.

#output database results
	while (@row=$sth->fetchrow_array)
	 { print "@row\n" }

Putting together the pieces, we have a small little Perl script which executes a single specific SQL statement and outputs the results:

#Windows-based Perl/DBI/MS Access example
 use DBI;

 #open connection to Access database
 $dbh = DBI->connect('dbi:ODBC:Clients');

 #prepare and execute SQL statement
 $sqlstatement="SELECT ClientName,ClientEmail FROM billing";
 $sth = $dbh->prepare($sqlstatement);
 $sth->execute || 
       die "Could not execute SQL statement ... maybe invalid?";

 #output database results
 while (@row=$sth->fetchrow_array)
  { print "@row\n" }

Hint: if Microsoft Access is open and running while you try to execute these Perl scripts you may run into resource conflicts. If so, simply shutdown Microsoft Access before using Perl.

Go to page: Prev  1  2  3  4  5  6  7  Next  

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS Access Archives







Latest Forum Threads
MS Access Forum
Topic By Replies Updated
Concatenate into one field ahoffman 6 September 7th, 08:43 PM
Replication and multiple users ALTA Tutor 1 September 4th, 01:53 AM
How to create user login form (MS Access 2000 - 2003) meLady 33 August 27th, 11:40 AM
relink to relative path TXT data files tobynegus 4 August 21st, 06:03 AM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES