The queries will remain the same, like "SELECT * FROM table WHERE name = '%s'" for example.
But all the functions don't.
BlueG's plugin adds a new connectionhandle field into the functions:
pawn Код:
// Old way:
mysql_query(Query);
// New way:
mysql_query(connectionHandle, Query, usecache = true); / usecache is an optional parameter
Also, mysql_free_result doesn't do anything.
BlueG's plugin uses cache to store the result.
mysql_query returns the cache-id, which you need to free the cache later on after processing the result using cache_delete.
pawn Код:
// Setup local variables
new Query[1024], Cache:result;
format(Query, sizeof(Query), "SHOW TABLES LIKE '%s'", table_playerdata);
result = mysql_query(SQL_db, Query, true);
if (cache_get_row_count(SQL_db) == 0)
{
printf("MySQL: >> Creating table %s...", table_playerdata);
format(Query, sizeof(Query), "CREATE TABLE `%s` (", table_playerdata);
format(Query, sizeof(Query), "%s`ID` int(11) NOT NULL AUTO_INCREMENT, ", Query);
format(Query, sizeof(Query), "%s`PlayerName` varchar(25) NOT NULL, ", Query);
format(Query, sizeof(Query), "%s`Password` varchar(130) NOT NULL, ", Query);
format(Query, sizeof(Query), "%s`AdminLevel` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`Money` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`Score` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`Fines` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`Tickets` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`TicketCost` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`SpeedInMph` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%s`JailTime` int(11) NOT NULL DEFAULT '0', ", Query);
format(Query, sizeof(Query), "%sPRIMARY KEY (`ID`)) ", Query);
format(Query, sizeof(Query), "%sENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8", Query);
mysql_query(SQL_db, Query, false);
printf("MySQL: >> Table '%s' created", table_playerdata);
}
else
printf("MySQL: Table '%s' exists, skipping table creation and data insertion", table_playerdata);
// Free the result after executing "SHOW TABLES LIKE ..." to prevent memory leaks
cache_delete(result, SQL_db);
This is a piece of code my script uses to check if a table exists.
If it doesn't exist, the table is created using a non-cached mysql_query (due to "false" as third parameter).
Creating a table doesn't return a result, so cache can be disabled.
Checking if the table exists using SHOW TABLES LIKE '%s' does return something, so "true" in the mysql_query tells the plugin to use the cache function to store the result.
After processing the result using cache_get_row_count, the cache is cleared using cache_delete(CacheID) to prevent memory-leaks.
This code also is a part of my own script using BlueG's plugin:
pawn Код:
SQL_Data_Load()
{
// Setup local variables
new Query[128], Cache:result;
format(Query, sizeof(Query), "SELECT * FROM %s", table_vehicleinfo);
result = mysql_query(SQL_db, Query, true);
VehicleInfoLoad();
cache_delete(result, SQL_db);
}
// This function is called to load the vehicle-info
VehicleInfoLoad()
{
// Setup local variables
new Rows, ID, Price, Float:MaxFuel, Float:Consump, RefuelT, Disabled, CountSuccess, CountFailed, Query[128];
// Get the amount of rows (vehicle-info)
Rows = cache_get_row_count(SQL_db);
// If there are any rows (vehicle-info) loaded, load data and overwrite default script-values
if (Rows >= 1)
{
// Loop through all rows
for (new Row; Row < Rows; Row++)
{
// Load the data
ID = cache_get_field_content_int(Row, "ID", SQL_db);
Price = cache_get_field_content_int(Row, "Price", SQL_db);
MaxFuel = cache_get_field_content_float(Row, "MaxFuel", SQL_db);
Consump = cache_get_field_content_float(Row, "Consumption", SQL_db);
RefuelT = cache_get_field_content_int(Row, "RefuelTime", SQL_db);
Disabled = cache_get_field_content_int(Row, "Disabled", SQL_db);
// Check if the ID is invalid (out of range)
if ((ID < 0) || (ID >= sizeof(AVehicleInfo)))
{
// Count the amount of failed vehicle-info entries (invalid ID's)
CountFailed++;
printf("*** ERROR: Invalid ID found in table %s: %i (entry deleted)", table_vehicleinfo, ID);
mysql_format(SQL_db, Query, sizeof(Query), "DELETE FROM %s WHERE ID = '%i'", table_vehicleinfo, ID);
mysql_tquery(SQL_db, Query, "", "");
// Continue with the next vehicle-info entry from the MySQL query
continue;
}
// Store all the data
AVehicleInfo[ID][VehiclePrice] = Price;
AVehicleInfo[ID][VehicleMaxFuel] = MaxFuel;
AVehicleInfo[ID][FuelConsumption] = Consump;
AVehicleInfo[ID][RefuelTime] = RefuelT;
if (Disabled == 1)
AVehicleInfo[ID][VehicleDisabled] = true;
else
AVehicleInfo[ID][VehicleDisabled] = false;
// Count the succesfully loaded vehicle-info entries
CountSuccess++;
}
}
// Print the amount of vehicle-info entries loaded for debugging
printf("*** Vehicle-info loaded: %i (successful: %i, failed: %i)", Rows, CountSuccess, CountFailed);
return 1;
}
This code shows the loading part of my vehicle-info table.
The gathering of data from a result will also be alot different from your other plugin I guess.
Also different is the mysql_format function.
It allows the usage of the %e specifier instead of %s using "format".
The %e is actually the same as using mysql_real_escape_string.
The given string is automatically escaped using the %e, so there is no need to call mysql_real_escape_string, ever.
Also new to you might be the mysql_tquery.
This is a threaded query, which is sent to MySQL, but your script doesn't have to wait until it has completed processing.
The DELETE queries in this code are done using threaded queries.
So, the deletion of invalid vehicle-entries can be done whenever MySQL finds the time to process them.
PS: sorry for my long posts.