1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| function DBConnect : boolean;
begin
Result := false;
// Init des paramètres pour le serveur MySQL
if mySQL_Res <> nil then
mysql_free_result(mySQL_Res);
mySQL_Res := nil;
if mySQLhandle <> nil then
begin
mysql_close(mySQLhandle);
mySQLhandle := nil;
end;
// Init du serveur MySQL
mySQLhandle := mysql_init(nil);
if mySQLhandle = nil then
begin
Verbose(VRB_ERROR, '*** mysql_init failed !!! ***', []);
Result := false;
Exit;
end;
// Tentative de connexion à la base de données
if mysql_real_connect(mySQLhandle, pChar(CfgCampi.GetMgrServerHost), pChar(CfgCampi.GetMgrDbUsername),
pChar(CfgCampi.GetMgrDbUserpass), pChar(CfgCampi.GetMgrDbPath), 0, nil, CLIENT_MULTI_STATEMENTS) = nil then
begin
Verbose(VRB_ERROR, '*** Connexion to database failed !!! ***', []);
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
Result := false;
Exit;
end
else
begin
Verbose(VRB_INFO, 'Connected to MySQL server: %s', [UnicodeString(mysql_get_server_info(mySQLhandle))]);
end;
// Fonction à lancer après chaque connexion
if (mysql_set_character_set(mySQLhandle, pChar('UTF8')) <> 0) then
begin
Verbose(VRB_ERROR, 'MySQL Error: %s', [UnicodeString(mysql_error(mySQLhandle))]);
end;
Result := not (Result);
end; |
Partager