有一段时间没正经的用delphi了,前两天下载了Delphi2010,用它来找找Delphi的感觉,把写的一些东西共享出来,大家共同学习一下,呵呵。
我想做一个双色球的分析软件,以下是其中实现的部分类的单元文件,其他文件后续一点一点的补齐,有不妥的地方,希望大家指正,谢谢啦!
在使用Delphi2010是,发现个问题,在用show函数调用窗体时,主窗口总是在子窗口的下面。
{工具类单元,包括一些用到的辅助类定义}
unit uCustomUtils;
interface
uses
Classes;
type
TLottery = packed record
{红球集合}
RedBalls: TStringList;
{篮球}
BlueBall: string;
{开奖期 }
Period: string;
{开奖时间}
Time: string;
{奇偶数比}
OddEvenCompare: string;
{大小比}
BigSmallCompare: string;
{和值}
Sum: string;
end;
pLottery = ^TLottery;
implementation
end.
{数据库辅助单元,通过这个类去与数据库打交道,因为当前用到的是Access数据库,}
{所以继承了一个Access的类,其他数据库,可以再另行实现}
unit uDBHelper;
interface
uses
Classes, ADODB, SysUtils, Windows,
uCustomUtils;
const
CONNECTION_STRING_ACCESS = ‘Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False’;
type
TDBHelper = class
private
FDBFilePath: string;
FCommandText: string;
FConnection: TADOConnection;
public
{执行Insert,Delete,Update操作}
procedure Execute; virtual; abstract;
{执行select操作}
procedure SelectExecute; virtual; abstract;
{打开数据库连接}
procedure Open; virtual; abstract;
{关闭数据库连接}
procedure Close; virtual; abstract;
{构造函数}
constructor Create(dbfilePath: string); virtual;
{析构函数}
destructor Destory; virtual;
public
property DBFilePath: string read FDBFilePath;
property CommandText: string read FCommandText write FCommandText;
end;
TAccessHelper = class(TDBHelper)
private
FLotterys: TList;
public
procedure Execute; override;
procedure SelectExecute; override;
procedure Open; override;
procedure Close; override;
constructor Create(dbfilePath: string); override;
destructor Destroy; override;
end;
implementation
{ TDBHelper }
constructor TDBHelper.Create(dbfilePath: string);
begin
FConnection := TADOConnection.Create(nil);
FDBFilePath := dbfilePath;
end;
destructor TDBHelper.Destory;
begin
FreeAndNil(FConnection);
end;
{ TAccessHelper }
procedure TAccessHelper.Close;
begin
if FConnection.Connected then
FConnection.Close;
end;
constructor TAccessHelper.Create(dbfilePath: string);
var
dbfile: string;
begin
inherited;
FLotterys := TList.Create;
try
dbfile := Format(CONNECTION_STRING_ACCESS, [FDBFilePath]);
FConnection.ConnectionString := dbfile;
FConnection.LoginPrompt := False;
FConnection.Open;
except
on E:Exception do
MessageBox(0, Pchar(‘Open Database error.’), Pchar(”), MB_OK+MB_ICONINFORMATION)
end;
end;
destructor TAccessHelper.Destroy;
begin
FConnection.Close;
FreeAndNil(FLotterys);
inherited;
end;
procedure TAccessHelper.Execute;
var
qry: TADOQuery;
begin
qry := TADOQuery.Create(nil);
try
with qry do
begin
Connection := FConnection;
SQL.Clear;
SQL.Add(FCommandText);
ExecSQL;
end;
finally
qry.Free;
end;
end;
procedure TAccessHelper.Open;
begin
FConnection.Open;
end;
procedure TAccessHelper.SelectExecute;
var
qry: TADOQuery;
item: TLottery;
i: Integer;
strRed: string;
begin
qry := TADOQuery.Create(nil);
try
with qry do
begin
Connection := FConnection;
SQL.Clear;
SQL.Add(FCommandText);
Open;
//遍历记录部分
First;
while not Eof do
begin
item.Period := FieldByName(‘period’).AsString;
item.BlueBall := FieldByName(‘blue’).AsString;
item.Time := FieldByName(‘time’).AsString;
item.OddEvenCompare := FieldByName(‘oddeven’).AsString;
item.BigSmallCompare := FieldByName(‘bigsmall’).AsString;
item.Sum := FieldByName(‘sum’).AsString;
item.RedBalls := TStringList.Create;
for i := 1 to 6 do
begin
strRed := ‘red’+IntToStr(i);
item.RedBalls.Add(FieldByName(strRed).AsString);
end;
FLotterys.Add(@item);
Next;
end;
end;
finally
qry.Free;
end;
end;
end.
{彩票的处理单元,包括录入,查询}
unit uLotterys;
interface
uses
Classes, SysUtils,
uCustomUtils, uDBHelper;
type
TLotteryManager = class
private
FLotterys: TList;
FDBHelper: TAccessHelper;
{格式化TLottery类型为字符串}
{%s,%s,%s…}
{期 ,时间,红球,蓝球,奇偶比,大小比,和值}
function FormatLottery(const lottery: TLottery): string;
public
constructor Create(DBFilePath: string);
destructor Destory;
{添加单独一个中奖记录}
procedure ImportSingle(const lottery: TLottery; const tableName: string);
{批量添加中奖纪录}
{filePath: 要批量导入的数据文件}
procedure ImportMultiple(const items: TList; const tabelName: string);
{查询一定数量的中奖记录, 默认搜索全部s}
procedure Search( const tableName: string; const count: Integer = 0);
{现在只用于在查询返回结果时使用}
property Lotterys: TList read FLotterys;
end;
implementation
{ TLotteryManager }
constructor TLotteryManager.Create(DBFilePath: string);
begin
FLotterys := TList.Create;
FDBHelper := TAccessHelper.Create(DBFilePath);
end;
destructor TLotteryManager.Destory;
begin
FreeAndNil(FDBHelper);
FreeAndNil(FLotterys);
end;
function TLotteryManager.FormatLottery(const lottery: TLottery): string;
const
FORMAT_STR = ‘%s,%s’;
var
msg: string;
i: Integer;
begin
msg := lottery.Period;
msg := Format(FORMAT_STR, [msg, lottery.Time]);
for i := 0 to 5 do
begin
msg := Format(FORMAT_STR, [msg, lottery.RedBalls[i]]);
end;
msg := Format(FORMAT_STR, [msg, lottery.BlueBall]);
msg := Format(FORMAT_STR, [msg, lottery.OddEvenCompare]);
msg := Format(FORMAT_STR, [msg, lottery.BigSmallCompare]);
msg := Format(FORMAT_STR, [msg, lottery.Sum]);
end;
procedure TLotteryManager.ImportMultiple(const items: TList; const tabelName: string);
var
item: pLottery;
i: Integer;
begin
for i := 0 to items.Count – 1 do
begin
item := pLottery(items.Items[i]);
ImportSingle(item^, tabelName);
end;
end;
procedure TLotteryManager.ImportSingle(const lottery: TLottery; const tableName: string);
const
INSERT_STR = ‘insert into [%s] values(%s)’;
var
cmdText: string;
begin
cmdText := Format(INSERT_STR, [tableName, FormatLottery(lottery)]);
FDBHelper.CommandText := cmdText;
FDBHelper.Execute;
end;
procedure TLotteryManager.Search(const tableName: string; const count: Integer);
const
SELECT_STR = ‘select %s * from [%s]’;
var
cmdText: string;
begin
if count = 0 then
begin
cmdText := Format(SELECT_STR, [”,tableName]);
end
else begin
cmdText := Format(SELECT_STR, [IntToStr(count), tableName]);
end;
FDBHelper.CommandText := cmdText;
FDBHelper.Open;
end;
end.
文章知识点与官方知识档案匹配,可进一步学习相关知识MySQL入门技能树首页概览31292 人正在系统学习中
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!