SQLite
SQLite是一个老牌的轻量级别的本地文件数据库,完全免费且开源,不需要安装,无须任何配置,当然,这样管理功能就不是很强大了,但是它的主要应用也是在本地数据库,可以说是最简单好用的嵌入式本地数据库了。
SQLite只要一个DLL就可以实现全部功能。SQLite不需要数据库引擎,只有一个数据文件,占用系统资源非常少,很适合做Demo或小型应用。
同时,SQLite也是关系型数据库,支持大部分SQL语句。它支持事务机制和blob数据类型,支持大部分SQL92标准,最大支持数据库到2T。
它还有Python、Tcl、PHP、Java的绑定,这些语言可以直接使用SQLite数据库,因为它们自身包含了支持;还有ODBC接口,非常方便使用。
SQLite的一些基本操作跟SQL很类似,基本上有SQL基础的都能看明白。
SQLite的图像查看工具有很多,比如SQLiteSpy、SQLiteBrowser等。
SQLite默认是utf8编码,使用pragma encoding可以看出数据库的编码。
建立数据库后,可以直接输入“pragma encoding = UTF8/UTF16”来改变编码,但数据库有了数据以后,编码是不可以修改的。
SQLite的源码可以http://www.sqlite.org获得。关于SQLite的更进一步的语法和信息,请参http://www.sqlite.com.cn/http://www.sqlitecn.org。
delphi中使用sqlite3
这里有一个delphi中使用sqlite3的demo:http://www.itwriting.com/blog/articles/a-simple-delphi-wrapper-for-sqlite-3/comment-page-2
这个demo中包含了sqlite3.pas,sqlite3table.pas,sqlite.dll三个文件,里面包含了操作sqlite3的源代码,利用这三个文件,就不需要第三方组件了
添加步骤:
- 将simple sqlite 3.0 for delphi 中的 sqlite3.pas,sqlite3table.pas拷贝至工程所在的文件夹。并在工程中添加这两个个文件。
- 拷贝 sqlite.dll到编译生成exe文件的文件夹。这个根据个人的设定。
初步测试
- 引用SQLiteTable3.pas单元。
- 在窗体上创建一个叫btnVersion的按钮(Tbutton)。在btnVersion的click事件中写入下面的代码。
procedure TfrmAbout.btnVersionClick(Sender: TObject); var SqliteDB :TSQLiteDatabase; begin SqliteDB :=TSQLiteDatabase.Create(''); showmessage('Sqlite dll version:'+SqliteDb.version); SqliteDB.Free; end;
编译运行,成功的话,将会显示当前的sqlite dll的版本号
简单工作原理描述
在simple sqlite3.0 for delphi的几个文件中,主要用到两个文件。分别是sqlite3.pas,sqlite3table.pas。
- sqlite3.pas实现sqlite.dll的接口外部定义。
- sqlite3table.pas进行简单的访问函数封装。
在delphi中,通过sqlite3table.pas来实现对sqlite数据库的各种访问。
读写数据
有一个叫做test.db的sqlite数据库文件,在编译生成的exe文件所在的目录。里面有一个叫做testTable的表格。
下面是根据demo提练出来的读写语句:
unit TestSqlite; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SQLiteTable3; type TForm1 = class(TForm) lbl1: TLabel; ebID: TEdit; ebName: TEdit; lbl2: TLabel; lbl3: TLabel; ebNumber: TEdit; memNotes: TMemo; btnTest: TButton; procedure btnTestClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} 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 //display second row //sltb.Next; 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; end.
转载请注明:落伍老站长 » 【delphi】delphi操作sqlite3