Bild:
Eine Funktion, um zu überprüfen, ob Tabellen in einer Datenbank vorhanden sind.
Voraussetzungen:
• MySQL Plugin von G-sTyLeZzZ
• MySQL Datenbank mit Verbindungsdaten
Gesamte Funktion:
(Pastebin)
stock mysql_table_existence(amount, tables[][], connection = 1)
{
print("\n[MySQL] Checking if connection exists.");
if(mysql_ping(connection) == 1)
{
print("[MySQL] Connection exists. Checking tables.\n");
// Check if there are tables to check
if(!strlen(tables[0]))
{
print("[MySQL] Script won't work. No tables avaible to check.");
print("[MySQL] Table checking done. Have fun!\n");
gDatabaseError = true;
return 1;
}
// Storage, Query, MissingTables Counter, FoundTables Counter
// [0] = Query
// [1] = Storage
new string[2][64],
missingTables = 0,
foundTables = 0;
for(new t=0; t<amount; t++)
{
// Create Query
format(string[0], 64, "SHOW TABLES LIKE '%s'", tables[t]);
mysql_query(string[0], -1, -1, connection);
mysql_store_result(connection);
// Check if it exists
while(mysql_fetch_row_format(string[1], "|", connection))
{
if(!strcmp(string[1], tables[t])) { gDatabaseError = false; break; }
else gDatabaseError = true;
}
// Anti-Empty-Failure
if(!string[1][0]) gDatabaseError = true;
// Output Message
if(gDatabaseError == true)
printf("[MySQL] Table '%s' not found.", tables[t]);
else
printf("[MySQL] Table '%s' found.", tables[t]);
// Release Memory and Reset
mysql_free_result(connection);
string[0][0] = '\0';
string[1][0] = '\0';
if(gDatabaseError == true) ++missingTables;
else ++foundTables;
gDatabaseError = false;
}
// Output Error, when a table is missed
if(missingTables >= 1)
{
if(missingTables == 1) printf ("\n[MySQL] Script won't work. One table wasn't found.");
else printf ("\n[MySQL] Script won't work. %d tables weren't found.", missingTables);
printf("[MySQL] Table checking done. Have fun!\n");
gDatabaseError = true;
}
else
{
if(foundTables == 1) printf("\n[MySQL] No table is missing. One table found.");
else printf("\n[MySQL] No table is missing. %d tables found.", foundTables);
printf("[MySQL] Table checking done. Have fun!\n");
gDatabaseError = false;
}
printf("[MySQL] Copyright (c) [OSF]Atom - Table Check\n");
return 1;
}
else
{
print("[MySQL] Connection doesn't exist. Aborting.\n");
// No connection - no databas'es
gDatabaseError = true;
return 0;
}
}
Verwendung:
new table[][] = {"alpha", "beta", "charlie", "delta"};
mysql_table_existence(4, table);
Parameter:
amount = Anzahl der Tabellen
tables[][] = Der Array mit den Tabellen-Namen
connection = Falls jemand eine spezielle Verbindung benutzt
Sonstige Voraussetzungen:
new bool:gDatabaseError = false;
Hinweise:
Wenn (gDatabaseError == true) ist, dann könnt ihr den Spieler vorm Registrieren,
was vielleicht mehr Fehler gibt, aufhalten und rauskicken.
Beispiel:
public OnPlayerConnect(playerid)
{
// When a table or something else is broken
if(gDatabaseError == true)
{
ShowPlayerDialog(playerid, 0, 0, "Database Error", "Sie wurden vom Server entfernt, da ein Problem mit der Datenbank vorliegt, um jegliche Fehler zu vermeiden.\n\nFreundliche Grüße\nDas Team", "Schließen", "");
Kick(playerid);
return 0;
}
return 1;
}
Freundliche Grüße
Martez