delphi Sqlite

Delphi中SQLite如何读写二进制字段(Blob类型)

在Delphi中,有大量的组件可以操作SQLite数据库,如UniDAC就是其中一个比较优秀的,当然还有ASQLite3Components,也有SQLite3版的ODBC驱动,可直接用ADO操作。本文简要说明SynopseSQLite3读写二进制字段,先说下SynopseSQLite3的优点,静态编译集成SQLite3引擎,不需要额外的DLL支持,支持SQLite3加密,支持JSON表,支持网络版的SQLite3.支持线程安全保护.
首先建一个表,字段类型为BLOB
SL.Add('CREATE TABLE IF NOT EXISTS TEST (');
SL.Add('FileName TEXT PRIMARY KEY,');
SL.Add('Data BLOB);');
SL.Add('');
写入二进制数据:

var
FFileStream:TFileStream;
FData:array of Char;
FSQLR:TSQLRequest;
ASQL:AnsiString;
begin
FFileStream:=TFileStream.Create('test.xml',fmOpenReadWrite);
ASQL:='INSERT INTO TEST(FileName,Data) VALUES('+QuotedStr('test.xml')+',?)';
try
SQLite数据库对象.Execute('DELETE FROM TEST');
//
SetLength(FData,FFileStream.Size);
FFileStream.Position:=0;
FFileStream.Read(PChar(FData)^,Length(FData));
FSQLR.Prepare(SQLite数据库对象.DB,ASQL);
FSQLR.Bind(1,PChar(FData),Length(FData));
FSQLR.Execute;
finally
FreeAndNil(FFileStream);
end;
View Code

其中问号是参数,SQLite数据库对象类型为TSQLDataBase
读取数据并写入到文件:

var
FBlobField:TSQLBlobStream;
FFileStream:TMemoryStream;
FData:array of Char;
begin
try
FFileStream:=TMemoryStream.Create;
FBlobField:=SQLite数据库对象.Blob('','TEST','Data',1,True);
try
FBlobField.Position:=0;
SetLength(FData,FBlobField.Size);
FBlobField.ReadBuffer(PChar(FData)^,FBlobField.Size);
FFileStream.Write(PChar(FData)^,FBlobField.Size);
FFileStream.SaveToFile("test.xml");
finally
FreeAndNil(FBlobField);
FreeAndNil(FFileStream);
end;
except
Result:='';
end;
View Code

转载请注明来源,谢谢,

 

Delphi連接SQLite3 SQLite ODBC Driver

最近在嘗試在Delphi中使用SQLite,昨晚終於找到一個連接SQLite的方法:安裝 SQLite ODBC Driver 在 Delphi中通過ADO組件(TADOQuery, TADOConnection)直接訪問。
步驟:
1. 下載 SQLite ODBC Driver;
2. 安裝 SQLite ODBC Driver;
3. 在Delphi工程中添加 TADOQuery, TADOConnection 組件;
4. 設置 TADOConnection 的ConnectionString;
設置步驟:
1)單擊TADOConnection組件 ConectionString變的按鈕, 選擇 "Use Connection String" -> "Build"
2)彈出的菜單中, 程序選擇: "Microsoft OLE DB Provider for ODBC Drivers"
3)指定的數據源: 選"使用數據源名稱"->"SQLite3 Datasource"
4)測試連接是否成功。

(over)

// 創建表 test
procedure TForm1.Bt_createClick(Sender: TObject);
begin
try
if cnnSqlite.Connected=false then cnnSqlite.open;
if sQry.Active then sQry.Close;
sQry.SQL.Clear;
sQry.SQL.Add('create TABLE test (testid INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, title string unique, text text)');
sQry.ExecSQL;
finally
cnnSqlite.Close;
end;
showMessage('over');
end;
View Code

SQLite加密 wxSqlite3

一直在网上搜wxSqlite3的文档,但是总找不到能真正解决问题的,就是一个简单的编译wxSqlite3自带的示例也出了老多问题,后来却发现,其实wxSqlite3的readme中已经有了详细的方法,哦,就在眼皮子底下!为了避免再一次的遗忘,就在这里暂作一个记录吧。


截至记录时间,wxSqlite3的版本号为2.1.1,Sqlite3的版本为3.7.6.2,这两个都可以直接在网上下载,
wxSqlite3的站点http://wxcode.sourceforge.net/components/wxsqlite3;
http://sourceforge.net/projects/wxcode/files/Components/wxSQLite3/
http://sourceforge.net/projects/wxsqlite/?source=directory
Sqlite3的站点http://www.sqlite.org/;

其中wxSqlite3自带了已编译的Sqlite3.7.6.1的DLL,当然,我的目的是自己编译静态的Lib,所以只能自己下来重新编译了。当然,我要的Lib是要带加密功能的,呵呵。

 

wxSQLite3 is a C++ wrapper around the public domain SQLite 3.x database and is specifically designed for use in programs based on the wxWidgets library.

wxSQLite3 does not try to hide the underlying database, in contrary almost all special features of the current SQLite version 3.7.10 are supported, like for example the creation of user defined scalar or aggregate functions. Since SQLite stores strings in UTF-8 encoding, the wxSQLite3 methods provide automatic conversion between wxStrings and UTF-8 strings. This works best for the Unicode builds of wxWidgets. In ANSI builds the current locale conversion object (wxConvCurrent) is used for conversion to/from UTF-8. Special care has to be taken if external administration tools are used to modify the database contents, since not all of these tools operate in Unicode resp. UTF-8 mode.

Since version 1.7.0 optional support for key based database encryption (128 bit AES) is also included. Starting with version 1.9.6 of wxSQLite3 the encryption extension is compatible with the SQLite amalgamation source and includes the extension functions module. Support for 256 bit AES encryption has been added in version 1.9.8.

从wxSqlite3 1.9.6开始,它的加密扩展就已经从C++转为纯C语言实现,因此现在可以直接编译SQLite
amalgamation source distribution实现了。只需要编译文件sqlite3secure.c即可,其已经include了所有需要的源文件。当然C++版本的文件也在codec目录(\sqlite3\secure\src\codec-c)中,但作者强烈的不推荐用它。


唠了这么多,现在正式开始。
1、下载sqlite-amalgamation-XXXXXX.zip,它已经包含了所有的源文件,也不需要另外的辅助工具了,解压到某一目录,如Sqlite3。
2、如果你下的sqlite-amalgamation-XXXXXX.zip没有makefile,那就自己做一个project吧,在VS2008里新建一个空工程,只加入sqlite3secure.c文件到工程即可,修改其中的#include "sqlite.c"的sqlite.c文件路径为上一步解压的文件的路径。
3、在配置属性中设置配置类型为静态库(.Lib),添加预处理
SQLITE_HAS_CODEC=1
CODEC_TYPE=CODEC_TYPE_AES128
SQLITE_CORE
THREADSAFE
SQLITE_SECURE_DELETE
SQLITE_SOUNDEX
SQLITE_ENABLE_COLUMN_METADATA
4、编译生成Lib文件。
5、将生成的Lib文件取代wxsqlite3-2.1.1\sqlite3\lib中的sqlite3.lib文件,然后编译wxSqlite3为静态库,别忘了设置USE_DYNAMIC_SQLITE3_LOAD=0。


》》》》》》》》》》》》》》》》》》》》


Delphi使用wxsqlite加密Sqlite数据库

提起桌面数据库,Sqlite谁都知道,但对于它的加密一直困扰着很多人,而delphi的加密使用范例更少。在网上混了两天,查找了一些Sqlite加密的相关资料,也知道了wxsqlite这个东西,它是一个sqlite3.dll的嵌入扩展,重要的是里面加入了对Sqlite的AES 128bit-256bit加密扩展,而且用的是Sqlite预留的函数接口,这意味着什么呢?UniDAC从3.5.14版本以后加入了对encrypted sqlite的支持,这功能支持 SEE/CEROD这样使用预留接口开发的加密功能的第三方扩展,也就是说wxsqlite同样能被UniDAC所支持,这意味着你就不用再去改UniDAC的源代码了,wxsqlite可以在我这下载http://download.csdn.net/source/3215472

下面说说具体的使用方法,下载后你可以在sqlite3/secure目录下找到AES128和AES256两个文件夹,分别包含两种加密类型扩展的库文件,找到sqlite3.dll,然后放到你要调用的路径,然后下面是我随便Copy修改后的delphi的源码:

unit main; 

interface 


uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
Dialogs, DB, MemDS, DBAccess; 

type 
TSQLiteDB = Pointer; 
Tsqlite3=Pointer; 
TForm4 = class(TForm) 
procedure FormCreate(Sender: TObject); 
private 
{
      Private declarations } 
public 
{
      Public declarations } 
end; 
const 
{
     $IF Defined(MSWINDOWS)} 
SQLiteDLL = 'sqlite3.dll'; 
{
     $ELSEIF Defined(DARWIN)} 
SQLiteDLL = 'libsqlite3.dylib'; 
{
     $linklib libsqlite3} 
{
     $ELSEIF Defined(UNIX)} 
SQLiteDLL = 'sqlite3.so'; 
{
     $IFEND} 
var 
Form4: TForm4; 
db: TSQLiteDB; 
function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_open'; 
function sqlite3_key ( 
pDb: Tsqlite3; // Database handle 
pKey: PAnsiChar; // Database PassWord (UTF-8) 
nKey: Integer // Database sizeofpassword 
): integer; cdecl; external SQLiteDLL name 'sqlite3_key'; 
function sqlite3_rekey ( 
pDb: Tsqlite3; // Database handle 
pKey: PAnsiChar; // Database PassWord (UTF-8) 
nKey: Integer // Database sizeofpassword 
): integer; cdecl; external SQLiteDLL name 'sqlite3_rekey'; 
implementatio
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值