Oui je sais travailler sans problème avec MySql et Delphi. Il est important de choisir le bonne dll pour libmysql.dll et dbexpmysql.dll (voir site borland).
exemple de source:
unit MyDBCon;
interface
uses SysUtils, Classes, DBXpress, DB, SqlExpr, FMTBcd,Functions, Provider, DBClient, DBLocal, DBLocalS;
type TDMMyUsOptions = class(TDataModule) MySQLCon: TSQLConnection; MyClientDataSet: TSQLClientDataSet; SqlDataSetUpd: TSQLDataSet; procedure DataModuleCreate(Sender: TObject);
private procedure ReadInitFile(sFilename:string;sSubCateg:string;ObjCon:TSQLConnection); { Private declarations } public { Public declarations } procedure Connect(sFile:String;sGroup:String); procedure Close(); function GetDataset(sSql:string):TSQLClientDataSet; procedure GetLocDataSet(sSql:string;ClientDataSet:TSQLClientDataSet); function UpdateDataSet (sSql:Ansistring):integer; function LocateDataset (sTable:string;sField:string;sValue:string):Boolean; end;
var DMMyUsOptions: TDMMyUsOptions;
implementation
{$R *.dfm}
function TDMMyUsOptions.LocateDataset(sTable:string;sField:string;sValue:string):Boolean; var sSqlString:string;
begin result := false; //SQLDataSetUpd.DBConnection :=MySQLCon; SQLDataSetUpd.CommandType :=ctQuery; sSqlString:= 'select ' + sfield +' from '+ sTable + ' where ' + sField + ' = ' + ''''+ sValue+ ''''; SQLDataSetUpd.CommandText := sSqlString; SQLDataSetUpd.Open; if SQLDataSetUpd.Active = true then begin if SQLDataSetUpd.RecordCount > 0 then result := true; SQLDataSetUpd.Close; end; end;
procedure TDMMyUsOptions.ReadInitFile(sFilename:string;sSubCateg:string;ObjCon:TSQLConnection); var F:textfile; lig:String; bFlag:boolean; iPos:Integer; begin bflag:=false; if (sFileName <> '')and (sSubCateg <> '') then begin sSubCateg := '[' + sSubCateg + ']'; assignfile(F,sFileName); reset(F); while (not eof(F))and (bflag=false) do begin readln(F,lig); if InStr(0,lig,sSubCateg) = 1 then begin while bflag=false do begin readln(F,lig);
if InStr(0,lig,'User_Name')=1 then begin iPos:=Instr(0,lig,'='); if iPos > 1 then begin iPos:=length(lig)-ipos; ObjCon.Params.Values['User_Name']:=right(lig,ipos); end; end; if InStr(0,lig,'Password')=1 then begin iPos:=Instr(0,lig,'='); if iPos > 1 then begin iPos:=length(lig)-ipos; ObjCon.Params.Values['Password']:=right(lig,ipos); end; end; if InStr(0,lig,'Database')=1 then begin iPos:=Instr(0,lig,'='); if iPos > 1 then begin iPos:=length(lig)-ipos; ObjCon.Params.Values['Database']:=right(lig,ipos); end; end; if InStr(0,lig,'HostName')=1 then begin iPos:=Instr(0,lig,'='); if iPos > 1 then begin iPos:=length(lig)-ipos; ObjCon.Params.Values['HostName'] :=right(lig,ipos); end; end; if InStr(0,lig,'[END]')=1 then begin bflag:=True; end; end; end; end; closefile(f); end;
end;
function TDMMyUsOptions.GetDataset (sSql:string):TSQLClientDataSet; begin
MyClientDataSet.DBConnection :=MySQLCon; MyClientDataSet.CommandType :=ctQuery; MyClientDataSet.CommandText :=sSql; MyClientDataSet.Open;
result :=MyClientDataSet; end;
procedure TDMMyUsOptions.GetLocDataSet(sSql:string;ClientDataSet:TSQLClientDataSet); begin if ClientDataSet.Active=True then begin ClientDataSet.Close; end;
ClientDataSet.DBConnection :=MySQLCon; ClientDataSet.CommandType :=ctQuery; ClientDataSet.CommandText :=sSql; ClientDataSet.Open; end; function TDMMyUsOptions.UpdateDataSet(sSql:Ansistring):integer; begin try SQLDataSetUpd.CommandText :=sSql; result := SQLDataSetUpd.ExecSQL;
except result := -1; end; end;
procedure TDMMyUsOptions.Connect(sFile:String;sGroup:String); begin ReadInitFile(sFile,sGroup,MySQLcon); //ReadInitFile('MyUsOptions.txt','MyUsOptions',MySQLcon); MySQLcon.Open; end;
procedure TDMMyUsOptions.Close; begin if MySQLcon.Connected = true then begin MySQLcon.Close; MySQLcon.Free; end; end;
procedure TDMMyUsOptions.DataModuleCreate(Sender: TObject); begin
SQLDataSetUpd.SQLConnection :=MySQLCon; SQLDataSetUpd.CommandType :=ctQuery;
end;
end.
------------------------------- Réponse au message : -------------------------------
> lorsque j'execute : > MySQLDataSet.CommandType :=ctQuery; > MySQLDataSet.CommandText :='select Symbol From Tb2'; > MySQLDataSet.Open; > > Si Symbol est définit comme index ou clé primaire (et seulement dans ce cas la). J'ai l'erreur suivante : > Invalid variant type conversion. > Comment solutionner cette erreur..... > Merci +++ > Eric > >
|