Forum > Databases

[SOLVED] get the data type for a specific variable from the loaded database.

<< < (3/3)

BrunoK:
Dont know if it replies to your question. Any way ...

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program pgmDataType; {$mode objfpc}{$H+} uses  Classes,  SysUtils,  BufDataset,  DB; var  vDataSet: TBufDataSet = nil;  { <-- Change to a suitable TDataSet class for  ex :  vDataSet: TSqlQuery = nil;  }     procedure CreateDataset;  var    lFieldDef: TFieldDef;  begin    vDataSet := TBufDataset.Create(nil);    with vDataSet, FIeldDefs do begin      with AddFieldDef do begin        Name := 'IntegerField';        DataType := ftInteger;      end;      with AddFieldDef do begin        Name := 'IAmAtringFIeld';        DataType := ftString;        Size := 20;      end;      CreateDataset;      Append;      Fields[0].Value := 0;      Fields[1].Value := 'Hello world !';      Post;      Append;      Fields[0].Value := 263;      Fields[1].Value := 'I am  a TStringField !';      Post;      Close;    end;  end;   procedure DumpDataSetFieldInfo;  var    i: integer;  begin    with vDataSet do begin      WriteLn('I''m active=', Active);{  Add following line for data sets other than TBufDataset -->   for example for an TSQLQuery dataset  ->      Open;            { One or the other or both, I dont remember  }      UpdateFieldDefs; { May be not even required }}      for i := 0 to FieldDefs.Count - 1 do        with FieldDefs[i] do          { Here you have each database field definition as selected by FPC.DB,            to know the type that's DataType }          WriteLn(Name, ' ', DataType, ' ', Size);    end;  end;begin  CreateDataset;  DumpDataSetFieldInfo;  vDataSet.Free;  ReadLn;end.

Seenkao:
BrunoK, я не уверен. По моему слишком громоздкое решение...


Google translate:
BrunoK, I'm not sure. Too cumbersome a solution in my opinion...

BrunoK:

--- Quote from: Seenkao on August 05, 2024, 05:09:29 pm ---BrunoK, I'm not sure. Too cumbersome a solution in my opinion...

--- End quote ---
Cumbersome ? This is a full demo program.
You add the lines in the following code that will retireve in an easy way to the Datatype attributed by the DB code. This does not depend on any specific Database like you do where querying table_select * from sqlite_master where TableName = myTable.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  function FieldTypeForName(aDataSet: TDataSet; aFieldName: string): TFieldType;  var    lFieldDefIx: integer;  begin    lFieldDefIx := aDataSet.FieldDefs.IndexOf(aFieldName);    if lFieldDefIx < 0 then      Result := ftUnknown    else      Result := aDataSet.FieldDefs[lFieldDefIx].DataType;  end; begin  CreateDataset;  DumpDataSetFieldInfo;  WriteLn('FieldType for ', 'IAmAtringFIeld', ' = ',    FieldTypeForName(vDataSet, 'IAmAtringFIeld'));  vDataSet.Free;  ReadLn;end.      

Seenkao:

--- Quote from: BrunoK on August 05, 2024, 06:23:21 pm ---Cumbersome ? This is a full demo program.

--- End quote ---
Извиняюсь, но да. И я не говорю что ваш пример - это плохой пример! Вполне возможно для других он больше подойдёт.
Приведённый мной пример, это так же можно сказать полноценная программа. Я могу данные просто записать либо в массив, либо какой-то компонент и работать с ними.
Надеюсь вы меня поймёте!


Google translate:
Sorry, but yes. And I'm not saying that your example is a bad example! It's quite possible that it would be more suitable for others.
The example I gave is also a full-fledged program. I can simply write the data either to an array or to some component and work with it.
I hope you understand me!

Navigation

[0] Message Index

[*] Previous page

Go to full version