Accessing database through HTTP/CGI

This is a complement to a previous question about accessing a database through the Internet.  From the answer I got, I have decided that the simplest way to do it would be to use HTTP and a CGI program on the server.

I already purchased the IMS component from Argosoft which contains an HTTP implementation.  From what I understand, I need to write a CGI program that will access the database on the server.  

Question 1:  Should I use Delphi to write the CGI program ? If so, is the CGIExpert component a good choice ?

Question 2: Does that mean that if the server is running Windows NT then I can use a single user version of Interbase as a database (what comes with Delphi Professional) ?

Question 3: What about BDE ? If I use Interbase, will I have to install BDE on the server that runs my CGI written in Delphi ?

I am new with CGI so if you could direct me to an example that would be great.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I've written some CGI programs in Delphi accessing local tables, i.e. Paradox/dBase tables through the BDE.

Tell me if you are interested in some code.

BTW, i've downloaded but never used "CGI Expert", so cannot tell how it works; but CGI is a much or less an easy protocol, so i better like writing my own code... :)
1.Yes you can use delphi and all other languages. You must only create good response in html. If your program can execute on the server side then it can be used for cgi. You can't use Delphi for CGI on linux but for Windows NT you can. CGI Expert is good start point however I thinnk so you can write better CGI without this componnent.
2.You can't because license will be broken.
3.Yes BDE is a god solution.

p.s. Try to read CGI specyfication for your server. You must understand ServerParsed CGI and how to use CGI in forms. You can pass parameters in two ways.......... read this !
ymailhotAuthor Commented:
Thank you Julio and Mirek.

Julio, your code would be greatly appreciated. Please send iy to
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

ymailhotAuthor Commented:
My Internet provider has a dedicated MS-SQL server which I could access via ODBC.  Would this be a better solution than using a CGI ? Would I need to install the BDE on each machine which accesses the database ?
ymailhotAuthor Commented:
My Internet provider has a dedicated MS-SQL server which I could access via ODBC.  Would this be a better solution than using a CGI ? Would I need to install the BDE on each machine which accesses the database ?
It is not an alternative to CGI; you need CGI to access the MS-SQL server itself; i.e. you need CGI as a bridge between the web server and the database engine.

Any ODBC compliant database engine may be accessed from Delphi; since you are going to write your CGIs with Delphi, then they can be able to access ODBC driven databases (MS-SQL in your case).

As to the machines which accesses the database, do not forget that database access happens only on the server side, no matter where the request comes from.

I have been programming severel different Delphi CGI applications using both MS-SQL and simple querying. In The final result, I've programmed an example using DDE with a CGI-Service and a CGI-Client. All examples are provided with simple explanations and necessary files.
The problem is, that you cannot keep the Database open with one application. You have to open and close it each time, and opening a Database and closing it, is rather ressource-demanding. In between, the INTRBASE service provides multiuser access to the Service application, security for hacking and fasten up the querying all in two simple applications.
You can have it, if necessary.

Here's an example of a simple Delphi CGI-application:
1. The HTML file first:
<title>Testing CGI - with Delphi Interbase Database</title>

<body background =>
<form action="http://localhost/cgi-win/test.exe" method=post>
<h2><Center>My Search Person CGI</Center></h2>
<HR>Word to search:
<input type= text name= searchfield1 size=20><br>
(with automatic wildcard selection)<br>
<select name=formfield1>
<option value="Forname">Forename
<option value="Surname">Surname
<option value="Username">Username
<option value="Age">Age
<HR><select name=formfield3>
<option value="IGNORE">ignore
<option value="AND">and
<option value="OR">or
Second search:
<input type= text name= searchfield2 size=20><br>
(med automatisk wildcard selektion)<br>
<select name=formfield2>
<option value="Forname">Forename
<option value="Surname">Surname
<option value="Username">Username
<option value="Age">Age


<input type=submit value="Submit">

       Request Protocol=HTTP/1.0
       Request Method=POST
       Request Keep-Alive=No
       Document Root=C:\WEBSITE\HTDOCS\
       Executable Path=/cgi-win/test.exe
       Server Software=WebSite/1.1e
       Server Name=
       Server Port=80
       CGI Version=CGI/1.3 (Win)
       Remote Address=
       User Agent=Mozilla/3.01 [da] (Win95; I)
       Content Type=application/x-www-form-urlencoded
       Content Length=81
       GMT Offset=3600
       Debug Mode=No
       Output File=C:\Website\CGI-Temp\43ws.htm
       Content File= C:\Website\CGI-Temp\43ws.inp
       [Form Literal]

       [Extra Headers]

      The Outputfile will contain the string: "C:\Website\CGI-Temp\43ws.htm"
      This means, that you have to write to axctly that filename. The Server program will handle it afterwards.
      The "43ws.INP" contains the inputinformation from the user, but you allready got them here under the [Form
      Litteral] topic.

      And now to the program example:

Unit TestUnit;

  Uses IniFiles, StdCtrls, DB, DBTables;

  PicDir = [String containing public path to pictures of users];
  db1: Tdatabase;
  dBase: TQuery;
  PicDir: String;

  procedure InitDB;
    db1 := TDatabase.create(nil);
    db1.databasename := 'MyBase';
    db1.drivername   := 'INTRBASE';

    db1.params.add('SERVER NAME=C:\MYBASE.GDB');
    db1.params.add('USER NAME=SYSDBA');
    db1.params.add('OPEN MODE=READ/WRITE');
    db1.params.add('SCHEMA CACHE SIZE=8');
    db1.params.add('SCHEMA CACHE TIME=-1');
    db1.params.add('MAX ROWS=-1');
    db1.params.add('BATCH COUNT=200');
    db1.params.add('ENABLE SCHEMA CACHE=FALSE');
    db1.params.add('SCHEMA CACHE DIR=');

    db1.loginprompt  := false;
    db1.connected    := true;

    DBase:= TQuery.create(nil);
    DBase.databaseName:= 'MyBase';
    DBase.requestLive:= true

  function Correct(s: String): String;
  var // Makes the first letter uppercase on each word
    i: Integer;
    oldc: Char;
    oldc:=' ';
    For i:=1 to length(s) do
      if (oldc=' ') and (s[i]<>' ') then

  Procedure GetTabel(var IniFile: TIniFile);
    s: String; // Search String
    t: String; // Search type
    DoubleSearch: String;
    Statement: String; //SQL Statement
    Bool: String;
    t := IniFile.ReadString('Form Literal','formfield1','');
    s := IniFile.ReadString('Form Literal','searchfield1','""');
    s:= LowerCase(s);
    Statement:='select * from MyTabel where ('+t+' Like ' + s + ')';
    Bool := IniFile.ReadString('Form Literal','formfield3','');
    if Bool<>'IGNORE'then
      t := IniFile.ReadString('Form Literal','formfield2','');
      s := IniFile.ReadString('Form Literal','searchfield2','""');
      S := LowerCase(s); // SQL is Case sensitive
      Statement:=Bool+' ('+t+' Like ' + s + ')';

  Procedure WriteResult(var f: textFile);
    Fname, Sname, User: String;
    Age, Tlf, PicID: String;
    If DBase.RecordCount>0 then
      while not DBase.eof do
        Fname:= DBase.FieldByName('Forename').asString;
        Sname:= DBase.FieldByName('Surname').asString;
        User:= DBase.FieldByName('UserName').asString;
        Age:= DBase.FieldByName('Age').asString;
        Tlf:= DBase.FieldByName('Tlf').asString;
        PicID:= DBase.FieldByName('PicID').asString;

        Writeln(f,'<B>Forename: </B>'+Correct(Fname)+'<BR>');
        Writeln(f,'<B>SurName: </B>'+Correct(Sname)+'<BR>');
        Writeln(f,'<B>User Ident.: </B>'+Uppercase(User)+'<BR>');
        Writeln(f,'<B>Age: </B>'+Age+'<BR>');
        Writeln(f,'<B>Telephone #: </B>'+Tlf+'<BR>');
        Writeln(f,'<CENTER><IMG SRC="'+PicDir+PicID+'"></CENTER><br><hr>');;
    end else
      Writeln(f,'<BR><HR><BR><B>The query returned no result </B><BR>');

  Procedure ProcessScript;
    IniFile: TInifile;
    filename: string;
    f: Textfile; //Same as 'File of Text'
    IniFile:= TInifile.create(Paramstr(1)); // Paramstr(1)

    // *** header begin ***

    filename:= IniFile.Readstring('System','Output File','');
    rewrite(f); // Create output file
    writeln(f,'HTTP/1.0 200 OK');
    writeln(f,'Date: Friday, 28 Nov-97, 13:36:12 GMT');
    writeln(f,'Allow-ranges: bytes');
    writeln(f,'Content-type: text/html');
    writeln(f,'Content-length: 0');
    writeln(f,'<body background =>');
    writeln(f,'The following matched the query: <BR><BR>');

    // *** header end ***

   GetData(IniFile); // Hand over the Inifile to the 'GetData' procedure, to finish the query.
   WriteResult(f); // Write the resulting attributes




Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.