How to connect an Access database in Perl

Source: Internet
Author: User
Tags connect odbc sql net win32 window access database access

The first step is to install the WIN32-ODBC module, which is as follows:

1: Download the win32-odbc.zip from the Tools column, download the WinZip to a temp directory after downloading, a total of three files:

Readme

Win32-odbc.ppd

Win32-odbc.tar.gz

2: Under the DOS window, run the following DOS command in the TEMP directory: ppm install WIN32-ODBC.PPD.

Then prepare the test database (Access)

1: Start Ms ACCESS, create a new empty database, name Odbctest.mdb, and save it in a directory (remember the path).

2: Then create a new table and create three fields:

Field name Data type length

Name character, length 50

Email characters, length 50

Age number, Long integer

Save this table as address (note that this example does not use an automatically incremented ID.) Enter several records:

The following are the referenced contents:

Nighthawk Nighthawk@163.net 20 1234567

John Jt@163.net 24 0284393293

Kit kit@21cn.com 18 3948932

After saving, close the database file.

3: Open the ODBC data source (32-bit) in Control Panel, locate the user data source list in the User DSN bar, select a row named "MS Access Database", and press the "Configure" key.

Press "SELECT ..." in the Database box to select the Odbctest.mdb file created in step 1.2, and press OK. All other items in the ODBC settings are set by default, then OK, OK, close the dialog window.

The final test:

The following are the referenced contents:
#!/usr/bin/perl
Use WIN32::ODBC;
$DSN = "MS Access to the Database";
$DBase = "Access.mdb";
#连接数据库
if (!) ( $db = new Win32::odbc ($DSN))) {
Print "Connection database failed. N";
Exit ();
}
else{
Print "Connect the database successfully (connection number:", $db->connection (), ") nn";
}
#数据库中的表
Print "Table in database:";
@tables = $db->tablelist;
Print @tables;
Print "n";
#选择数据表
if (! $db->sql ("SELECT * from [address] WHERE age>=20")) {
@FieldNames = $db->fieldnames ();
$Cols = $ #FieldNames + 1;
#表中字段数
Print "Table Address field number: $Colsn";
#字段列表
for ($i = 0; $i < $Cols; $i + +) {
Print "$FieldNames [$i]t";
}
Print "n";
#列出年龄大于20的记录
while ($db->fetchrow ()) {
@values = $db->data ();
Print @values;
Print "n";
}
}
##### SQL #########
#添加记录
$sqlinsert = "INSERT into address
VALUES (' Euler ', ' euler@21cn.com ', 28, ' 021-345689 ');
#更新记录
$sqlupdate = "UPDATE address SET age = age+10";
#删除记录
$sqldelete = "DELETE from address WHERE name= ' Jimtyan '";
$RC = $db->sql ($sqlinsert);
Die QQ (SQL failed "$sqlinsert":), $db->error (), QQ (n) if $RC;
$RC = $db->sql ($sqlupdate);
Die QQ (SQL failed "$sqlupdate":), $db->error (), QQ (n) if $RC;
$RC = $db->sql ($sqldelete);
Die QQ (SQL failed "$sqldelete":), $db->error (), QQ (n) if $RC;
#关闭链接
$db->close ();



Related Article

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.