2013-02-05 71 views
1

我嘗試使用Delphi XE3插入SQLite中的blob字段。我該如何插入到使用Delphi XE3的SQLite中的Blob

我一直在使用這樣的TDBXCommand和TSQLConnection。 但BLOB字段沒有被插入,甚至我cannnot收到使用設爲TSQLConnection查詢

procedure TDBXCommandHelper.Init(const AQry: String); 
begin 
    Parameters.ClearParameters; 
    Close; 
    Prepare; 
    Text := AQry; 
end; 

procedure dmDB.InsertPicture; 
const 
    QRY = 'INSERT INTO Memo(Picture) VALUES(?)'; 
var 
    LTransaction: TDBXTransaction; 
    LBlob: TDBXParameter; 
    LStream: TFileStream; 
begin 
    LTransaction := FDBCon.BeginTransaction; 
    LStream := TFileStream.Create('d:\sample.bmp', fmOpenRead); 
    LBlob := TDBXParameter.Create; 
    try 
    try 
     FDBXCmd := FDBCon.DBXConnection.CreateCommand; 
     FDBXCmd.CommandType := TDBXCommandTypes.DbxSQL; 
     FDBXCmd.Init(QRY); 

     LBlob.DataType := TDBXDataTypes.BlobType; 
     LBlob.SubType := TDBXSubDataTypes.BinarySubType; 
     LBlob.Value.SetStream(LStream, False); 
     FDBXCmd.Parameters.AddParameter(LBlob); 
     FDBXCmd.ExecuteUpdate; 
    except 
     on E: Exception do 
     FDBCon.RollbackFreeAndNil(LTransaction); 
    end; 
    FDBCon.CommitFreeAndNil(LTransaction); 
    finally 
    FreeAndNil(LStream); 
    FreeAndNil(LBlob); 
    end; 
end; 

任何結果,但我不能得到任何結果

procedure TInsertThread.NoteInsertExcute; 
const 
    QRY = 'INSERT INTO Memo(Picture) VALUES(:Picture)'; 
var 
    LTransaction: TDBXTransaction; 
    LParams: TParams; 
    LStream: TMemoryStream; 
begin 
    LTransaction := FDBCon.BeginTransaction; 
    LParams := TParams.Create(nil); 
    LStream := TMemoryStream.Create; 
    LStream.LoadFromFile(FValues.Values[NAME_PICTURE]); 
    try 
    LParams.CreateParam(ftBlob, 'Picture', ptInput); 
    LParams.ParamByName('Picture').LoadFromStream(LStream, ftBlob); 
    FDBCon.Execute(QRY, LParams); 
    FDBCon.CommitFreeAndNil(LTransaction); 
    finally 
    FreeAndNil(LStream); 
    FreeAndNil(LParams); 
    end; 
end; 
+0

您可以將LStream(內存流)的內容編碼爲base64字符串,並將其推送到您的db字段。 – ComputerSaysNo

+0

這個「插入備忘錄(圖片)」是什麼意思? SQLite3沒有列定義,即只有一些「親和性」。您可以在任何列中插入任何類型的數據。嘗試刪除「備忘錄()」。 SQLite3處理blob沒有問題。 –

+0

謝謝你的支持。 備註是表名和表是這樣定義.. 'CREATE TABLE [備註]( [ID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [用戶名] INTEGER NOT NULL, [日期] TEXT NULL, [類型] TEXT NOT NULL, [Value] TEXT NOT NULL, [Picture] BLOB NULL )' – gomsun2

回答

1

你可以嘗試以下方法:

function GetFileAsBytesValue(AFileName: TFileName): TArray<Byte>; 
var 
    Len: Integer; 
    LStream: TMemoryStream; 
begin 
    LStream := TMemoryStream.Create; 
    try 
    LStream.LoadFromFile(AFileName); 
    Len := LStream.Size; 

    SetLength(Result, Len); 
    Move(LStream.Memory^, Result[0], Len); 
    finally 
    LStream.Free; 
    end; 
end; 

procedure dmDB.InsertPicture; 
const 
    QRY = 'INSERT INTO Memo(Picture) VALUES(?)'; 
var 
    LTransaction: TDBXTransaction; 
    LDBXCmd: TSQLQuery; 
    LParam: TParam; 
begin 
    LTransaction := FDBCon.BeginTransaction; 
    LDBXCmd := TSQLQuery.Create(FDBCon); 
    try 
    try 
     LDBXCmd.SQLConnection := FDBCon; 
     LDBXCmd.SQL.Text := QRY; 
     LParam := LDBXCmd.Params.CreateParam(ftBlob, 'Picture', ptInput); 
     LParam.AsBlob := GetFileAsBytesValue('d:\sample.bmp'); 
     LDBXCmd.ExecSQL; 
    except 
     on E: Exception do 
     FDBCon.RollbackFreeAndNil(LTransaction); 
    end; 
    FDBCon.CommitFreeAndNil(LTransaction); 
    finally 
    LDBXCmd.Free; 
    end; 
end; 
+0

謝謝。我會查一下 – gomsun2

2

這很容易就像:

var 
    ms: TMemoryStream; 
    sq: TSQLQuery; 
begin 
    ms := TMemoryStream.Create; 
    ms.LoadFromFile('C:\Pictures\l.jpg'); 
    if ms <> nil then 
    begin 
    sq := TSQLQuery.Create(nil); 
    sq.SQLConnection := con1; 
    sq.SQL.Text := 'update db1 set picture= :photo ;'; 
    sq.Params.ParseSQL(sq.SQL.Text, true); 
    sq.Params.ParamByName('photo').LoadFromStream(ms, ftBlob); 
    sq.ExecSQL(); 
    end; 


其中con1是TSQLConnection。

相關問題