You are not logged in.
i Should know the size of Character field, and Whether or not field can be null?
Offline
You have ISqlDBRows.ColumnNull() for this.
For the field size, use ColumnUtf8() and its returned length.
function ColumnUtf8(Col: integer): RawUtf8;
ColumnUtf8 can't get field size, only return empty string
Offline
What do you call "field size" then?
create table Test (F1 char(20), F2 varchar(10));
insert into Test(F1,F2) Values('F1 Test Value','Should ok')
How Can I get F1 Field Size=20 and F2 Field size=10?
Last edited by gunix (2025-01-09 08:31:03)
Offline
Please take care about how you ask your question: it has nothing to do with TSqlDbStatement.
It is part of the table description.
All this information is available from TSqlDBConnectionProperties.GetFields().
Offline
SqlStatement:TSqlDbStatement;
SqlStatement.Execute('select A.F1,A.F2,...... from Test A join xxx B on A.xxx=B.xxx where .....');
ColumnType(0) Can Get F1 FieldType ftUtf8, But i don't know how to get field size
Offline
You are making a lot of confusion, I am afraid.
Read again my answer, and try to find out what is wrong with your assumptions.
First: why do you need this "field size"?
Modern databases (like PostgreSQL) does not even favor any such field size in table field declarations.
Offline
in order to convert into Delphi TDataset, FieldDefs.Add( FieldName,FieldType,FieldSize,CanNull)
Offline
in order to convert into Delphi TDataset, FieldDefs.Add( FieldName,FieldType,FieldSize,CanNull)
You can try the TSqlDBConnectionProperties.GetFields() method ab mentioned. This method outputs the field definition to the Fields parameter, which is a TSqlDBColumnDefineDynArray type. Each element contains the following information:
TSqlDBColumnDefine = packed record
/// the Column name
ColumnName: RawUtf8;
/// the Column type, as retrieved from the database provider
// - returned as plain text by GetFields method, to be used e.g. by
// TSqlDBConnectionProperties.GetFieldDefinitions method
// - SqlCreate will check for this value to override the default type
ColumnTypeNative: RawUtf8;
/// the Column default width (in chars or bytes) of ftUtf8 or ftBlob
// - can be set to value <0 for CLOB or BLOB column type, i.e. for
// a value without any maximal length
ColumnLength: PtrInt;
...
end;
Offline
You can try the TSqlDBConnectionProperties.GetFields() method ab mentioned. This method outputs the field definition to the Fields parameter, which is a TSqlDBColumnDefineDynArray type. Each element contains the following information:
I don't know Field from which Table,because I read Sql from Json File, Fetch Data,Display Data.
Last edited by gunix (2025-01-10 06:52:46)
Offline
I don't know Field from which Table,because I read Sql from Json File, Fetch Data,Display Data.
Then the is no such thing as "field size" in JSON.
What you could get is the maximum size of all values in a JSON string field, but it is mostly pointless.
Therefore, my initial question stands: why do you need this "field size"?
And in which unit? UTF-8 bytes? Unicode codepoints?
Offline
fsize :: Result
-> Column
-> IO Int
fsize result (Col colNum) = numFromResult result $ \ptr -> c_PQfsize ptr colNum;
Can Use fsize function in libpq.dll get field size on Postgresql DBMS?
if Can get field size ,the unit can be UTF-8 bytes
Offline
then,why FieldDefs need set field size of string field?
DBgrid Can Adjust Column width by Field size?
also I can set field size to -1
Offline
DBgrid needs some other mean to adjust its columns, because UTF-8 size is not at all the same as visual width.
FieldDefs use field size only for old DBs requiring it.
Offline
unidac can Get field size, string field.size =character_octet_length
pq.fsize always return -1
when client app commit data to server, raise error "value is too long(20)" when insert more than 20 character ( fiedtype=varchar(20)),so, Give a permit length is a good idea in client side.
I have test ,Can't work right
procedure TSqlDBPostgresStatement.BindColumns;
var
nCols, c,k: integer;
cName: RawUtf8;
p: PUtf8Char;
begin
ClearColumns;
nCols := PQ.nfields(fRes);
fColumn.Capacity := nCols;
for c := 0 to nCols - 1 do
begin
p := PQ.fname(fRes, c);
FastSetString(cName, p, StrLen(p));
with AddColumn(cName)^ do
begin
ColumnAttr := PQ.ftype(fRes, c);
ColumnType := TSqlDBPostgresConnectionProperties(Connection.Properties).
Oid2FieldType(ColumnAttr);
//add
ColumnDataSize:=PQ.fsize(fRes, c);
end;
end;
end;
Last edited by gunix (2025-01-11 14:20:34)
Offline
I solved this problem by defining the fields of the dataset directly at design time
Offline