I am working in Delphi and I have the following code:
try
conexion:=TConexion.Create();
finally
with DataModule1.qinsertproducto do
begin
close;
sql.Clear;
SQL.Add('INSERT INTO producto (nombre,unidad,precio,stock,idcat,idmarca,codalfa,aliiva,idsubcat,granel,contenido)');
SQL.Add(' VALUES (nombre=:nombre,unidad="u",precio=:precio,stock=:stock,idcat=:idcat,idmarca=:idmarca,codalfa=:codalfa,aliiva=:aliiva,idsubcat=0,granel="n",contenido="1")');
Params.ParamByName('nombre').Value:=nombre;
Params.ParamByName('precio').AsFloat:=precio;
Params.ParamByName('stock').AsFloat:=stock;
Params.ParamByName('idcat').AsInteger:=cat;
Params.ParamByName('idmarca').AsInteger:=marca;
Params.ParamByName('codalfa').AsString:=codalfa;
Params.ParamByName('aliiva').AsFloat:=iva;
ExecSQL();
end;
end;
end;
For the connection I use the singleton pattern. I share it just in case if it has something to do with it:
unit SingleConexion;
interface
type
TConexion = class
private
class var _instance: TConexion;
public
//Global point of access to the unique instance
class function Create: TConexion;
destructor Destroy; override;
end;
implementation
{ TSingleton }
uses MODULODATOS,IniFiles,Forms,Dialogs,Sysutils;
class function TConexion.Create: TConexion;
var
ini:TIniFile;
begin
if (_instance = nil) then
begin
ini:=TInifile.Create(ExtractFilePath(Application.ExeName) + 'caja.ini');
with DataModule1.CONEXION do
begin
Close;
DriverName:='MySQL';
Params.Values['UserName']:=ini.ReadString('admin','User','');
Params.Values['Password']:=ini.ReadString('admin','Pass','');
Params.Values['HostName']:=ini.ReadString('admin','IP','');
Params.Values['DataBase']:=ini.ReadString('admin','bd','');
Params.Values['Port']:=ini.ReadString('admin','Port','3306');
Open;
end;
_instance:= inherited Create as Self;
end;
result:= _instance;
end;
destructor TConexion.Destroy;
begin
_instance:= nil;
inherited;
end;
end.
The thing is that the idproduct is autoincremental and the values of the variables assigned to the parameters do arrive because I tested them with a ShowMessage. But it inserts a new record with all empty fields.
I would try to focus on the
INSERT
.try..except
to know if any error is generated in the execution (the complete code is not there and I don't know if you have already captured it).ExecSQL
which normally returns the number of inserted records.