yo trabajo con dbexpress, ahora me siento medio autista, ya que practicamente nadie los ocupa, tube varios problemas, qu e ya resolvi....
te envio el codigo de mis rutinas, si alguien ayuda a mejorarla mejor:
primero debes tener un TSQLConection, e la cual conectar, para ello debes primero hacer doble click en el e informar que tipo de coneccion vas a usar ( mysql), luego de definir esto debes poner la ruta de las dlls necesarias, que son dbexpmysql.dll y c:\mysql\bin\LIBMYSQL.dll por lo general (ojo que el driver funciona ok solo hasta la version 4.0.algo, salvo que funcione bien con la 4.1 o superior y no me halla enterado), luego puedes usar los componentes TSImpledataset para tus consultas y acceso a datos, las coplicaciones van con midas, que nunca me funcionaro bien... por ello cree unas rutinas para solucionar estos problemas, para ello cree una TSQLQuery para manejar las transacciones.... que debe existir con el nombre consulta, el resto es pan comido... en vez de usar applyupdate usas el procedimiento que esta mas a bajo y le pasas como parametro la componente TSimpleDataset que tiene tu consulta a datos: las reglas que puse de estas son:
* linea 1 para select +campos
* linea 2 para from +tabla
* linea 3 para where +condicion
* linea 4 para order by +campos
eso, solo sirve para actualizar 1 tabla para mas habria que arreglar el codigo.... yo las tengo como publicas en frm_main asi que deberas ajustarla a tu formulario...... saludos.
Código Delphi
[-]
Function TFrm_Main.Delimitador(DataSet:TSimpleDataSet;Campo:String;Old_Value:Boolean):String;
var d,m,a:Word;
TextoAux,Flotante:String;
i:Integer;
Nuevo:Boolean;
FloatAuxouble;
FechaAux:TDateTime;
Begin
with DataSet do
Begin
Nuevo:=False;
Try
If fieldbyname(Campo).DataType in [ftInteger,ftFloat,ftCurrency]
Then
Begin
FloatAux:=fieldbyname(Campo).OldValue;
end
else
Begin
If fieldbyname(Campo).DataType in [ftDate,ftTime,ftDateTime]
then FechaAux:=fieldbyname(Campo).OldValue
else Flotante:=fieldbyname(Campo).OldValue;
end;
except
Nuevo:=true;
end;
if fieldbyname(Campo).DataType in [ftString,ftBlob] then
Begin
Flotante:='';
If (Old_Value) and (not Nuevo) then TextoAux:=fieldbyname(Campo).OldValue
else TextoAux:=fieldbyname(Campo).AsSTring;
for i:=1 to length(TextoAux) do
Begin
if (TextoAux[i] = '"') or
(TextoAux[i] = '\') or
(TextoAux[i] = '''')
then Flotante:=Flotante+'\';
Flotante:=Flotante+TextoAux[i];
end;
Result:='"'+Flotante+'"';
End
else
Begin
if fieldbyname(Campo).DataType in [ftDate,ftTime,ftDateTime] then
Begin
If (Old_Value) and (not Nuevo)
then DecodeDate(fieldbyname(Campo).OldValue,a,m,d)
Else DecodeDate(fieldbyname(Campo).AsDateTime,a,m,d);
Result:=''''+FormatFloat('00',a)+'-'+FormatFloat('00',m)
+'-'+FormatFloat('00',d)+'''';
End
else
Begin
If fieldbyname(Campo).DataType in [ftInteger] then
Begin
If (Old_Value) and (not Nuevo)
then Result:=IntTostr(fieldbyname(Campo).OldValue)
else Result:=IntTostr(fieldbyname(Campo).AsInteger);
end
else
Begin
If (Old_Value) and (not Nuevo)
then Flotante:=fieldbyname(Campo).OldValue
else Flotante:=fieldbyname(Campo).AsString;
for i:=1 to length(Flotante) do
begin
if Flotante[i] = ',' then Flotante[i]:='.';
end;
Result:=Flotante;
end;
end;
end;
end;
end;
Procedure TFrm_Main.ActualizaRegistro(Query:TSimpleDataset;Refresca:Boolean);
var AuxSTR,NombreIndice,MYNombreIndice,Tabla,VClave,Linea,Tabladestino,CampoOrigen,
CampoDestino,Condicion,CondicionOK,Campo:String;
CONTADOR,XX,ClaveP,Rec_no:Integer;
X:TStringList;
a,m,D:Word;
CampoClave:Array[1..7]of String;
OKRef,Nuevo:Boolean;
FloatAux: Double;
FechaAux:TDateTime;
Begin
If Query.ClassType = TSimpleDataSet then
begin
x:=TStringList.Create;
x.Clear;
x.text:=Query.DataSet.CommandText;
Tabla:=copy(x[1],pos(' ',x[1])+1,length(x[1])-(pos(' ',x[1])));
ClaveP:=0;
Try
Consulta.SQL.Text:='DESCRIBE '+Tabla;
Consulta.Open;
while not Consulta.eof do
Begin
If Consulta.Fields[3].AsString <> ''
then
Begin
Inc(ClaveP);
CampoClave[ClaveP]:=Consulta.Fields[0].AsString;
end;
Consulta.next;
end;
NombreIndice:='';
MYNombreIndice:='';
for CONTADOR:=1 to ClaveP do
Begin
if CONTADOR > 1 then
Begin
NombreIndice:=NombreIndice+' and ';
MYNombreIndice:=MYNombreIndice+' and ';
end;
NombreIndice:=NombreIndice+CampoClave[CONTADOR]+' = '
+Delimitador(Query,CampoClave[CONTADOR],True);
if not (Query.FieldByName(CampoClave[CONTADOR]).DataType
in [ftDate,ftTime,ftDateTime]) then
MyNombreIndice:=MyNombreIndice+CampoClave[CONTADOR ]+' = '
+Delimitador(Query,CampoClave[CONTADOR],true)
else
Begin
DecodeDate(Query.FieldByName(CampoClave[CONTADOR]).OldValue,a,m,d);
MYNombreIndice:=MYNombreIndice+CampoClave[CONTADOR]+' = '''
+IntTostr(a)+'-'+IntToStr(m)+'-'+IntToStr(d)+'''';
end;
end;
Consulta.SQL.Clear;
Consulta.SQL.Add('Select *');
Consulta.SQL.Add('From '+Tabla);
Consulta.SQL.Add('Where '+NombreIndice);
Consulta.Open;
VClave:=Trim(Consulta.Fieldbyname(CampoClave[1]).asString);
Consulta.close;
Consulta.SQL.Clear;
Nuevo:=false;
try
If Query.FieldByName(CampoClave[1]).DataType in [ftInteger,ftFloat,ftCurrency]
Then
Begin
FloatAux:=Query.FieldByName(CampoClave[1]).OldValue;
end
else
Begin
If Query.FieldByName(CampoClave[1]).DataType in [ftDate,ftTime,ftDateTime]
then FechaAux:=Query.FieldByName(CampoClave[1]).OldValue
else AuxSTR:=Query.FieldByName(CampoClave[1]).OldValue ;
end;
except
Nuevo:=true;
end;
If (Nuevo) and (VClave <> '') then
Begin
Showmessage('Error, Clave Duplicada, cambie la clave e intente Nuevamente');
end
else
Begin
If (VClave = '') then
Begin
Consulta.SQL.Add('Insert into '+Tabla);
Consulta.SQL.Add('(');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
Consulta.SQL.Add('');
XX:=1;
for CONTADOR:=0 to Query.FieldCount - 1 do
BEGIN
TRY
IF LENGTH(Consulta.SQL[XX]) > 150 THEN INC(XX);
If (Query.Fields[contador].FieldKind = fkData) then
Begin
if CONTADOR <> 0 then Consulta.SQL[XX]:=Consulta.SQL[XX]+','
+Query.Fields[Contador].DisplayName
else
Consulta.SQL[XX]:=Consulta.SQL[XX]+Query.Fields[Contador].DisplayName;
end;
except
end;
end;
XX:=4;
Consulta.SQL[XX]:=Consulta.SQL[XX]+') Values (';
for CONTADOR:=0 to Query.FieldCount - 1 do
BEGIN
TRY
IF LENGTH(Consulta.SQL[XX]) > 200 THEN INC(XX);
If (Query.Fields[contador].FieldKind = fkData) then
Begin
IF (Query.Fields[Contador].ASSTRING <> '') THEN
Begin
if CONTADOR <> 0
then Consulta.SQL[XX]:=Consulta.SQL[XX]+','
+Delimitador(Query,Query.Fields[contador].DisplayName,false)
else Consulta.SQL[XX]:=Consulta.SQL[XX]
+Delimitador(Query,Query.Fields[contador].DisplayName,false);
end
else
Begin
Consulta.SQL[XX]:=Consulta.SQL[XX]+',Null';
end;
end;
except
end;
end;
Consulta.SQL[6]:=Consulta.SQL[6]+')';
Consulta.ExecSQL;
end
else
Begin
Consulta.SQL.Clear;
Consulta.SQL.Add('Update '+Tabla+' Set ');
for CONTADOR:=0 to Query.FieldCount - 1 do
BEGIN
If (Query.Fields[contador].FieldKind = fkData) then
Begin
If (Query.Fields[contador].AsString <> '')
then
Begin
if Consulta.SQL.Count = 1
then Consulta.SQL.Add(Query.Fields[contador].DisplayName+' = '
+Delimitador(Query,Query.Fields[contador].DisplayName,false))
else Consulta.SQL.Add(','+Query.Fields[contador].DisplayName+' = '
+Delimitador(Query,Query.Fields[contador].DisplayName,false));
end
else
Begin
if Consulta.SQL.Count = 1
then Consulta.SQL.Add(Query.Fields[contador].DisplayName+' = Null')
else Consulta.SQL.Add(','+Query.Fields[contador].DisplayName+' = Null')
end;
end;
END;
Consulta.SQL.Add('Where '+NombreIndice);
Consulta.ExecSQL;
try
Consulta.SQL.Text:='Select * from system_ir where Tabla = "'+UPPERCASE(Tabla)+'"';
Consulta.Open;
OKRef:=True;
with TSQLQuery.Create(nil)do
Begin
SQLConnection:=Consulta.SQLConnection;
while not Consulta.eof do
Begin
Tabladestino:=Consulta.fieldByname('Tabla_Destino').AsString;
CampoOrigen:=Consulta.fieldByname('Campo_Origen').AsString;
CampoDestino:=Consulta.fieldByname('Campo_Destino').AsString;
If Consulta.fieldByname('Condicion').AsString = '' then
SQL.Text:='Update '+Tabladestino+' set '+CampoDestino+' = '
+Delimitador(Query,CampoOrigen,false)+' where '+CampoDestino+' = '
+Delimitador(Query,CampoOrigen,True)
else
Begin
Condicion:=Consulta.fieldByname('Condicion').AsString;
If pos('= :', Condicion) = 0
then CondicionOK:=Condicion
else
begin
While pos('= :', Condicion) <> 0 do
Begin
CondicionOK:=Copy(Condicion,1,Pos('= :', Condicion)+1);
Condicion:=Copy(Condicion,Pos('= :', Condicion)+3,
length(Condicion)-(Pos('= :', Condicion)+2));
If Pos(' ',Condicion) = 0
then
Begin
Campo:=Condicion;
Condicion:='';
end
else
Begin
Campo:=copy(Condicion,1,pos(' ',Condicion)-1);
Condicion:=copy(Condicion,pos(' ',Condicion)+5,
length(Condicion)-pos(' ',Condicion)+5);
end;
CondicionOK:=CondicionOK+Delimitador(Query,Campo,True);
end;
If Condicion <> '' then CondicionOK:=CondicionOK+Condicion;
end;
SQL.Text:='Update '+Tabladestino+' set '+CampoDestino+' = '
+Delimitador(Query,CampoOrigen,false)+' where '+CampoDestino+' = '
+Delimitador(Query,CampoOrigen,True)+' and '+CondicionOK;
end;
ExecSQL;
Consulta.Next;
end;
Free;
end;
except
OKRef:=False;
end;
If not OKRef then Showmessage('Error de I.R');
end;
end;
If Refresca then
Begin
Rec_no:=query.RecNo;
query.Close;
query.Open;
Query.MoveBy(Rec_no-1);
end;
except
Showmessage('Error: Mostrar este mensage a su administrador:'+#13+Consulta.SQL.Text);
end;
end;
end;
Function TFrm_main.Apply_Delete(Query:TSimpleDataset):Boolean;
var x:TStringList;
Tabla,NombreIndice,MYNombreIndice:String;
CampoClave:Array[1..7]of string;
ClaveP,CONTADOR:Integer;
a,m,d:Word;
Begin
Result:=False;
If Query.ClassType = TSimpleDataSet then
begin
x:=TStringList.Create;
x.Clear;
x.text:=Query.DataSet.CommandText;
Tabla:=copy(x[1],pos(' ',x[1])+1,length(x[1])-(pos(' ',x[1])));
ClaveP:=0;
Try
Consulta.SQL.Text:='DESCRIBE '+Tabla;
Consulta.Open;
while not Consulta.eof do
Begin
If Consulta.Fields[3].AsString <> ''
then
Begin
Inc(ClaveP);
CampoClave[ClaveP]:=Consulta.Fields[0].AsString;
end;
Consulta.next;
end;
NombreIndice:='';
MYNombreIndice:='';
for CONTADOR:=1 to ClaveP do
Begin
if CONTADOR > 1 then
Begin
NombreIndice:=NombreIndice+' and ';
MYNombreIndice:=MYNombreIndice+' and ';
end;
NombreIndice:=NombreIndice+CampoClave[CONTADOR]+' = '
+Delimitador(Query,CampoClave[CONTADOR],True);
if not (Query.FieldByName(CampoClave[CONTADOR]).DataType
in [ftDate,ftTime,ftDateTime]) then MyNombreIndice:=
MyNombreIndice+CampoClave[CONTADOR ]+' = '
+Delimitador(Query,CampoClave[CONTADOR],True)
else
Begin
DecodeDate(Query.FieldByName(CampoClave[CONTADOR]).AsDateTime,a,m,d);
MYNombreIndice:=MYNombreIndice+CampoClave[CONTADOR]+' = '''
+IntTostr(a)+'-'+IntToStr(m)+'-'+IntToStr(d)+'''';
end;
end;
Consulta.SQL.Clear;
Consulta.SQL.Add('Delete ');
Consulta.SQL.Add('From '+Tabla);
Consulta.SQL.Add('Where '+NombreIndice);
Consulta.ExecSQL;
Apply_Delete:=True;
Except
Apply_Delete:=False;
end;
end;
end;