1 (edited by sibprogsistem 2020-11-18 11:10:04)

Topic: [скрипт] создание базы данных синтаксис MariaDB

скрип считывает данные из tables.ini и создает базу данных синтаксис  MariaDB

единственное в чем я не смог разобраться - это как правильно считать  значения по умолчанию..
 

procedure frmBuildProject_lacessdb_OnClick (Sender: TObject);
var
MySQL: TMyConnection;
i,j,p:integer;
section,sValue:TStringList;
tmiFil:TMemIniFile;
ini:TIniFile;
str,sPos,sID:String ;
begin
  frmStartProject.SQLConnection.Connected := False;
  MySQL := TMyConnection.Create(frmBuildProject);
  try
      MySQL.Options.UseUnicode := True;
      MySQL.Server := 'localhost';
      MySQL.Port := 3306;
      MySQL.Username := 'root';
      MySQL.Password := 'root';
      MySQL.Database := 'test';
      MySQL.LoginPromt := False;

      try
         MySQL.Connect;
      except
         ShowMessage('Can''t connect to database.');
      end;
      if MySQL.Connected then
      begin
        sID:='';
        str:='';
        tmiFil:=TMemIniFile.Create(ExtractFilePath (ParamStr (0))+'tables.ini');
        sValue:=TStringList.Create;
        section:=TStringList.Create;
        tmiFil.ReadSections(section);
        // считать все имена таблиц и подготовить строку удаления
        for i:=0 to section.Count-1 do
             if (i=section.Count-1) then str:=str+'`'+section[i]+'`' else str:=str+'`'+section[i]+'`,';
        // удалить все таблицы из базы данных если такие есть
        MySQL.ExecSQL('DROP TABLE IF EXISTS '+str);
        str:='';
        // создать таблицы и поля в базе данных
        for i:=0 to section.Count-1 do
        begin
          str:='CREATE TABLE `'+section[i]+'` (`id` INT(11) NOT NULL AUTO_INCREMENT';
          tmiFil.ReadSectionValues(section[i],sValue);
          for j:=0 to sValue.Count-1 do
          begin
            p:=PosEx('=',sValue[j],1);
            sPos:= copy(sValue[j],2,p-2);
            if pos('TEXT',sValue[j]) then str:=str+',`'+sPos+'` MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL';
            if pos('INTEGER',sValue[j]) then str:=str+',`'+sPos+'` INT(11) DEFAULT NULL';
            if pos('CURRENCY',sValue[j]) then str:=str+',`'+sPos+'` DOUBLE DEFAULT NULL';
            if pos('BOOLEAN',sValue[j]) then str:=str+',`'+sPos+'` INT(11) DEFAULT NULL';
            if pos('DATETIME',sValue[j]) then str:=str+',`'+sPos+'` DATETIME DEFAULT NULL';
            if pos('DATE',sValue[j]) then str:=str+',`'+sPos+'` DATE DEFAULT NULL';
            if pos('TIME',sValue[j]) then str:=str+',`'+sPos+'` TIME DEFAULT NULL';
            if pos('IMAGE',sValue[j]) then str:=str+',`'+sPos+'` MEDIUMBLOB DEFAULT NULL,`img_filename` text DEFAULT NULL';
            if pos('FILE',sValue[j]) then str:=str+',`'+sPos+'` MEDIUMBLOB DEFAULT NULL,`file_filename` text DEFAULT NULL';
            if pos('record_count',sValue[j]) then str:=str+',`record_count` int(11) DEFAULT NULL';
            if pos('>',sValue[j]) then begin
              str:=str+',`'+sPos+'` INT(11) NULL';
              sID:=sID+'ALTER TABLE `'+section[i]+'` ADD INDEX `'+section[i]+'_'+sPos+'` (`'+sPos+'`) USING BTREE;';
            end;
          end;
       str:=str+',PRIMARY KEY (`id`)) ENGINE = InnoDB;';
       if (i=section.Count-1) then str:=str+sID;
       MySQL.ExecSQL(str);
     end;
     MySQL.Disconnect;
   end;
   finally
   MySQL.Free;
   end;
end;

Re: [скрипт] создание базы данных синтаксис MariaDB

Спасибо за помощь!