How to connect to an Access database in Perl

Source: Internet
Author: User
Open the ODBC data source (32-bit) in the control panel, find the user data source list in the user DSN column, select a row named "MSAccess97Database", and press "configure.

This article teaches you how to connect to the Access database under Perl.

1. install the Win32-ODBC module

Step 1:

Download win32-odbc.zip from the toolsbar and use winzip to unbind it to a temp directory. There are three files in total:

Readme Win32-ODBC.ppd Win32-ODBC.tar.gz

Step 2:

In the DOS window, run the following doscommand in The temp directory: ppm install Win32-ODBC.ppd.

2. prepare the database for testing (Access)

Step 1:

Start ms access and create a new empty database named odbctest. mdb, which is stored in a directory (remember the path ).

Step 2:

Create a new table and create three fields:

Field Name data type length Name character, length 50 Email character, length 50 Age number, long integer

Keep this table as address (note that in this example, no ID is automatically added. Input several records:

Nighthawk nighthawk@163.net 20 1234567 John jt@163.net 24 0284393293 kit kit@21cn.com 18 3948932

After being retained, close the database file.

Step 3:

Open the ODBC data source (32-bit) in the control panel, find the user data source list in the user DSN column, and select a row named "MS Access 97 Database, then press the "configure" key.

In the Database box, press "Select...", Select the Database file odbctest. mdb created in step 1.2, and press OK. All other items in ODBC settings use the default settings, and then OK. close the dialog window.

III. test:

#! /Usr/bin/perl use Win32: ODBC; $ DSN = 'Ms Access 97 database'; $ DBase = 'Access. mdb '; # connect to the Database if (! ($ Db = new Win32: ODBC ($ DSN) {print 'failed to connect to the database. n'; exit () ;}else {print 'Connection succeeded (Connection number:', $ db-> Connection (), ') nn ';} # database table print 'database table: '; @ tables = $ db-> TableList; print @ tables; print 'n'; # select the data table if (! $ Db-> SQL ('select * FROM [address] WHERE age> = 20') {@ FieldNames = $ db-> FieldNames (); $ Cols =$ # FieldNames 1; # number of fields in the table print 'table address field count: $ Colsn'; # Field list for ($ I = 0; $ I <$ Cols; $ I) {print '$ FieldNames [$ I] t';} print 'n'; # List records older than 20 while ($ db-> FetchRow ()) {@ values = $ db-> Data (); print @ values; print 'n ';}} ##### SQL ######### add record $ sqlinsert = 'Insert INTO address VALUES ('Euler ', 'Euler @ 21cn.com', 28, '021-345689 ')'; # UPDATE record $ sqlupdate = 'update address SET age = age 10 '; # DELETE a record $ sqldelete = 'delete FROM address WHERE name = 'jimtyance'; $ rc = $ db-> SQL ($ sqlinsert ); die qq (SQL Failure '$ sqlinsert':), $ db-> Error (), qq (n) if $ rc; $ rc = $ db-> SQL ($ sqlupdate); die qq (SQL Failure '$ sqlupdate':), $ db-> Error (), qq (n) if $ rc; $ rc = $ db-> SQL ($ sqldelete); die qq (SQL Failure '$ sqldelete':), $ db-> Error (), qq (n) if $ rc; # closed link $ db-> Close ();
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.