Z
zxp_ping
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我对BOOK类持久化到数据库的一个演示,各位有空请提出看法。、
unit uBook;
{
================================================================================
* 软件名称:PersistentLearn
* 单元名称:uBook 书籍类
* 单元作者:曾湘平(zxp_ping@163.com)
* 备 注:提供书本类的基础属性和方法
* 开发平台:Windows 2003 + Delphi 7
* 修改记录:2008.03.06 V1.0
* 创建单元,实现功能
================================================================================
}
interface
uses Windows, SysUtils, Classes;
type
{ TBook 书本类 }
TBook = class(TPersistent)
private
//书本编号
iBookID: Integer;
//书本标题
sBookTitle: String;
//作者
sBookAuthor: String;
//......
public
//创建,初始值
constructor Create;
//获取书本编号
function GetID: Integer;
//获取书本标题
function GetTitle: String;
//获取作者姓名
function GetAuthor: String;
//设置书本编号
procedure SetID(const iID: Integer);
//设置书本标题
procedure SetTitle(const sTitle:String);
//设置作者姓名
procedure SetAuthor(const sAuthor:String);
//重写基类虚方法,实现成员赋值
procedure Assign(Source: TPersistent)
override;
end;
implementation
constructor TBook.Create;
begin
inherited Create;
iBookID:= -1;
sBookTitle:= '';
sBookAuthor:= '';
end;
function TBook.GetID: Integer;
begin
Result:= iBookID;
end;
function TBook.GetTitle: String;
begin
Result:= sBookTitle;
end;
function TBook.GetAuthor: String;
begin
Result:= sBookAuthor;
end;
procedure TBook.SetID(const iID: Integer);
begin
iBookID:= iID;
end;
procedure TBook.SetTitle(const sTitle:String);
begin
sBookTitle:= sTitle;
end;
procedure TBook.SetAuthor(const sAuthor:String);
begin
sBookAuthor:= sAuthor;
end;
procedure TBook.Assign(Source: TPersistent);
var
aBook: TBook;
begin
if (Source is TBook) then
begin
aBook:= TBook(Source);
Self.SetID(aBook.GetID);
Self.SetTitle(aBook.GetTitle);
Self.SetAuthor(aBook.GetAuthor);
end
else
inherited Assign(Source);
end;
end.
unit uBookManager;
{
================================================================================
* 软件名称:PersistentLearn
* 单元名称:uBookManager 书籍管理类
* 单元作者:曾湘平(zxp_ping@163.com)
* 备 注:提供书本对象到sqlite的映射,持久化
采用类ORM方式的简化
* 开发平台:Windows 2003 + Delphi 7
* 修改记录:2008.03.06 V1.0
* 创建单元,实现功能
================================================================================
}
interface
uses Windows, SysUtils, Classes, Variants, mksqlite3, uBook;
type
{ TBookManager }
TBookManager = class(TObject)
private
//Sqlite连接
FdbSqlite:tmksqlite;
//Sqlite数据查询
rsBook:IMksqlStmt;
procedure GetData;
public
//传入Sqlite连接进行创建对象
constructor Create(dbSqlite:tmksqlite);
destructor Destroy
override;
//得到所有的Book对象
function GetBooks(var BookList:array of TBook):Integer;
//得到一个Book对象
function GetBook(var aBook:TBook):Integer;
//增加一个Book对象
function AddBook(aBook:TBook):Integer;
//删除一个Book对象
function RemoveBook(aBook:TBook):Integer;
//根据Book ID查询
function LocateBookByID(ID:Integer
var aBook:TBook):Integer;
end;
implementation
constructor TBookManager.Create(dbSqlite:tmksqlite);
begin
inherited Create;
FdbSqlite:= dbSqlite;
GetData;
end;
destructor TBookManager.Destroy;
begin
inherited;
if assigned(rsBook) then
rsBook.close;
end;
function TBookManager.GetBooks(var BookList:array of TBook):Integer;
begin
//暂缓实现
Result:= 0;
end;
procedure TBookManager.GetData;
begin
if not assigned(rsBook) then
begin
//查询数据
rsBook:=FdbSqlite.createStmt;
rsBook.serverCursor:=true;
end;
rsBook.close;
rsBook.open('SELECT bookID,bookTitle,bookAuthor FROM BOOK');
end;
function TBookManager.GetBook(var aBook:TBook):Integer;
var
vID,vTitle,vAuthor:variant;
begin
Result:= -1;
if (not rsBook.eof) then
begin
//反序列化各列,返回对象
//暂不做太复杂机制,直接字段赋值
vID:= rsBook[0];
vTitle:= rsBook[1];
vAuthor:= rsBook[2];
aBook.SetID(Integer(vID));
aBook.SetTitle(varTostr(vTitle));
aBook.SetAuthor(varTostr(vAuthor));
rsBook.next;
Result:= 0;
end;
//重新获取数据
if (rsBook.eof) then
GetData;
end;
function TBookManager.AddBook(aBook:TBook):Integer;
begin
//序列化各属性
//插入一个BOOK对象到数据库
Result:= FdbSqlite._exec(
Format('INSERT INTO BOOK (bookID, bookTitle, bookAuthor) values (%d,''%s'',''%s'')',
[aBook.GetID, aBook.GetTitle, aBook.GetAuthor]));
end;
function TBookManager.RemoveBook(aBook:TBook):Integer;
begin
//删除一个BOOK对象
Result:= FdbSqlite._exec(
Format('Delete From BOOK where bookID=%d', [aBook.GetID]));
end;
function TBookManager.LocateBookByID(ID:Integer
var aBook:TBook):Integer;
begin
//暂缓实现
Result:= 0;
end;
end.
unit uBook;
{
================================================================================
* 软件名称:PersistentLearn
* 单元名称:uBook 书籍类
* 单元作者:曾湘平(zxp_ping@163.com)
* 备 注:提供书本类的基础属性和方法
* 开发平台:Windows 2003 + Delphi 7
* 修改记录:2008.03.06 V1.0
* 创建单元,实现功能
================================================================================
}
interface
uses Windows, SysUtils, Classes;
type
{ TBook 书本类 }
TBook = class(TPersistent)
private
//书本编号
iBookID: Integer;
//书本标题
sBookTitle: String;
//作者
sBookAuthor: String;
//......
public
//创建,初始值
constructor Create;
//获取书本编号
function GetID: Integer;
//获取书本标题
function GetTitle: String;
//获取作者姓名
function GetAuthor: String;
//设置书本编号
procedure SetID(const iID: Integer);
//设置书本标题
procedure SetTitle(const sTitle:String);
//设置作者姓名
procedure SetAuthor(const sAuthor:String);
//重写基类虚方法,实现成员赋值
procedure Assign(Source: TPersistent)
override;
end;
implementation
constructor TBook.Create;
begin
inherited Create;
iBookID:= -1;
sBookTitle:= '';
sBookAuthor:= '';
end;
function TBook.GetID: Integer;
begin
Result:= iBookID;
end;
function TBook.GetTitle: String;
begin
Result:= sBookTitle;
end;
function TBook.GetAuthor: String;
begin
Result:= sBookAuthor;
end;
procedure TBook.SetID(const iID: Integer);
begin
iBookID:= iID;
end;
procedure TBook.SetTitle(const sTitle:String);
begin
sBookTitle:= sTitle;
end;
procedure TBook.SetAuthor(const sAuthor:String);
begin
sBookAuthor:= sAuthor;
end;
procedure TBook.Assign(Source: TPersistent);
var
aBook: TBook;
begin
if (Source is TBook) then
begin
aBook:= TBook(Source);
Self.SetID(aBook.GetID);
Self.SetTitle(aBook.GetTitle);
Self.SetAuthor(aBook.GetAuthor);
end
else
inherited Assign(Source);
end;
end.
unit uBookManager;
{
================================================================================
* 软件名称:PersistentLearn
* 单元名称:uBookManager 书籍管理类
* 单元作者:曾湘平(zxp_ping@163.com)
* 备 注:提供书本对象到sqlite的映射,持久化
采用类ORM方式的简化
* 开发平台:Windows 2003 + Delphi 7
* 修改记录:2008.03.06 V1.0
* 创建单元,实现功能
================================================================================
}
interface
uses Windows, SysUtils, Classes, Variants, mksqlite3, uBook;
type
{ TBookManager }
TBookManager = class(TObject)
private
//Sqlite连接
FdbSqlite:tmksqlite;
//Sqlite数据查询
rsBook:IMksqlStmt;
procedure GetData;
public
//传入Sqlite连接进行创建对象
constructor Create(dbSqlite:tmksqlite);
destructor Destroy
override;
//得到所有的Book对象
function GetBooks(var BookList:array of TBook):Integer;
//得到一个Book对象
function GetBook(var aBook:TBook):Integer;
//增加一个Book对象
function AddBook(aBook:TBook):Integer;
//删除一个Book对象
function RemoveBook(aBook:TBook):Integer;
//根据Book ID查询
function LocateBookByID(ID:Integer
var aBook:TBook):Integer;
end;
implementation
constructor TBookManager.Create(dbSqlite:tmksqlite);
begin
inherited Create;
FdbSqlite:= dbSqlite;
GetData;
end;
destructor TBookManager.Destroy;
begin
inherited;
if assigned(rsBook) then
rsBook.close;
end;
function TBookManager.GetBooks(var BookList:array of TBook):Integer;
begin
//暂缓实现
Result:= 0;
end;
procedure TBookManager.GetData;
begin
if not assigned(rsBook) then
begin
//查询数据
rsBook:=FdbSqlite.createStmt;
rsBook.serverCursor:=true;
end;
rsBook.close;
rsBook.open('SELECT bookID,bookTitle,bookAuthor FROM BOOK');
end;
function TBookManager.GetBook(var aBook:TBook):Integer;
var
vID,vTitle,vAuthor:variant;
begin
Result:= -1;
if (not rsBook.eof) then
begin
//反序列化各列,返回对象
//暂不做太复杂机制,直接字段赋值
vID:= rsBook[0];
vTitle:= rsBook[1];
vAuthor:= rsBook[2];
aBook.SetID(Integer(vID));
aBook.SetTitle(varTostr(vTitle));
aBook.SetAuthor(varTostr(vAuthor));
rsBook.next;
Result:= 0;
end;
//重新获取数据
if (rsBook.eof) then
GetData;
end;
function TBookManager.AddBook(aBook:TBook):Integer;
begin
//序列化各属性
//插入一个BOOK对象到数据库
Result:= FdbSqlite._exec(
Format('INSERT INTO BOOK (bookID, bookTitle, bookAuthor) values (%d,''%s'',''%s'')',
[aBook.GetID, aBook.GetTitle, aBook.GetAuthor]));
end;
function TBookManager.RemoveBook(aBook:TBook):Integer;
begin
//删除一个BOOK对象
Result:= FdbSqlite._exec(
Format('Delete From BOOK where bookID=%d', [aBook.GetID]));
end;
function TBookManager.LocateBookByID(ID:Integer
var aBook:TBook):Integer;
begin
//暂缓实现
Result:= 0;
end;
end.