这段代码错在哪?编了一天的程序,这么简单的地方都糊涂了。

黑兵 2005-01-28 08:45:53
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
a1 = Date.SubString(1,4);
a2 = Date.SubString(6,2);
a3 = Date.SubString(9,2);
s="select * from lygw_td where Year(进店时间)='"+a1+"' and Month(进店时间)='"+a2+"' and Day(进店时间)='"+a3+"'";
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Active=True;
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
//
注:在数据库中进店时间的格式是:2005-01-28 13:17:37
提示错误:year(进店时间)='2005'
...全文
171 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
Winsky 2005-02-01
  • 打赏
  • 举报
回复
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
a1 = Date.SubString(1,4);
★a2 = StrToInt(Date.SubString(6,2));//转换成整数,去掉前头的0。比如是2月,不是02月
★a3 = StrToInt(Date.SubString(9,2));//转换成整数,去掉前头的0。比如是2月,不是02月
★s="select * from 进店时间 where Year(进店时间)="+a1+" and Month(进店时间)="+a2+" and Day(进店时间)="+a3+"";//去a1,a2,a3两端的单引号,SQL2000中Year,Month,Day是整形,不是字符型
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Open();
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
Winsky 2005-02-01
  • 打赏
  • 举报
回复
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
a1 = Date.SubString(1,4);
★a2 = StrToInt(Date.SubString(6,2));//转换成整数,去掉前头的0。比如是2月,不是02月
★a3 = StrToInt(Date.SubString(9,2));//转换成整数,去掉前头的0。比如是2月,不是02月
★s="select * from revmain where Year(revdate)="+a1+" and Month(revdate)="+a2+" and Day(revdate)="+a3+"";//去a1,a2,a3两端的单引号,SQL2000中Year,Month,Day是整形,不是字符型
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Open();
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
pcclever 2005-02-01
  • 打赏
  • 举报
回复
Date本来就是一个函数,不要用:String Date!!!
你可以试一下:

AnsiString today;
today=Date();
ShowMessage(today);


SubString(i,j)取一般的字符串还可以,但是“yyyy-mm-dd”格式的就不行了,最好用char进行处理。
黑兵 2005-01-31
  • 打赏
  • 举报
回复
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
int aa1,aa2,aa3;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
ShowMessage(Date);
a1 = Date.SubString(1,4);
aa1=StrToInt(a1);
a2 = Date.SubString(6,2);
aa2=StrToInt(a2);
a3 = Date.SubString(9,2);
aa3=StrToInt(a3);
s="select * from lygw_td where Year(进店时间)="+aa1+" and Month(进店时间)="+aa2+" and Day(进店时间)="+aa3;
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Active=True;
if(Query1->Eof)
{
Application->MessageBox("当前没有在店的团队!","系统提示", MB_OK);
return;
}
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
//编译都编译不过去
[C++ Error] Unit5.cpp(41): E2085 Invalid pointer addition
[C++ Warning] Unit5.cpp(51): W8004 'aa3' is assigned a value that is never used
[C++ Warning] Unit5.cpp(51): W8004 'aa2' is assigned a value that is never used
[C++ Warning] Unit5.cpp(51): W8004 'aa1' is assigned a value that is never used
是不是我的BCB6出毛病了?
可以以下代码在同一情况的另一个窗体里好用啊:
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
a1 = Date.SubString(1,4);
a2 = Date.SubString(6,2);
a3 = Date.SubString(9,2);
s="select * from lygw_td where Year(进店时间)='"+a1+"' and Month(进店时间)='"+a2+"' and Day(进店时间)='"+a3+"'";
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Active=True;
if(Query1->Eof)
{
Application->MessageBox("当前没有在店的团队!","系统提示", MB_OK);
return;
}
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
黑兵 2005-01-31
  • 打赏
  • 举报
回复
我用的是sql server2000
youhuai 2005-01-31
  • 打赏
  • 举报
回复
严重同意叶子和快意客的观点!year()函数的返回值是数值型的,不是文本型的,把'符号全去掉就没问题了.
哈哈,但要注意你使用的数据库系统!
黑兵 2005-01-31
  • 打赏
  • 举报
回复
s="select * from lygw_td where 进店时间='"+FormatDateTime("yyyy'-'mm'-'dd", DateTimePicker1->Date)+"'";
查不到任何数据。因为进店时间中有时间部分。怎么办?也就是说,我就是想查找某来来店购物的,而不管他是这天几点几分来的。只要是这一天的就查找出来。可“进店时间”是DateTime型,自动加入了时间啊。
hchile 2005-01-31
  • 打赏
  • 举报
回复
s="select * from lygw_td where 进店时间='"+FormatDateTime("yyyy'-'mm'-'dd", DateTimePicker1->Date)+"'";
hchile 2005-01-31
  • 打赏
  • 举报
回复
s="select * from lygw_td where 进店时间='"+FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date)+"'";
黑兵 2005-01-31
  • 打赏
  • 举报
回复
按照mli0080(leslie)的改法,改完后。错误如下:
Project Project1.exe raised exception class EDBEngineError with message 'Invalid use of keyword. Token:Year(进店时间)='2005' Line Number:'1'.Process stopped. Use Step or Run to continue.
黑兵 2005-01-31
  • 打赏
  • 举报
回复
有没有人知道有什么办法?
benjamincao 2005-01-29
  • 打赏
  • 举报
回复
楼上说的对 而且你没有说是什么错误啊 也可能数据库的格式和你调用时候用的格式不一致也可能报错的 如果你按照楼上说的做 结果还是报错的哈 你就有可能是第二种情况了啊!
quickeer 2005-01-29
  • 打赏
  • 举报
回复
问题是:Year(日期)函数的结果是数值,不是字符串,所以不用加"'"号.
楼上的方法正确!
我来看看CB 2005-01-29
  • 打赏
  • 举报
回复
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);

改成

Date = FormatDateTime("yyyy'-'mm'-'dd", DateTimePicker1->Date);
mli0080 2005-01-29
  • 打赏
  • 举报
回复
还有你的数据库是使用什么数据库呢,表的字段是不是year(进店时间),还是year呢,如果字段是时间类型,在oracle中需要使用to_Date这个函数,如果字段是数值型就不要加"'"了。。。。。。。。。。
mli0080 2005-01-29
  • 打赏
  • 举报
回复
兄弟把你的语句改成这个吧!执行前看一看s的值吧
try
{
Word nYear,nMonth,nDay,nHour,nMinute,nSecond,nMSecond;
TDateTime GetDate,GetTime;
GetDate = DateTimePicker1->Date;
DecodeDate(GetDate,nYear,nMonth,nDay);
DecodeTime(GetDate,nHour,nMinute,nSecond,nMSecond);
AnsiString s="select * from lygw_td where Year(进店时间)='"+IntToStr(nYear)+"'
and Month(进店时间)='"+IntToStr(nMonth)+"' and Day(进店时间)='"+IntToStr(nDay)+"'";
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Active=True;
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
jackyjian 2005-01-29
  • 打赏
  • 举报
回复
a1 = Date.SubString(1,4)->a1 = Date.SubString(0,4);,难道Substring()下标从1开始吗?我记得好像是0吧.
黑兵 2005-01-29
  • 打赏
  • 举报
回复
try
{
AnsiString s,ss,a1,a2,a3;
String Date;
Date = FormatDateTime("yyyy-mm-dd", DateTimePicker1->Date);
a1 = Date.SubString(1,4);
a2 = Date.SubString(6,2);
a3 = Date.SubString(9,2);
//s="select * from lygw_td where year(进店时间)='"+a1+"' and month(进店时间)='"+a2+"' and day(进店时间)='"+a3+"'";
s="select * from lygw_td where year(进店时间)="+a1+" and month(进店时间)="+a2+" and day(进店时间)="+a3;
Query1->Active=False;
Query1->SQL->Clear();
Query1->SQL->Add(s);
Query1->Active=True;
}
catch(...)
{
Application->MessageBox("发生错误!","系统提示", MB_OK);
return;
}
故障依旧。唉
叶子哟 2005-01-28
  • 打赏
  • 举报
回复
="select * from lygw_td where Year(进店时间)='"+a1+"' and Month(进店时间)='"+a2+"' and Day(进店时间)='"+a3+"'";

把这句里的'全部去掉就行了
黑兵 2005-01-28
  • 打赏
  • 举报
回复
日期用的是日期时间格式
加载更多回复(2)

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧