[fpc-pascal] Bump: Trouble setting/getting ftVarBytes field data in TBufDataset

2011-08-02 Thread Reinier Olislagers
On 2-8-2011 7:39, Reinier Olislagers wrote:
> I've got trouble finding out how to fill and retrieve data for a
> ftVarBytes field in a TBufDataset.
> 
> .AsString doesn't seem to work well - if I fill it with the string
> How do I fill this field?
> I get back
> How
> 
> I tried SetData, which takes a buffer as data, so I tried filling a
> PChar with the string data. Retrieving it leads to a big dump of memory,
> so I wonder how you define the end of the buffer in the SetData call.
> 
> Hope somebody can point me to some documentation or tell me the proper
> way to do this. I've looked at Delphi docs, but that dealt mostly with
> general BLOB fields, don't know if that includes varBytes.
> 
> Below some test code and output:
> program varbytesproject;
> 
> {$mode objfpc}{$H+}
> {$APPTYPE CONSOLE}
> 
> uses
>   {$IFDEF UNIX}{$IFDEF UseCThreads}
>   cthreads,
>   {$ENDIF}{$ENDIF}
>   Classes, SysUtils,
>   { you can add units after this }
>   DB, BufDataSet;
> 
> var
>   TestDataset: TBufDataset;
>   FieldDef: TFieldDef;
>   TestString: String;
>   TempPChar: PChar;
> begin
>   TestDataset := TBufDataset.Create(nil);
>   FieldDef := TestDataset.FieldDefs.AddFieldDef;
>   FieldDef.Name := 'ftVarBytes';
>   FieldDef.DataType := ftVarBytes;
>   FieldDef.Size := 2;
>   TestDataset.CreateDataSet;
>   TestDataset.Open;
>   TestDataset.Append;
> 
>   writeln('1. How do I fill an ftVarBytes field?');
>   Teststring:='How do I fill this field?';
>   writeln('2. Like this?');
>   TestDataset.FieldByName('ftVarBytes').Asstring := Teststring;
>   writeln('ftVarbytes.AsString: ' +
> TestDataset.FieldByName('ftVarBytes').AsString);
> 
>   writeln('3. Or like this?');
>   TempPChar:=PChar(TestString);
>   TestDataSet.FieldByName('ftVarBytes').SetData(TempPChar);
>   writeln('ftVarbytes.AsString: ' +
> TestDataset.FieldByName('ftVarBytes').AsString);
>   TestDataset.Post;
> 
>   writeln('4. Get data using this:');
>   writeln('ftVarbytes.AsString: ' +
> TestDataset.FieldByName('ftVarBytes').AsString);
>   writeln('5. Or get data using this:');
>   TempPchar:='';
>   if TestDataset.FieldByName('ftVarBytes').GetData(TempPChar) then
>   begin
> writeln('We filled TempPChar with: ' + TempPChar);
>   end
>   else
>   begin
> writeln('The Getdata function didn''t work.');
>   end;
>   TestDataset.Close;
>   TestDataset.Free;
> end.
> 
> Output:
> 1. How do I fill an ftVarBytes field?
> 2. Like this?
> ftVarbytes field contains: How
> 3. Or like this?
> ftVarbytes field contains: How
> 4. Get data using this:
> ftVarbytes field contains: How
> 5. Or get data using this:
> We filled TempPChar with:
> 
> !The Getdata function didn't work.
> 
> 
> 
> 
> 
> TObject♦
> 

Does anybody have a suggestion on how to use ftVarbytes fields or aren't
they supported in FPC?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Is there a way to create a Firebird embedded database programmatically?

2011-08-02 Thread Reinier Olislagers
When using the Firebird embedded database, it's nice to be able to
create the database using only FreePascal/Lazarus.

Is there a way to do this, e.g. using the SQLScript component, using
something like this:
const
  DatabaseFile = 'DATABASE1.FDB';
var
  CreateScript: TSQLScript;
  ScriptText: TStringList;
begin
  //Connection to Firebird database
  IBConnection1.HostName := ''; //must be empty for embedded Firebird;
must be filled for client/server Firebird
  IBConnection1.DatabaseName := DatabaseFile; //Filename of Firebird
database
  IBConnection1.Username := 'SYSDBA';
  IBConnection1.Password := 'masterkey'; //default password for SYSDBA
  IBConnection1.Charset := 'UTF8'; //Send and receive string data in
UTF8 encoding
  IBConnection1.Dialect := 3; //Nobody uses 1 or 2 anymore.
  if (FileExists(DatabaseFile)=false) then
  begin
CreateScript := TSQLScript.Create(nil);
ScriptText:=TStringList.Create;
try
  CreateScript.OnException:=@ExceptionHandler;
  IBConnection1.DatabaseName:='';
  CreateScript.DataBase:=IBConnection1;
  CreateScript.Transaction:=SQLTransaction1;
  IBConnection1.Open;
  SQLTransaction1.StartTransaction;

  ScriptText.Text:='CREATE DATABASE ''database1.fdb'' page_size
16384  user ''SYSDBA'' password ''masterkey'' default character set UTF8;';
  CreateScript.Script:=ScriptText;
  CreateScript.ExecuteScript;
  SQLTransaction1.Commit;

  SQLTransaction1.StartTransaction;
  ScriptText.Text:='CREATE TABLE Table1 (id VARCHAR(255), name
VARCHAR(255));';
  CreateScript.Script:=ScriptText;
  CreateScript.ExecuteScript;
  SQLTransaction1.Commit;

  IBConnection1.Close;
finally
  ScriptText.Free;
  CreateScript.Free;
end;
  end;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal