Link to home
Start Free TrialLog in
Avatar of vinhphucvn
vinhphucvn

asked on

Problem with delphi and mysql

Hi!
How are you?
I have problem with delphi and MySql, i can not use boolean type in MySql, and i use ADODB in delphi to access database, so i can not use .asboolean in delphi, do you have any solution to use boolean type with delphi and mysql? Thanks alot.
Avatar of wimmeyvaert
wimmeyvaert

When you look at the Field-Datatype in Delphi of a BOOLEAN field in MySQL, then you see 'TSmallIntField'.

That's why you cannot use .AsBoolean method, because internally, Delphi treats this field as an integer.

When you use .AsInteger, everything works fine.

So, in MySQL :
     False = 0
     True = 1

So, in Delphi :
     if Datasource1.DataSet1.FieldByName('YourBoolField').AsInteger = 1 then
           ShowMessage('True')
      else
           ShowMessage('False')


Best regards,

The Mayor.
Or you could make a very little function your own :
function AsMySQLBoolean(Value: Integer): Boolean;
begin
  Result := ( Value = 1 );
end;


And call it like this :
  if AsMySQLBoolean( DataSource1.DataSet.FieldByName('YourBoolField').AsInteger ) then
     ShowMessage( 'True' )
  else
     ShowMessage( 'False' );


instead of the original call :
  if DataSource1.DataSet.FieldByName('YourBoolField').AsInteger = 1 then
     ShowMessage( 'True' )
  else
     ShowMessage( 'False' );


This is of course just a workaround, but makes the code a bit more readable.
Hm, when you use the datatype 'BIT' in MySQL, then you get a real BooleanField in Delphi.

Is it possible to change the datatype of the MySQL-Fields into BIT ?

This should solve your problem then.
Avatar of vinhphucvn

ASKER

Dear Wimmeyvaert!
If i create new function to change, it's no problem, but i want to use .asBoolean in delphi.
If no way to use, i accept your comment, i will create new function.
I Change into BIT but have an automatically change into TinyInt(1).
Thanks a lot!
Wait for your answer.
Hm, That's strange.
I created a MySQL database with a Table in it that has a field of type BIT(1).

When I do Ctrl-F in my TADOTable's FieldEditor, then I can see that this field is a TBooleanField'.

And I can use it as such. (.AsBoolean, ....)

Even when I create a table with a BOOLEAN (= TINYINT(1) ) field and afterwards change it to BIT, then it remains BIT and not TINYINT(1).

Are you sure you applied your changes ?

I should work OK.
i don't know why, please tell me more about MySQL version what you use.
Thanks, i will check again.
ASKER CERTIFIED SOLUTION
Avatar of wimmeyvaert
wimmeyvaert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And what are your versions ?
Do you have older/newer version than I have ?
Did you get it to work ?

I'm curious now ;-)
it's not work but i can solve by function.
Do you know how to write the code to automatic register database to ODBC. i want to register by my programe ( by delphi). Thanks.
i don't like MySQL but i have project with delphi and mySQL. Can i contact you on MSN or yahoo Messenger? Thanks
Maybe this will do the trick. Not tested by me though.

procedure CreateODBC_SystemDsn( DriverName, DriverDll, Dsn, Server, Db, User, Pwd: String );
begin
  with TRegistry.Create do
    begin
      RootKey := HKEY_LOCAL_MACHINE;

      OpenKey( 'Software\ODBC\ODBC.INI\'+Dsn, True);
      WriteString( 'Driver'   , DriverDll );
      WriteString( 'SERVER'   , Server );
      WriteString( 'DATABASE' , Db );
      WriteString( 'UID'      , User );
      WriteString( 'PWD'      , Pwd );
      CloseKey;
      OpenKey( 'Software\ODBC\ODBC.INI\ODBC Data Sources', True );
      WriteString( Dsn, DriverName );
      Free;
    end; {with TRegistry}
end;


Call it like :
  CreateODBC_SystemDsn( 'MySQL ODBC 3.51 Driver', 'C:\Windows\system32\myodbc3.dll', 'YourDSNName', 'YourServer', 'UserName', 'Password' );


Maybe you can try it out.