码迷,mamicode.com
首页 > 数据库 > 详细

Delphi7中不用控件连接sqlite

时间:2014-12-22 13:03:34      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

DELPHI 7 没有办法使用lib文件,这就有点麻烦,但是咱们也没有必要一个一个Query,那样太痛苦了。我当时就好痛苦。咱们可以借助第三方的包装类,有一些是收费的,但是也有一些免费的,具体的信息大家请查看下面的地址 http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers

我这里使用的是sqlite simple delphi 这是一个完全免费的包装类,大家可以到它的官方网站下载,http://www.itwriting.com/sqlitesimple.php

下来以后是一个压缩包里面是一个demo。咱们需要里面的两个文件。SQLite3.pas 跟 SQLiteTable3.pas ,把他们复制到工程目录下面,然后在需要操作库的单元文件里面 uses SQLiteTable3 ,然后大家请查看官方的事例代码


procedure TForm1.btnTestClick(Sender: TObject);

var

slDBpath: string;
sldb: TSQLiteDatabase;
sltb: TSQLIteTable;
sSQL: String;
Notes: String;
begin
slDBPath := ExtractFilepath(application.exename)+‘test.db‘;
sldb := TSQLiteDatabase.Create(slDBPath);
try
if sldb.TableExists(‘testTable‘) then 
begin
sSQL := ‘DROP TABLE testtable‘;
sldb.execsql(sSQL);
end;
sSQL := ‘CREATE TABLE testtable ([ID] INTEGER PRIMARY KEY,[OtherID] INTEGER NULL,‘;
sSQL := sSQL + ‘[Name] VARCHAR (255),[Number] FLOAT, [notes] BLOB, [picture] BLOB COLLATE NOCASE);‘;
sldb.execsql(sSQL);
sldb.execsql(‘CREATE INDEX TestTableName ON [testtable]([Name]);‘);

//begin a transaction

sldb.BeginTransaction;

sSQL := ‘INSERT INTO testtable(Name,OtherID,Number,Notes) VALUES ("Some Name",4,587.6594,"Here are some notes");‘;

//do the insert

sldb.ExecSQL(sSQL);

sSQL := ‘INSERT INTO testtable(Name,OtherID,Number,Notes) VALUES ("Another Name",12,4758.3265,"More notes");‘;

//do the insert

sldb.ExecSQL(sSQL);


//end the transaction

sldb.Commit;


//query the data

sltb := slDb.GetTable(‘SELECT * FROM testtable‘);

try


if sltb.Count > 0 then

begin

//display first row


ebName.Text := sltb.FieldAsString(sltb.FieldIndex[‘Name‘]);

ebID.Text := inttostr(sltb.FieldAsInteger(sltb.FieldIndex[‘ID‘]));

ebNumber.Text := floattostr( sltb.FieldAsDouble(sltb.FieldIndex[‘Number‘]));

Notes := sltb.FieldAsBlobText(sltb.FieldIndex[‘Notes‘]);

memNotes.Text := notes;


end;


finally

sltb.Free;

end;


finally

sldb.Free;


end;


end;


这样操作起来是不是很简单,^_^


<4> PHP调用SQLite3 ,这个不用我多废话,请参看我写的《通过Apache + PHP5 + PDO 连接 SQLite3 数据库》一文。


<5>Visual Basic , 因为我没有仔细研究,我无发言权,请参看SQLite官方的资料


http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers


COM Wrappers / Visual Basic DLLs部分

 
<6>JAVA 请参看http://www.loveunix.net/bbs/index.php?showtopic=35051 帖子,讲述的比较详细。

 第二部分 使用事务写库

 事务的具体理论概念我不想多说了,如果不明白的请大家到GOOGLE搜索关键字“数据库事务”,能找到N多文章资料。我们使用VC++来做事例。还有使用到上面的两个函数

 void WriteSqlite()

{

try

{

OpenSqlite();

execSQL("begin transaction;"); \\开始一个事务,记录操作

char strSQL[MAX_PATH];

sprintf(strSQL,"INSERT INTO Table_Name (nAction1, nAction2, nAction3) VALUES(%d,%d,%d)", szVar1,szVar2, szVar3,);

execSQL(strSQL);

execSQL("commit transaction;"); \\执行提交之前数据库自动保存用户对数据库操作序列。

return;

}

catch(...)

{

execSQL("rollback transaction;");

}

//Add custom code


}

 
本文至此而完,上述代码均在 Windows2000 Pro + VC++6 英文企业版本 + Delphi7 英文企业版 编译通过


Delphi7中不用控件连接sqlite

标签:

原文地址:http://my.oschina.net/u/1777508/blog/359149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!