This is the last of it plus a demo sample. Feel free to comment or
join the TMySQLDatabase group.
Thanks,
pdb
// MySQLDatabase.pas
//
// Code Ver : v0.2a
// Compiler : Delphi v6.24 & Delphi 2005 (Version 9)
// Database : MySQL v5.0.4
// Last Edit: April 20, 2005
//
//
// Basic description:
//
// This program enables basic MySQL database functionality using
// Delphi. The use of these classes are identical to TQuery but it is
// not a visual component. In addition, the code (a lot of which was
// lifted from the delphi vcl, db.pas) gives a clear example of how
// the tfields and tparams classes work.
//
// My main objective is to provide a simpler implementation than the
// standard delphi controls without design time editing. I find that
// straight coding is easier to understand and much clearer when
// demoing.
//
// My second objective is code reuse. I ported a my Realtime Dow
// Jones News Server (from Sybase ASE) in about 2 hours, including the
// database schema.
//
// Paul Di Biasio
//
//
// =========================== version changes =====================
// v0.1 April 20, 2005
// added tfield, tfields, tparam & tparams functionality
//
// v0.2a April 21, 2005
// added TMySQLNumericField, TMySQLFloatField
// fixed null fields pointing to previous record
// moved SQLStatement string to public for so you can monitor
// what gets sent
// fixed parametized queries of more than on line in sql[] list
// see BindParams
// set ParsedSQLCmd, program was parsing on every call to
// fieldbyname
// added logfilename: string; OutputToLog boolean; & proc
// AppendToLog to TMySQLQuery;
// added proc TMySQLParam.SetDataAsString.. to format escape
// chars
// fixed parameter binding problem when fValueBuffer had a ?
// char in it.
//
// ============================ TO DO LIST ==========================
// float an integer field types
// edit masks
// put warning when requested rows > threshold
// DatabaseErrorFmt(SFieldRangeError, [Value, DisplayName, Min,
Max]);
// program output for OutputServerResponse
// clear should reset fields and params and set ParsedSQL=false
// assign fieldclasstypes (see code after .end stmt)
// implement parameter binding
//
//
uses MySQLDatabase;
{$R *.dfm}
procedure TForm1.btnSelectClick(Sender: TObject);
var
MySQL: TMySQLDatabase;
MySQLQuery: TMySQLQuery;
r,c: integer;
begin
MySQL:=TMySQLDatabase.Create;
MySQL.ServerName:='localhost';
MySQL.UserName:='root';
MySQL.Password:='mysql';
MySQL.DatabaseName:='dbdemos';
MySQL.Open;
if MySQL.Active then begin
MySQLQuery:=TMySQLQuery.Create(MySQL);
MySQLQuery.SQL.add('SELECT *, Salary/12 Mthly, Salary/12*.075
FICA FROM employee where Salary>:in_salary and EmpNo>:in_empno');
MySQLQuery.ParamByName('in_salary').AsString:='50000';
MySQLQuery.ParamByName('in_empno').AsString:='0';
MySQLQuery.Open;
MySQLQuery.Fields[0].DisplayLabel:='Emp #';
MySQLQuery.Fields[0].DisplayWidth:=30;
MySQLQuery.FieldByName('EmpNo').DisplayWidth:=30; // same as above
MySQLQuery.FieldByName('HireDate').DisplayLabel:='Started';
MySQLQuery.FieldByName('HireDate').DisplayFormat:='mm/dd/yyyy';
MySQLQuery.FieldByName('Salary').DisplayFormat:='$ #,.00';
MySQLQuery.FieldByName('Mthly').DisplayFormat:='$ #,.00';
MySQLQuery.FieldByName('FICA').DisplayFormat:='$ #,.00';
MySQLQuery.FieldByName('FICA').DisplayLabel:='F.I.C.A.';
MySQLQuery.FieldByName('PhoneExt').DisplayFormat:='.00';
// can't demo because alignment because the std delphi grid
doesn't support it
MySQLQuery.FieldByName('Salary').Alignment:=taRightJustify;
// Note: if you change the query from SELECT * FROM employee
then the
// columns will not match. Rather than give an error, those
values are
// set to the private MySQLQuery.Fields.fDummyField record
StringGrid1.ColCount:=MySQLQuery.FieldCount+1; //label grid
columns
StringGrid1.ColWidths[0]:=24;
for c:=0 to MySQLQuery.FieldCount-1 do begin
StringGrid1.cells[c+1,0] :=MySQLQuery.Fields[c].DisplayLabel;
StringGrid1.ColWidths[c+1]:=MySQLQuery.Fields[c].DisplayWidth;
end;
edAffectedRows.text:=IntToStr(MySQLQuery.RecordCount);
//label grid rows
StringGrid1.RowCount:=MySQLQuery.RecordCount+1; //label grid rows
for r:=1 to MySQLQuery.RecordCount do
StringGrid1.cells[0,r]:=IntToStr(r);
// fill in the TStringGrid1
r:=0;
MySQLQuery.first;
while not MySQLQuery.eof do begin
inc(r);
for c:=0 to MySQLQuery.FieldCount-1 do
//StringGrid1.cells[c+1,r]:=MySQLQuery.Fields[c].AsString;
// these two lines do the same
StringGrid1.cells[c+1,r]:=MySQLQuery.FieldByNumber(c).AsString; //
these two lines do the same
MySQLQuery.next;
end;
MySQLQuery.Free;
end;
MySQL.free;
end;
-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/delphi-en/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/