Generally we always create MySQl database using tool like heidisql, mysql yog, phpadmind and so on. But in this tutorial I will show how to create database and table using delphi by runtime. Many ways to create  MySQL  database with runtime for example :

  • Import sql text by shellexecute to call mysql.exe or call batch file
  • Using Tzconnection
  • Using ZSQLProcessor
  • Using TZQuery
  • or may be using ado component witch connected with ODBC

In this section I will give example how to create MySQL database using Tzconnection

Next… on

The first time create new application and save all (project and unit). and then

  • add Tzconnection
  • add button
  • onbutton clik write codes :
  1. make connection to mysql
  2. if success then create database
  3. create table
procedure TForm1.Button1Click(Sender: TObject);
var sdatabase,A,B,Csql:string;
begin
  sdatabase:='CREATE DATABASE IF NOT EXISTS SCHOOl';
  A:='USE SCHOOL;';
  B:='DROP TABLE IF EXISTS TSISWA;';
  Csql:='CREATE TABLE TSISWA ( ' +
   ' NIS varchar(9) default NULL,' +
   ' NAMA varchar(9) default NULL,' +
   ' ALAMAT varchar(9) default NULL'+
   ' )ENGINE=MyISAM DEFAULT CHARSET=latin1; ';
 ZConnection1.Disconnect;
 ZConnection1.HostName:='localhost';
 ZConnection1.User:='root';
 ZConnection1.Protocol:='mysql';
 ZConnection1.Port:=3306;
 ZConnection1.Password:='';
// connect
  try
  ZConnection1.Connect;
  except
    on E: Exception do showmessage(E.Message);
  end;
  //create database
    Try
    ZConnection1.ExecuteDirect(sdatabase);
    showmessage('success');
    except
     on E: Exception do showmessage(E.Message);
    end;
   //create table

    Try
    ZConnection1.ExecuteDirect(A);
    ZConnection1.ExecuteDirect(B);
    ZConnection1.ExecuteDirect(Csql);
    showmessage('success create table..!');
    except
      on E: Exception do showmessage(E.Message);
    end;

end;

 

 

source

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZConnection;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ZConnection1: TZConnection;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var sdatabase,A,B,Csql:string;
begin
  sdatabase:='CREATE DATABASE IF NOT EXISTS SCHOOl';
  A:='USE SCHOOL;';
  B:='DROP TABLE IF EXISTS TSISWA;';
  Csql:='CREATE TABLE TSISWA ( ' +
   ' NIS varchar(9) default NULL,' +
   ' NAMA varchar(9) default NULL,' +
   ' ALAMAT varchar(9) default NULL'+
   ' )ENGINE=MyISAM DEFAULT CHARSET=latin1; ';
 ZConnection1.Disconnect;
 ZConnection1.HostName:='localhost';
 ZConnection1.User:='root';
 ZConnection1.Protocol:='mysql';
 ZConnection1.Port:=3306;
 ZConnection1.Password:='';
// connect
  try
  ZConnection1.Connect;
  except
    on E: Exception do showmessage(E.Message);
  end;
  //create database
    Try
    ZConnection1.ExecuteDirect(sdatabase);
    showmessage('success');
    except
     on E: Exception do showmessage(E.Message);
    end;
   //create table

    Try
    ZConnection1.ExecuteDirect(A);
    ZConnection1.ExecuteDirect(B);
    ZConnection1.ExecuteDirect(Csql);
    showmessage('success create table..!');
    except
      on E: Exception do showmessage(E.Message);
    end;

end;

end.

form

object Form1: TForm1
  Left = 192
  Top = 124
  Width = 427
  Height = 286
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 168
    Top = 120
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ZConnection1: TZConnection
    Left = 128
    Top = 80
  end
end
Print Friendly