Advertisement
SaamSouza

Database

Apr 20th, 2019
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.89 KB | None | 0 0
  1. unit uPrincipal;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  7.   System.Classes, Vcl.Graphics,
  8.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  9.  
  10. type
  11.   TfrmPrincipal = class(TForm)
  12.     Label1: TLabel;
  13.     btnBD: TButton;
  14.     procedure btnBDClick(Sender: TObject);
  15.   private
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20.  
  21. var
  22.   frmPrincipal: TfrmPrincipal;
  23.  
  24. implementation
  25.  
  26. {$R *.dfm}
  27.  
  28. uses u_DM;
  29.  
  30. procedure TfrmPrincipal.btnBDClick(Sender: TObject);
  31. var
  32.   tabela: String;
  33. begin
  34.   tabela := 'Favoritos';
  35.   try
  36.     DM.Consulta.Close;
  37.     DM.Consulta.SQL.Add
  38.       ('CREATE TABLE if not exists '+tabela+'(id INT NOT NULL AUTO_INCREMENT, `Usuario` VARCHAR(30), `Url` VARCHAR(300), `Computador` VARCHAR(30), PRIMARY KEY(id)) DEFAULT CHARSET = utf8');
  39.     DM.Consulta.ExecSQL;
  40.   except
  41.     on E: Exception do
  42.       ShowMessage(E.ClassName + #13 + 'Erro!' + #13 + E.Message);
  43.   end;
  44. end;
  45.  
  46. end.
  47.  
  48. //DataModule
  49. unit u_DM;
  50.  
  51. interface
  52.  
  53. uses
  54.   System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  55.   FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  56.   FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MySQLDef,
  57.   FireDAC.VCLUI.Wait, FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf,
  58.   FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
  59.   FireDAC.Comp.UI, FireDAC.Phys.MySQL, Vcl.Dialogs, Data.FMTBcd;
  60.  
  61. type
  62.   TDM = class(TDataModule)
  63.     Conexao: TFDConnection;
  64.     Driver: TFDPhysMySQLDriverLink;
  65.     DataSource: TDataSource;
  66.     Cursor: TFDGUIxWaitCursor;
  67.     Consulta: TFDQuery;
  68.     Comando: TFDCommand;
  69.     procedure DataModuleCreate(Sender: TObject);
  70.     procedure DataModuleDestroy(Sender: TObject);
  71.  
  72.   private
  73.     { Private declarations }
  74.   public
  75.     { Public declarations }
  76.  
  77.   end;
  78.  
  79. var
  80.   DM: TDM;
  81.  
  82. implementation
  83.  
  84. {%CLASSGROUP 'Vcl.Controls.TControl'}
  85.  
  86. uses uPrincipal;
  87. {$R *.dfm}
  88.  
  89. procedure TDM.DataModuleCreate(Sender: TObject);
  90. var
  91.   banco: String;
  92. begin
  93.   (* Obs: Essas configurações são para conexão local(localhost) *)
  94.   banco := 'DBase';
  95.   Conexao.Params.UserName := 'root';
  96.   Conexao.Params.Password := '';
  97.   Driver.VendorLib := 'libmySQL.dll';
  98.  
  99.   try
  100.     Conexao.Connected := True;
  101.     DM.Comando.Close;
  102.     DM.Comando.CommandText.CommaText :=
  103.       ('CREATE DATABASE IF NOT EXISTS ' + banco +
  104.       ' DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci');
  105.     DM.Comando.Execute;
  106.     DM.Comando.Close;
  107.     DM.Comando.CommandText.CommaText := 'USE ' + banco + '';
  108.     DM.Comando.Execute;
  109.  
  110.   except
  111.     on E: Exception do
  112.     begin
  113.       showMessage(E.ClassName + #13 + 'Erro ao conectar' + #13 + E.Message);
  114.       exit
  115.     end;
  116.   end;
  117. end;
  118.  
  119. procedure TDM.DataModuleDestroy(Sender: TObject);
  120. begin
  121.   Conexao.Connected := False;
  122.   Consulta.Active := False;
  123. end;
  124.  
  125. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement