📄 ch15.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 15 -- Windows CGI: Database Backending </TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 15</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Windows CGI: Database Backending</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#DatabaseandDataFormats" >Database and Data Formats</A>
<UL>
<LI><A HREF="#FlatFileDatabase" >Flat File Database</A>
<LI><A HREF="#RelationalDatabase" >Relational Database</A>
</UL>
<LI><A HREF="#CommunicatingwithDatabases" >Communicating with Databases</A>
<UL>
<LI><A HREF="#FlatFileRead" >Flat File Read</A>
<LI><A HREF="#ODBC" >ODBC</A>
<LI><A HREF="#SQL" >SQL</A>
<LI><A HREF="#DDE" >DDE</A>
</UL>
<LI><A HREF="#DatabaseTools" >Database Tools</A>
<UL>
<LI><A HREF="#VisualBasic" >Visual Basic</A>
<LI><A HREF="#Delphi" >Delphi</A>
<LI><A HREF="#cc" >C/C++</A>
<LI><A HREF="#OtherTools" >Other Tools</A>
</UL>
<LI><A HREF="#CreatingYourDatabase" >Creating Your Database</A>
<UL>
<LI><A HREF="#TheData" >The Data</A>
<LI><A HREF="#TheTools" >The Tools</A>
<LI><A HREF="#Communications" >Communications</A>
<LI><A HREF="#TheCode" >The Code</A>
<LI><A HREF="#UsingtheDatabase" >Using the Database</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
Too much information is available on the Internet for you to get
by without using databases. Every time you look at a search site
and every time someone places an order, data is being transferred,
searched, and stored. Behind the scenes, a lot of work is going
on, and it can be accomplished a number of different ways.
<P>
What goes into creating the functionality and maintaining these
databases? It can be a lot of work. For large servers with thousands
of users, search engines can be complex monsters occupying an
entire machine that has more RAM than most people have total hard
drive space. Online transactions often require their own special
server, in addition to the basic Web server, to store special
transaction information and perform all the transaction processing..
This information can get complex and overwhelming really quickly,
but it doesn't have to be that way.
<P>
Using Windows CGI (WinCGI) functionality, some popular tools,
and a little of your time, you can put your own Web-based database
functionality together in no time at all. I'm going to save the
example for the end of this chapter because you should be aware
of the many choices before continuing. This chapter covers the
following:
<UL>
<LI><FONT COLOR=#000000>Database and data formats</FONT>
<LI><FONT COLOR=#000000>Communications with a database</FONT>
<LI><FONT COLOR=#000000>Tools you can use</FONT>
</UL>
<P>
Of course, all this information is followed by the example. So,
if you're one of those people who looks at the last page of the
mystery novel before reading it, you can just jump there now.
But for the more adventurous, I cover all the elements that go
into such a choice, so you'll know if it's right for you and what
else you could do instead.
<H2><A NAME="DatabaseandDataFormats"><FONT SIZE=5 COLOR=#FF0000>Database
and Data Formats</FONT></A></H2>
<P>
A database stores information so that when people look for information
in a database, they can find it easily. Like tiny filing cabinets
on a hard drive, the database stores this information in just
about any organizational method, as long as someone or something
can help the searcher find that information later. In the database
world, this organization involves file formats and one of two
general categories of databases: flat file or not-flat file (relational).
<H3><A NAME="FlatFileDatabase">Flat File Database</A></H3>
<P>
Accounting books, tables on paper, old spreadsheets, lists of
information separated by semicolons in a text file-they are all
the quintessential representations of what are dubbed <I>flat
file </I>databases. What exactly is a flat file database? If you
take a list of information and save it to a file in some order,
you end up with a database. But how deep does the information
in that database go? The information goes just as far as what
you put into it. Flat file databases are like a phone book: You
can look up a person's name or look up a business' name, but you're
limited in what else you can do with the information. If you're
looking for a friend you knew years ago, and you find 30 names
that match, that's all the information you have to go on. You've
used up what information is available to you in that level of
the database, and you need more depth of detail to make the right
choice-unless you plan to use the brute force method and call
every person until you get the right one. Once the information
is presented in its original format, there's no more "depth"
to what is contained in that file. Sure, you can find other files
and other sources of information, but you have to track down the
information yourself, the hard way. Instead of having the depth
and breadth of what you really need in that instance, the source
of information provides only a shallow level of detail. In short,
it's "flat."
<P>
Don't think for a minute that flat file databases are useless.
Far from it! I just used the preceding illustration to point out
one of the basic ideas behind a flat file database-it's just one
level of data. This kind of database still has a large number
of uses, like a phone book, a shopping catalog, a dictionary,
a menu-anything that has pieces of data that tie together very
tightly and provide what it's designed to with that single tier
of information.
<P>
Creating a flat file database can be as easy as entering information
into a file sequentially, like a typical spreadsheet. Save it
to a file, and you have an instant flat file database. The trick
is in ordering the data so that you can quickly and easily scan
through the file later.
<H3><A NAME="RelationalDatabase">Relational Database</A></H3>
<P>
When you need to get more information from data, you have to go
beyond a flat file database. If you're looking for a friend's
name in the ultimate phone book, you might say, "Find everyone
in New York with the last name of Jones, first initial J, who
went to school in Texas, and is married to someone with the first
initial of R or M." Calling directory assistance won't get
you that kind of information-unless directory assistance is a
relational database of rather prodigious size and flexibility.
<P>
Relational databases can do what their name implies-establish
relations between pieces of information. I'm not saying that they're
all-knowing and do this work by themselves; I'm just saying that
they provide the extra layers of information that you're looking
for. So, for example, in a relational database, you might have
a table of customers and their addresses, a table of names of
people who attended your user conference, and a table of resellers
of your product. Using a relational database, you could ask for
a list of everyone who attended your user conference but isn't
a customer and get the name and address of the reseller closest
to that person. You could do this all in one big long command,
without any steps in between.
<P>
Relational databases have several advantages over flat file databases.
In a relational database, tables can point to one source of information;
if five different tables include someone's address, for example,
they're all really just pointing to some arbitrary storage space
that they interpret as the address instead of five copies of that
same data floating around. The power of being able to perform
complex operations also weighs heavily in the favor of relational
databases and makes heavy-duty processing easy.
<P>
An important point to come to terms with, though, is that a relational
database is not necessarily easy to use. You can't just say, "Give
me record #5." You have to be familiar with the syntax of
the commands the database uses and how to create (and in some
cases interlink) databases effectively. When you see positions
for database administrators advertised, you should realize that
the position is not just someone who watches over a filing cabinet;
keeping all the connections and the data safe, accessible, and
up to date is a real endeavor.
<H2><A NAME="CommunicatingwithDatabases"><FONT SIZE=5 COLOR=#FF0000>Communicating
with Databases</FONT></A></H2>
<P>
Just as you can make choices of different database types, you
can make choices for how you access the data. Different methods
of communication have their own benefits and drawbacks, but the
format of the file you're accessing and what you're accessing
it with can sometimes end up making the choice for you.
<H3><A NAME="FlatFileRead">Flat File Read</A></H3>
<P>
Typical flat file access is either <I>sequential</I>, in which
you just keep reading until you get there, or <I>offset</I>, in
which you can either pick specific sections of the file (based
on sizes of data chunks you plan on storing) or be slightly more
random about the whole process. Delimited ASCII text files, for
example, can be addressed through a sequential read and the use
of pattern matching; you simply check each line for the existence
of some string and then store it (or a pointer to it) if and when
it does match. Offset reads allow the program to jump to specific
sections of the database. If you have a name field of 30 bytes
followed by a phone number field of 30 bytes, for example, you
can simply pick a byte position to land yourself at a particular
field in a specific record.
<H3><A NAME="ODBC">ODBC</A></H3>
<P>
Open Database Connectivity (ODBC) provides a translator for letting
different external <BR>
programs and databases talk. Like CGI in its own way, ODBC ensures
that incoming information gets placed into what the database considers
a standard form and that outgoing information gets sent back in
an understandable manner to the original program. These translators,
or drivers, come in different flavors for different database formats
and make use of some system files in the background that tie them
into the rest of the system.
<P>
ODBC doesn't just help with files on the same system. You can
have an ODBC driver that takes care of special networking communications
needed to go from a Windows pc to a UNIX machine running an Oracle
database, and indeed a whole slew of them are available from different
vendors. You still need some other hardware and software on the
machine asking the questions to ensure that information can get
through and then get back, but the driver takes care of a lot
of messy details.
<H3><A NAME="SQL">SQL</A></H3>
<P>
The Structured Query Language (SQL) is a standard for asking questions
of more powerful databases. Most flat file databases don't support
it because some overhead is involved, and with simple databases,
SQL has power that's not necessarily worth the effort. When you
need it, though, SQL provides the power to make even the most
complex relational databases behave like you want them to.
<P>
The concept behind using SQL is that it's reasonably like forming
a sentence. In real life, <BR>
you might say, "Get all the strawberries in the big bucket."
In SQL, you would enter <TT><FONT FACE="Courier">SELECT strawberries
from BigBucket</FONT></TT>.
<P>
Using SQL requires having a handle on SQL terms: <I>actions</I>,
<I>fields</I>, <I>tables</I>, and <I>conditions</I>. You can use
one of the four primary SQL actions: <TT><FONT FACE="Courier">SELECT</FONT></TT>,
<TT><FONT FACE="Courier">INSERT</FONT></TT>, <TT><FONT FACE="Courier">UPDATE</FONT></TT>,
or <TT><FONT FACE="Courier">DELETE</FONT></TT>. Because most data
operation on the Web, such as search engines, rely on obtaining
information from a large database rather than modifying it, we'll
leave analysis of those data-changing operations (<TT><FONT FACE="Courier">INSERT</FONT></TT>,
<TT><FONT FACE="Courier">UPDATE</FONT></TT>, and <TT><FONT FACE="Courier">DELETE</FONT></TT>)
for something you can investigate if your application requires
it. All you'll need for that analysis is a good SQL language book,
a database, and some time to try it all out.
<P>
Getting things from a database is selecting them, so that part's
easy. Next, you have to figure out what you're getting. You also
have to be careful not to confuse getting with any special instructions
like "…and don't get any rotten ones!"
<P>
In SQL, you specify the fields of information that you want to
return-such as phone number, address, or name-that correspond
to fields you've created in your database. Because SQL is normally
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -