Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Conexión con bases de datos (https://www.clubdelphi.com/foros/forumdisplay.php?f=2)
-   -   insertar fecha con ado y Tquery (https://www.clubdelphi.com/foros/showthread.php?t=64924)

microbiano 10-11-2009 17:56:22

insertar fecha con ado y Tquery
 
bueno el problema que tengo es que no guarda la fecha correcta en la base de datos por ejemplo si paso como parametro 10/11/2009 en la base de datos me inserta la fecha 1900-01-01 00:00:00.000, y no encuentro el problema alguien me puede ayudar
Código Delphi [-]
procedure Tfcontribuyente.BguardarClick(Sender: TObject);
 var wsap: string;
 var wnocontrato: string;
 var wnombre: string;
 var wpaterno: string;
 var wmaterno: string;
 var wlocalidad: string;
 var wcalle: string;
 var wnoext: integer;
 var wnoint: integer;
 var wcp: integer;
 var wfechare: TDate;
 var wtel: String;
 var wobs: string;
 var wanio:  string;
 var wmes: string;
 var wdia: string;
 var wsegundo: string;
{genero numero de contrato}
  wsap:='SAP-C';
  wanio:=FormatDateTime('yyyy',Date);
  wmes:=FormatDateTime('mm',Date);
  wdia:=FormatDatetime('dd',date);
  wsegundo:=formatdatetime('ss',Time);
  {tomo datos de campo}
  wnocontrato:=trim(wsap+wanio+wmes+wdia+wsegundo);
  wnombre:=trim(fcontribuyente.txtnombre.Text);
  wpaterno:=trim(fcontribuyente.txtpaterno.Text);
  wmaterno:=trim(fcontribuyente.txtmaterno.Text);
  wlocalidad:='SAN MIGUEL XALTIPAN';
  wcalle:= trim(fcontribuyente.txtcalle.Text);
  wnoext:=StrToint(fcontribuyente.txtext.Text);
  wnoint:=StrToint(fcontribuyente.txtint.Text);
  wcp:=StrToint('54090');
  wfechare:=Dfecha.Date;
  wtel:=trim(fcontribuyente.txttel.Text);
  wobs:=trim(fcontribuyente.txtobser.Text);
  {ejecucion}
  fmodulo.Qcontry.Close;
Código SQL [-]
with fmodulo.Qcontry do
    begin
     sql.Clear;
     sql.Add(' insert into sap_contrato (');
     Sql.Add(' nocontrato ,');    //1
     sql.Add(' nombre ,');//2
     sql.Add(' paterno ,');//3
     sql.Add(' materno ,');//4
     sql.Add(' localidad ,');//5
     sql.Add(' calle ,');//6
     sql.Add(' noext ,');//7
     sql.Add(' noint ,');//8
     sql.Add(' cp ,');//9
     sql.Add(' fecharegistro ,');//10
     sql.Add(' telefono ,');//11
     sql.Add(' observacio ');//12
     sql.Add(' ) values (');
     sql.Add(' '+QuotedStr(wnocontrato)+', ');//1
     sql.Add(' '+QuotedStr(wnombre)+', ');//2
     sql.Add(' '+QuotedStr(wpaterno)+', ');//3
     sql.Add(' '+QuotedStr(wmaterno)+', ');//4
     sql.add(' '+QuotedStr(wlocalidad)+', ');//5
     sql.Add(' '+QuotedStr(wcalle)+', ');//6
     sql.Add(' '+IntToStr(wnoint)+', ');//7
     sql.Add(' '+inttostr(wnoext)+', ');//8
     sql.Add(' '+Inttostr(wcp)+', '); //9
     sql.Add(' '+DateToStr(wfechare)+', ');//10
     sql.Add(' '+QuotedStr(wtel)+', ');//11
     sql.Add(' '+QuotedStr(wobs)+')');//12
     fcontribuyente.lvalida.Caption:=fmodulo.Qcontry.SQL.Text;
     try
      ExecSQL;
     except
      showmessage('Problemas con query ['+text+']');
     end;
    end;

aunque valido el texto del Tquery el muestra lo siguiente.
Código SQL [-]
 insert into sap_contrato (
 nocontrato ,
 nombre ,
 paterno ,
 materno ,
 localidad ,
 calle ,
 noext ,
 noint ,
 cp ,
 fecharegistro ,
 telefono ,
 observacio 
 ) values (
 'SAP-C2009111032', 
 'HORACIO', 
 'XOCHITEMOL', 
 'BAUTISTA', 
 'SAN MIGUEL XALTIPAN', 
 'REFORMA', 
 6, 
 6, 
 54090, 
 10/11/2009, 
 '53834328', 
 'INICIO DE CONTRATO')

en el text del tquery la fecha es: 10/11/2009 en la base de datos me inserta 1900-01-01 00:00:00.000

cual creen que sea el problema. espero me puedana yudar

Axel_Tech 10-11-2009 18:45:05

¿Qué motor de base de datos usas? ¿Qué tipo de dato es ese campo? Si es Timestamp cámbialo por Date. También creo que deberías insertar la fecha entre comillas: '10/11/2009' y no 10/11/2009 a secas.

microbiano 10-11-2009 18:51:23

Hola
 
el motor de base de datos es Sql server 2000, ya lo intente entre comillas y aun asi no funciona que me recomiendas?.

el tipo de dato es DateTime.

saludos espero tus comentarios y gracias por tomarte la molestia de contestar

ContraVeneno 11-11-2009 01:48:42

yo te recomendaría utilizar parametros, de esa forma no te tienes que preocupar por hacer conversiones de fechas a texto o viceversa, ni tampo te preocupas por el formato de la fecha o por las comillas:

Código Delphi [-]
with UnADOQuery do begin 
 If active then close; 
 SQL.Clear; 
 SQL.Add('Insert Into Tabla(Numero, Nombre, Fecha)'); 
 SQL.Add('Values(:Numero, :Nombre, :Fecha)'); 
 with Parameters do begin  
  ParamByName('Numero').Value := UnNumero.AsInteger; //asFloat, asCurrency, etc  
  ParamByName('Nombre').Value := UnTexto.AsString;  
  ParamByName('Fecha').Value := UnDateTimePicker.Date; 
 end; //parameters 
 ExecSQL;
end; //with



Y por cierto, sería mucho mejor utilizar un TADOQuery que un TQUery

Axel_Tech 11-11-2009 09:43:15

Prueba utilizando la función Convert de SQL Server y el formato ANSI en la fecha (YYYYMMDD):
Código:

convert(datetime, '20091110', 103)
Con el parámetro 103 te lo guardará como tú quieres: dd/mm/yyyy.

microbiano 12-11-2009 19:26:01

GRacias
 
Gracias ya resolvi el problema pongo el codigo por si a alguien le sirve
Código Delphi [-]
with fmodulo.Qcontry do
    begin
     sql.Clear;
     sql.Add(' insert into sap_contrato (');
     Sql.Add(' nocontrato ,');    //1
     sql.Add(' nombre ,');//2
     sql.Add(' paterno ,');//3
     sql.Add(' materno ,');//4
     sql.Add(' ncompleto, '); //5
     sql.Add(' localidad ,');//6
     sql.Add(' calle ,');//7
     sql.Add(' noext ,');//8
     sql.Add(' noint ,');//9
     sql.Add(' cp ,');//10
     sql.Add(' fecharegistro ,');//111
     sql.Add(' telefono ,');//12
     sql.Add(' observacio, ');//13
     sql.add(' status ');//14
     sql.Add(' ) values (');
     sql.Add(' '+QuotedStr(wnocontrato)+', ');//1
     sql.Add(' '+QuotedStr(wnombre)+', ');//2
     sql.Add(' '+QuotedStr(wpaterno)+', ');//3
     sql.Add(' '+QuotedStr(wmaterno)+', ');//4
     sql.Add(' '+QuotedStr(wcompleto)+', '); //5
     sql.add(' '+QuotedStr(wlocalidad)+', ');//6
     sql.Add(' '+QuotedStr(wcalle)+', ');//7
     sql.Add(' '+IntToStr(wnoint)+', ');//8
     sql.Add(' '+inttostr(wnoext)+', ');//9
     sql.Add(' '+Inttostr(wcp)+', '); //10
     sql.Add(' '+QuotedStr(DateToStr(wfechare))+', ');//11
     sql.Add(' '+QuotedStr(wtel)+', ');//12
     sql.Add(' '+QuotedStr(wobs)+', ');//13
     sql.Add(' '+Inttostr(westatus) +')');//14
     try
      ExecSQL;
      application.MessageBox('Registro Almacenado Correctamente','',mb_ok + mb_iconinformation);
      fcontribuyente.txtcontrato.Text:='';
      fcontribuyente.txtnombre.Text:='';
      fcontribuyente.txtpaterno.Text:='';
      fcontribuyente.txtint.Text:='';
      fcontribuyente.txtext.Text:='';
      fcontribuyente.txtcalle.Text:='';
      fcontribuyente.txttel.Text:='';
      fcontribuyente.txtobser.Text:='';
     except
      showmessage('Problemas con query ['+text+']');
     end;


La franja horaria es GMT +2. Ahora son las 08:40:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi