Title: How to check and install MyODBC driver
Question: How can I check if MySQL ODBC driver is installed and how can I Install it from my Delphi application
Answer:
Use these two functions to check or install MyODBC driver. Note that you have to provide myodbc.dll file by yourself.
Functions were tested with the MyODBC driver version 2.50 on Win98 and Win2k.
8
////////////////////////////////////////////////////////////////////////////////
// //
// CheckMyODBCDriver //
// ----------------- //
// //
// Checks installation of the MyODBC driver //
// //
// Input: //
// (none) //
// //
// Result [Byte]: //
// 0: MySQL driver installed //
// 1: missing registry values //
// 2: missing file myodbc.dll //
// //
////////////////////////////////////////////////////////////////////////////////
function CheckMyODBCDriver: Byte;
var fReg: tRegistry;
bDriversValue, bMySQLKey, bMySQLValues: Boolean;
sDriver, sSetup: String;
begin
// checking the registry
try
fReg := TRegistry.Create;
fReg.RootKey := HKEY_LOCAL_MACHINE;
// checking key entry \Software\ODBC\ODBCINST.INI\ODBC Drivers
fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\ODBC Drivers', True );
bDriversValue := fReg.ValueExists( 'MySQL' ) and
( fReg.ReadString( 'MySQL' ) = 'Installed' );
// checking key entry \Software\ODBC\ODBCINST.INI\MySQL
bMySQLKey := fReg.KeyExists( '\Software\ODBC\ODBCINST.INI\MySQL' );
// if exists key entry \Software\ODBC\ODBCINST.INI\MySQL
// check also the values
if bMySQLKey then begin
fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\MySQL', True );
bMySQLValues := fReg.ValueExists( 'APILevel' ) and
fReg.ValueExists( 'ConnectFunctions' ) and
fReg.ValueExists( 'Driver' ) and
fReg.ValueExists( 'DriverODBCVer' ) and
fReg.ValueExists( 'FileExtns' ) and
fReg.ValueExists( 'FileUsage' ) and
fReg.ValueExists( 'Setup' ) and
fReg.ValueExists( 'SQLLevel' );
if bMySQLValues then begin
sDriver := Trim( fReg.ReadString( 'Driver' ) );
sSetup := Trim( fReg.ReadString( 'Setup' ) );
end;
end else
bMySQLValues := False;
if ( bDriversValue and bMySQLKey and bMySQLValues ) then Result := 0
else Result := 1;
fReg.CloseKey;
fReg.Free;
except
Result := 1;
fReg.Free;
Exit;
end;
// if registry entries OK = check driver files ...
if ( Result = 0 ) then begin
if not ( FileExists( sDriver ) and FileExists( sSetup ) ) then Result := 2;
end;
end;
////////////////////////////////////////////////////////////////////////////////
// //
// InstallMyODBCDriver //
// ------------------- //
// //
// Installs MyODBC driver //
// //
// Input: //
// sDllLocation - location of the file myodbc.dll [String] //
// //
// Result [Byte]: //
// 0: MyODBC succesfuly installed //
// 1: file myodbc.dll does not exist //
// 2: error accesing registry //
// //
////////////////////////////////////////////////////////////////////////////////
function InstallMyODBCDriver( sDllLocation: String ): Byte;
var fReg: tRegistry;
begin
if not FileExists( sDllLocation ) then begin
Result := 1;
Exit;
end;
if ( CheckMyODBCDriver = 0 ) then begin
Result := 0;
end else begin
try
fReg := TRegistry.Create;
fReg.RootKey := HKEY_LOCAL_MACHINE;
fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\ODBC Drivers', True );
fReg.WriteString( 'MySQL', 'Installed' );
fReg.CloseKey;
fReg.CreateKey( '\Software\ODBC\ODBCINST.INI\MySQL' );
fReg.OpenKey( '\Software\ODBC\ODBCINST.INI\MySQL', True );
fReg.WriteString( 'APILevel', '2' );
fReg.WriteString( 'ConnectFunctions', 'YYN' );
fReg.WriteString( 'Driver', sDllLocation );
fReg.WriteString( 'DriverODBCVer', '02.50' );
fReg.WriteString( 'FileExtns', '*.txt' );
fReg.WriteString( 'FileUsage', '0' );
fReg.WriteString( 'Setup', sDllLocation );
fReg.WriteString( 'SQLLevel', '1' );
fReg.CloseKey;
fReg.Free;
Result := 0;
except
fReg.Free;
Result := 2;
end;
end;
end;