Generating GUID/getting temp table name automatically in delphi...

What is GUID?

A Globally Unique Identifier (GUID) is an alphanumeric identifier used to indicate a unique installation of a product. GUIDs are used in many popular software applications such as web browsers and media players.

The GUID is in the format 8-4-4-4-12: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" where each x is a hexadecimal number in the range 0-9 or AF. For example: 6F9619FF-8B86-D011-B42D-00C04FC964FF is a valid GUID value.


Why use GUIDs?

No two computers in the world will generate duplicate GUID values. GUIDs are primarily used in networks or systems with multiple nodes, multiple computers, to assign identifiers that must be unique. It is used as the primary key in the database, the identification is unique, and there is no error in converting data between different databases, but there will be a lot of trouble with self-incrementing fields. On the Windows platform, GUIDs are widely used: registry, class and interface identifiers, databases, and even automatically generated machine and directory names.

How to generate GUID in Delphi:

//Lazy girl: QQ:112412387

// uses ComObj
var
  AGuid: TGUID;
  sGUID: string;
begin
  sGUID := CreateClassID;
  ShowMessage(sGUID); // Guid with curly braces
  Delete(sGUID, 1, 1);
  Delete(sGUID, Length(sGUID), 1);
  ShowMessage(sGUID); // Remove the Guid of curly braces, occupy 36 bits with a minus sign in the middle
  sGUID:= StringReplace(sGUID, '-', '', [rfReplaceAll]);
  ShowMessage(sGUID); // Remove The Guid of the minus sign, accounting for 32
end;

The algorithm for generating the GUID is based on the following aspects: 1. The current date and time. 2. Network card address. 3. Clock sequence. 4. Auto-increment counter. Among them, the network card addresses are different from each other. For machines without network cards, the auto-incrementing counter is unique to the machine in use. MS guarantees that 100 GUIDs are generated per second in the same computer. A GUID is absolutely unique for more than 3000 years.


Example of getting temporary table name:

function GetTempTableName(iKind:integer=0):String;
var
  guidTableName:TGUID;
begin
  CreateGUID(guidTableName);
  if iKind=0 then

   //BSTempTableName自定义的表名前缀
   Result:=BSTempTableName+GUIDToString(guidTableName)
  else
   Result:=GUIDToString(guidTableName);
  Result:=StringReplace(Result,'{','',[rfReplaceAll]);
  Result:=StringReplace(Result,'}','',[rfReplaceAll]);
  Result:=StringReplace(Result,'-','',[rfReplaceAll]);
  if g_SQLtype <> 'MSSQL' then
    result := 'ORCL'+ RightStr(Result,10);
end;


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325495011&siteId=291194637