delphi tutorial database tutorial

delphi,mysql,firebird FOR YOU

When you want to restore your mysql tables, to avoid errors, you must drop foreign key and create its again. Mysql stores foreign key constraint into information_schema.key_column_usage table. These are several steps that you can be done to drop and create foreign key:


  1. Export foreign key script by using “select into outfile” command. For example:

select concat('alter table ',table_name,' add constraint ',

constraint_name,' foreign key(',column_name,')',

' references ',referenced_table_name,'(',

referenced_column_name,')')

into outfile 'c:/fktestdatabase.sql' from

information_schema.key_column_usage

where table_schema='testdatabase'

Note: You can use EMS Manager lite to do this method.


  1. Drop your foreign key


  1. Create your foreign key again by using shellexecute command. For example:


shellexecute(self.Handle,pchar('open'),

pchar('C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe'),

pchar('-u root -proot -h localhost -e "use testdatabase;source '+

setpath(extractfilepath(paramstr(0))+'fktestdatabase.sql;')+

'" '),

pchar('C:\Program Files\MySQL\MySQL Server 5.0\bin'),sw_show);


For further details download my application here.


Actually, mysql triggers can't be deactivated. We can only drop and creating its again. Now I give you several steps how to drop and create mysql triggers:


  1. Export your triggers into a file by using mysqldump command. Look at this code:

mysqldump -u your username -pyour password -h your host name --triggers -d -t -r "your file path" your database name.

example: mysqldump -u root -proot -h localhost --triggers -d -t -r "c:\dmptrigger.sql" test


  1. Make a dataset, fill it with this query:

example:

select trigger_name from INFORMATION_SCHEMA.TRIGGERS

where trigger_schema='testdatabase'

  1. Drop your trigger with this command:

example: conn.ExecuteDirect('drop trigger '+ctriggertrigger_name.Value);


  1. Create your triggers again. In delphi, you can use shellexecute command.


Example:

shellexecute(self.Handle,pchar('open'),

pchar('C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe'),

pchar('-u root -proot -h localhost -e "use test;source '+

setpath(extractfilepath(paramstr(0))+'dmptrigger.sql;')+

'" '),

pchar('C:\Program Files\MySQL\MySQL Server 5.0\bin'),sw_show);


For further details, download my application here. This application using ado for the connection.


This tutorial primarily provided for users who use mysql 5.0.

When you want to connect to mysql database using Dbexpress, you have to use SQLConnection component. The default library file to connect mysql database is dbexpmysql.dll. But, when you use mysql 5.0 this library file obviously useless. So, what's the library file that we should use to connect mysql database? The answer is Dbxopenmysql50.dll(you can search it by google) or download here.

Before connecting to mysql database, you have to know about these points:
1.Your mysql username
2.Your mysql Password
3.Which database that you want to connect.

Ok, now I give you several steps that you have to do. I assuming you that you already have a dbxopenmysql50.dll:
1. Add the following lines to dbxdrivers.ini. This is normally located in C:\Program Files\Common Files\Borland Shared\DBExpress\dbxdrivers.ini

[Installed Drivers]
OpenMySQL50=1

[OpenMySQL50]
LibraryName=dbxopenmysql50.dll
GetDriverFunc=getSQLDriverMYSQL50
VendorLib=libmysql.dll
2. Add to dbxconnections.ini
This is normally located in
C:\Program Files\Common Files\Borland Shared\DBExpress\dbxconnections.ini

[OpenMySQL50]
DriverName=OpenMySQL50
HostName=ServerName
Database=DBNAME
User_Name=root
Password=password
BlobSize=-1

3. Place dbxopenmysql50.dll and libmysql.dll at same path with your project file.
4. Make a data module, place TSQLConnection
5. Right clicking TSQLConnection, it's appears pop-up menu, choose edit connection Properties
New form appears. Choose OpenMySQL50, then setting up some values of your connection like this

Hostname= your computername
Database=your database name
user_name=your mysql username
password=your mysql password

Then click OK
4.Invoke object inspector, change some values of TSQLConnection properties like this:

loginprompt=false
GetDriverfunc=getSQLDriverMYSQL50
LibraryName=dbxopenmysql50.dll
VendorLib=libmysql.dll

You can check your connection by changing connected property to true. If your connection was successful you would have no message, but on the contrary you would get error message.

The information about all tables in mysql is stored in the table that we recognize it as Information_schema.tables . There are two fields that usually getting involved to display table names,firstly table_name and secondly table_schema.

select table_name from information_schema.tables where table_schema='Your database';

The table_schema field inform you about database name and table_name field for table name.

What's the purpose of displaying table names? It is usually used when we want to restore or transfer data by table per table. How to restore database manually(table per table)? See for the next article.

When you open your application for the first time, it reads Tformatsettings variables. Tformatsettings defines a structure that used to format numbers, date-times values and currency values. However, if you change these variables, is not influenced anything with your windows regional settings.

To update regional setting globally you have to use Setlocaleinfo function(see WinSDK for complete explanation). In this tutorial, I just give you an example the implementation of this function(changing decimal symbol). See this code bellow:

Example OnFormCreate

procedure TForm1.FormCreate(Sender: TObject);

begin
setlocaleinfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDECIMAL,'.');
end;

However this function has a weakness because you have to reopen your application to get the effect. So, if you want to update your setting automatically you have to add these function:

postmessage(HWND_BROADCAST,WM_WININICHANGE,0,0);
SendNotifyMessage(HWND_BROADCAST,WM_WININICHANGE,0,0);

As you know, in Delphi there are many components that we can use to call stored procedure. ADOStoredproc for ADO(DBGo), SQLStoredProc for Dbexpress and StoredProc for BDE.

However, those components sometimes are not compatible with server database. Instead of using those components, an easy way to call mysql stored procedure is using execute method(BDE or ADO) or executeDirect(DBExpress).

Here is example:

For Ado:
adoconnection1.Execute('call testproc('+
inttostr(6) + ')');


BDE
database1.Execute('call testproc('+
inttostr(5) + ')');

Dbexpress
SQLConnection1.Executedirect('call testproc('+
inttostr(6) + ')');



note:
If you use DBExpress as your connection you must have dbxopenmysql50.dll library.

Have you ever got this error? These are possibilities:

1.visible value of your form property is true
2.formstyle value of your form property is FsMDICHILD. Replace with FSNORMAL
3.enabled value of your form property is false.

friend links

Ray Blogs