473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Connecting to an MS Access database.

I am working on a class assignment where I have to connect to an MS Access database. The perl script is contained in a separate file *.pl

I went into Control panel/ODBC/ and selected Microsoft Access driver and identified the source name.

I was looking at a previous post here and many things appear to be similar to my requirements.

1. Add dbi inclusion. (pretty straight forward, like an “include” in C or “import” in Java
Expand|Select|Wrap|Line Numbers
  1. use DBI;
  2. $::db_host = 'localhost';
  3. $::db_name = 'KTC.mdb';
  4. $::db_user = 'root';
  5.  
My database is titled KTC.mdb and is located in the apache2\database directory
I’m not sure what the db_user=’root’ statement does?

2.Add database connection hardcoding the user name and password. (use the DBI connect call and store the handle)
This is somewhat confusing because there are no passwords on our database. I am also using access not mysql. DBI:mysql:host= ??
Expand|Select|Wrap|Line Numbers
  1. # $::db = DBI->connect(
  2. # "DBI:mysql:host=$::db_host;database=$::KTC.mdb",
  3. # $::db_user) || die $DBI::errstr;
  4.  
3.Add a query to select the product name, description, units in stock, and price. (use products table and only select one product, where “ProductID = $productID”)
Expand|Select|Wrap|Line Numbers
  1. my $stm= $::db->prepare("SELECT ProductName, ProductDescription, UnitsInStock, UnitPrice FROM Products WHERE ProductID = $productID ORDER BY ProductID");
  2. $stm->execute || die $::db->errstr;
  3. my $LST = $stm->fetchall_hashref('ProductID');
  4.  
4. Add statement handle definition and execution.
5. Code the loop by fetching an array from the statement handle. (use “fetchrow_array()”, will return records from database into an array “@product”, there are also other ways to do this, could return a hash)
6. Display the records. (students often get confused with this. You need to access the indices of the array to display in the table. Each index corresponds to the column name in the select statement of your query. The syntax to grab an index of an array for this project in perl is (“$product[x]” where x is a number pertaining to the column name, will map to product information)

I haven’t even looked at these requirements yet.

7. Disconnect database.
Expand|Select|Wrap|Line Numbers
  1. $rc = $dbh->disconnect();
  2.  
Do I need more than this?
Oct 27 '06 #1
2 11571
vssp
268 100+
=**************************************
= Name: Create Table/MS Access/ADO
= Description:This is a simple script th
= at will create a
new table in a Access Database. This is to show
how to connect to an Access Database thru Perl
by using DBI and the ADO driver. You can also
connect by using ODBC, by replacing where you
ADO. dbi:ADO:$DSN
= By: Randy McCleary
=
=This code is copyrighted and has = limited warranties.Please see http://w
= ww.Planet-Source-Code.com/vb/scripts/Sho
= wCode.asp?txtCodeId=457&lngWId=6 =for details. =**************************************

#!/usr/bin/perl -w
################################################## #
## This is a simple script that will create a
## new table in a Access Database. This is to show
## how to connect to an Access Database thru Perl
## by using DBI and the ADO driver. You can also
## connect by using ODBC, by replacing where you
## ADO. dbi:ADO:$DSN
################################################## #
use POSIX;
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use Time::Local
print header;

######################################
## Set DSN-Less Connection
######################################
my $DSN = 'driver=Microsoft Access Driver (*.mdb);dbq=C:\WINDOWS\perl.mdb';
my $dbh = DBI->connect("dbi:ADO:$DSN", '','')
or die "$DBI::errstr\n";

######################################
## Generate the SQL Statement
######################################
my $sql = <<"EndOfSQL";
CREATE TABLE tblContacts2 (
ID COUNTER,
LastName CHAR(40),
FirstName CHAR(40),
MiddleName CHAR(20);
HomePhone CHAR(40),
WorkPhone CHAR(40),
CellPhone CHAR(40);
BirthDay CHAR(20);
Fax CHAR(40),
Email CHAR(40),
Address1 CHAR(40),
Address2 CHAR(40),
City CHAR(30),
State CHAR(10),
ZipCode CHAR(20),
CONSTRAINT ID_PK PRIMARY KEY(ID)
)
EndOfSQL


################################
## Execute the SQL Statement
################################
$dbh->do($sql)
or die "Execution problem: $DBI::errstr";
print "Table was Created";

######################################
## Close the connection when finished:
######################################
$dbh->disconnect;



I hpe this helpfull for u

vssp
Oct 30 '06 #2
Thanks, I finally got it to connect, I just have a data fiormatting problem now.
Oct 31 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John Morgan | last post by:
I have Enterprise Manager on my local machine. For the last twelve months it has been connecting without problem to my online SQL Server database provided by my ISP. Three weeks ago the ISP...
2
by: news | last post by:
We currently have our mySQL server on the same box as the Apache server. For security and load balancing, we're going to be moving the mySQL server to another box. We're already using a single...
1
by: Tim Dol | last post by:
I have inherited a VB6-SP5 program connecting to an Access 2000 database. I'm relatively new to VB/Access programming, so I may overlook something obvious. I'll try to explain the problem I can't...
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
5
by: bill | last post by:
I am having difficulties connecting to a remote Sql server database in VB.net. Lets say my remote server is found at 255.255.255.255 and its name is MyServer. The database is called MyDatabase....
5
by: Sebastian | last post by:
I'm using the following code to connect to an access 2000 database. (with reference to adodb) Public DBvar As New ADODB.Connection() DBvar.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data...
5
by: Al | last post by:
Hi, I am looking for sample VB.net code for working with Access 97 database. I know how to work with SQL but I dont know how to read and writ into access 97. Any help is very much appreciated. ...
2
by: Patrick F | last post by:
Hi, i have SQL Server 2005 and a database set that is called, myCompany the problem is that i cant connect from my page to it, here is from the web.config: ( i have got this connectionstring from...
0
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, I've my win32 application which connecting to my sql database. till now i used MSDE as my database and now i want to upgrade to sql express 2005. after the upgrade i tried to connect to the...
1
by: Yew12 | last post by:
Hi, I'm having problems connecting to a Microsoft Access database. Im using PHP and ODBC but im not using a DNS. The scripts below give no errors but I they only give me a blank white screen. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.