Perl Connect Access Database

zhaozj2021-02-08  329

Perl Connect Access Database

The topic is to install ActivePerl (505 or more), and MS Access 97

1. Install the Win32-ODBC module

step 1:

Download Win32-odbc.zip from the Tools section, unwave to a Temp directory with Winzip after downloading, there are three files:

Readme

Win32-odbc.ppd

Win32-odbc.tar.gz

Step 2:

Under the DOS window, run the following DOS command in the Temp directory:

PPM Install Win32-odbc.ppd

II. Prepare the test database (Access)

step 1:

Start MS Access, create a new empty database, named odbctest.mdb, save it in a directory (remember the path).

Step 2:

Then create a table to create three fields:

Field name data type

Name characters, length 50

Email character, length 50

AGE number, long integer

Save this table as Address (Note that this example is not available with an automatically added ID). Enter a number of records:

Nighthawk nighthawk@163.net 20 1234567

John jt@163.net 24 0284393293

Kit Kit@21cn.com 18 3948932

After saving, turn off the database file.

Step 3:

Open the ODBC data source (32-bit) in the control panel, in the user DSN column, find the user data source list, select the name "MS Access 97 Database", then press the Configure button.

Press "SELECT .." in the Database box, select the database file odbctest.mdb established in step 1.2, press OK. Other projects in ODBC settings are all default settings, then OK, determine, close the dialog window.

Third. At this time, the database is already available, let's 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 "Connect the database failed ./N";

exit ();

}

Else {

Print "Connect the database success (connection number:", $ dB-> connection (), ") / n / n";

}

# 图 表 数据 数据

PRINT "Table in the database:";

@Tables = $ dB-> TableList;

Print @tables;

Print "/ n";

# Select data table

IF (! $ db-> sql ("Select * from [address] where agn> = 20")) {

@Fieldnames = $ db-> fieldnames ();

$ Cols = # FieldNames 1;

# Table in the field

Print "Table Address field Number of: $ cols / n";

# Field list

For ($ I = 0; $ i <$ cols; $ i ) {

Print "$ FIELDNAMES [$ I] / T";

Print "/ n";

# List the record of age 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 Record

$ 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;

# Close the link

$ dB-> close ();

For further learning, please visit Win32-ODBC Module Authors Home: http://www.roth.net/perl/odbc/

Nighthawk

转载请注明原文地址:https://www.9cbs.com/read-1519.html

New Post(0)