Mega Code Archive

 
Categories / Delphi / Examples
 

Update your data base from a CGI

Title: Update your data base from a CGI Question: How can I update my database from a web browser? Answer: In my company have needed the confection of a program that permit my user to put certain data on a data base, without the intervention neither the installation additional, BDE, ADO or something else . The solution that I chose, by the relative simplicity of the project, it was accomplished a CGI, so that my user could use his browser simply. Here I describe the project: ++INTERFACE In the interface of the good client that more than the navigator, but the user would have to be capable of seeing the previous results for take decision to introduce the his own. It is thus, that a simple page HTML would not serve, since would have to collect data of a table, to show them and furthermore to show a form that they could fill and then to send. Thereinafter the user would press a button to send the information, this is updated and are shown the new data. ++SERVING. Of the foregoing have been able to conclude that they would have to have 3 actions of the CGI. Action Description /progm Showed the data of the user and the form /upd Updade data. /show Alone showed the updated data. ++CLIENTS. The users or clients of the application, executed it introducing the address that below is shown, in his browser, (IE for example). Web server, alone receives the parameter in the following way, something which begins in reality the small WEB application. (THIS LINK IS ONLY A EXAMPLE) http://192.168.70.25/times.exe/progm ++CODE object WebModule1: TWebModule1 OldCreateOrder = False OnCreate = WebModuleCreate Actions = item Default = True Name = 'WebActionItem3' PathInfo = '/progm' Producer = DataSetTableProducer1 end item Name = 'WebActionItem2' PathInfo = '/upd' OnAction = WebModule1WebActionItem2Action end item Name = 'WebActionItem1' PathInfo = '/show Producer = DataSetTableProducer2 end Left = 189 Top = 103 Height = 375 Width = 544 object qryHoras: TADODataSet Active = True ConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Docs\T' + 'IMES.mdb;Persist Security Info=False' CursorType = ctStatic CommandText = 'select * from Times order by Names' Parameters = Left = 48 Top = 88 end object DataSetTableProducer1: TDataSetTableProducer Footer.Strings = ( '' ' FULL NAME' + '' ' ' ' '0"' ' ' ' ' ' ' ' ' ' ' ' ' ' DIA' ' ' '' ' 8/5/20' + '00' ' 9/5/2000 'on' ' 10/5/2000 '/option' ' 11/5/2000 'tion' ' 12/5/2000 'ption' ' 15/4/5000 'on' ' 16/5/2000 'tion' ' 17/5/2000 '/option' ' 18/5/2000 'ption' ' 19/5/2000 'ption' ' 22/5/2000 'ion' ' 23/5/2000 'tion' ' 24/5/2000 '/option' ' ' ' ' ' ' ' ' ' ' ' ' ' TIME' ' ' '' ' 09:00 to' + ' 10:30' ' 10:30 to 12:30 'ption' ' 15:00 to 16:30 'ption' ' 16:30 to 18:00 'ption' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 'SEND' ' ' ' ' ' ') Header.Strings = ( ' ' ' ' ' ' ' 'size="5" color="#FF3333" 'go.jpg" width="92" height="65" '' ' ' ' 'or="#FF3333"SCHEDULE' ' MAINTAIN ' ' ' ' 'F3333" ' height="65"' ' ' ' ') DataSet = qryTime TableAttributes.Align = haCenter TableAttributes.Border = 1 Left = 48 Top = 144 end object DataSetTableProducer2: TDataSetTableProducer Header.Strings = ( '' ' ' ' '" height="42"' ' ' ' SCHEDULE' ' ' ' ' ' '') DataSet = qryTime TableAttributes.Align = haCenter TableAttributes.BgColor = 'White' TableAttributes.Border = 1 Left = 152 Top = 144 end object updHoras: TADOQuery ConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Docs\T' + 'imes.mdb;Persist Security Info=False' Parameters = item Name = 'pNames' Attributes = [paNullable] DataType = ftFixedChar NumericScale = 255 Precision = 255 Size = 510 Value = '0' end item Name = 'pDay' DataType = ftFixedChar NumericScale = 255 Precision = 255 Size = 510 Value = '0' end item Name = 'pTime' Attributes = [paNullable] DataType = ftFixedChar NumericScale = 255 Precision = 255 Size = 510 Value = '' end SQL.Strings = ( 'Insert Into Times (Names,Day, Time) VALUES (:pNames, :pDay, :p' + 'Time)') Left = 152 Top = 88 end end unit Main_Web; interface uses Windows, Messages, SysUtils, Classes, HTTPApp, DB, DBTables, DBWeb, DSProd, ADODB; type TWebModule1 = class(TWebModule) qryTimes: TADODataSet; DataSetTableProducer1: TDataSetTableProducer; DataSetTableProducer2: TDataSetTableProducer; updTimes: TADOQuery; procedure WebModule1WebActionItem2Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); procedure WebModuleCreate(Sender: TObject); procedure WebModule1WebActionItem3Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); private { Private declarations } public { Public declarations } end; var WebModule1: TWebModule1; implementation {$R *.DFM} procedure TWebModule1.WebModule1WebActionItem2Action(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); begin if not updTimes.Prepared then updTimes.Prepared := True; updTimes.Parameters.ParamByName('pNames').Value := Request.QueryFields.Values['pNames']; updTimes.Parameters.ParamByName('pDay').Value := Request.QueryFields.Values['pDay']; updTimes.Parameters.ParamByName('pTime').Value := Request.QueryFields.Values['pTime']; updTimes.ExecSQL; Response.SendRedirect('http://192.168.70.25/scripts/Times.exe/mostrar'); end; end. ++USED ELEMENTS As will be able you to appreciate the characteristics, here used are not anything novel, but I wish to explain briefly each one of they: TADODATASET: To bind to the database through OLE DB. One of them the qryTimes simply it accomplishes show the data, the other the updTimes, accomplishes the update on the data. DataSetTableProduc: For the automatic generation of the page HTML. Here I use due two that by norm it should be to use one different for each action. In this article can see that it exists some respect dependency to the serving where we run the application this can be solving using a small tip, shown in the page of Ian Marteens: http://www.marteens.com/trick2e.htm (in Spanish, sorry). I wait that it here exposed could clarify in certain way the doubts that we have all when we begin ourselves in the world of dynamic Web applications.