本人初学,请教!如何在本地数据库中以日期时间查找记录...(50分)

  • 主题发起人 xminsong
  • 开始时间
X

xminsong

Unregistered / Unconfirmed
GUEST, unregistred user!
索引字段为日期时间类型,数据类型Paradox,谢谢!!
 
select * from tab where dt>=#2002/2/2# and dt<=#2002/3/2#
 
对不起,是我没讲清楚,我只是以文件连接,不用SQL。
 
你想直接读paradox吗? 最好的办法还是用SQL

begin
Query1.Close;
Query1.SQL.Clear;
Query1.SQL.Add('Select * from Table1 where 日期字段1 between :ADate and :BDate');
Query1.Params[0].AsDate = StrToInt(Edit1.Text);
Query1.Params[2].AsDate = StrToInt(Edit2.Text);
Query1.Prepare;
Query1.Open;
end;

1、查询具体日期,例如
select * from people.db where birthday>="6/19/1999"
即日期型表示须为月/日/年
2、查询年、月、日之类
select * from people.db where extract(year from birthdate)="1999"
select * from people.db where extract(month from birthdate)="6"

假如你的查询参数是可变的,即日期不固定,那么需要编个函数来件日期转为上文所
说的格式,可以这样:

//转换日期函数
function mydatetostr(indate: tdate): string;
var
year, month, day: word;
begin
decodedate(indate, year, month, day);
result :='"'+ inttostr(month) + '/' + inttostr(day) + '/' +
inttostr(year)+'"';
end;

//获取转换年的函数
function mygetyeartostr(indate: tdate): string;
var
year, month, day: word;
begin
decodedate(indate, year, month, day);
result := inttostr(year);
end;

1、假设要比较的日期由datetimepicker给出,则查询语句可以这样:
with query1 do
begin
close;
sql.Clear;
sql.Add('select * from people.db where birthdate>='+
mydatetostr(datetimepicker1.date));
open;
end;

2、假设要比较的年份由datetimepicker的年份给出,则查询语句可以这样:
with query1 do
begin
close;
sql.Clear;
sql.Add('select * from people.db where
extract(year from birthdate)>='+
mygetyeartostr(datetimepicker1.date));
open;
end;

 
我要记录的数据很少,不想用SQL,用FindKey我又无法搞定,请帮帮忙!谢谢!
 
function Locate(const KeyFields: String;
const KeyValues: Variant; Options: TLocateOptions): Boolean;
例:
with Table1 do
Locate('dt',StrToDateTime(Edit1.Text),[]);
 
请问delphi有没有专门的函数集?在那里可以买到?
 
我的问题用笨方法解决了,我先把数据库里的DateTime数据换成字符串,再比较。
虽然在这次没得到我想要的答案,但确实学到不少知识,谢谢htw、poor Liu、ysai
的帮忙,特别多谢htw,三位都有分,谢谢!!
 
多人接受答案了。
 
顶部