[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1107
  • Last Modified:

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.
0
vinhphucvn
Asked:
vinhphucvn
  • 7
  • 4
1 Solution
 
wimmeyvaertCommented:
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.
0
 
wimmeyvaertCommented:
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.
0
 
wimmeyvaertCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
vinhphucvnAuthor Commented:
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.
0
 
wimmeyvaertCommented:
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.
0
 
vinhphucvnAuthor Commented:
i don't know why, please tell me more about MySQL version what you use.
Thanks, i will check again.
0
 
wimmeyvaertCommented:
Here is all the info about my config :

Delphi 5 Enterprise Edition
Windows XP Professional SP2

MySQL Administrator software : V1.1.7
MySQL Server version : V5.0.18 (as can be seen in MySQL Administrator)
MySQL Client version : V5.0.11 (as can be seen in MySQL Administrator)
I use ODBC-driver to connect to MySQL : V3.51.12.00

Best regards,

The Mayor.
0
 
wimmeyvaertCommented:
And what are your versions ?
Do you have older/newer version than I have ?
Did you get it to work ?

I'm curious now ;-)
0
 
vinhphucvnAuthor Commented:
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.
0
 
vinhphucvnAuthor Commented:
i don't like MySQL but i have project with delphi and mySQL. Can i contact you on MSN or yahoo Messenger? Thanks
0
 
wimmeyvaertCommented:
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now