Problem saving data in database
#1

Hello, so I have this script from a tutorial on this forum, the problem is that does not save any time in the mysql database .Conectare database is done successfully
Код:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>

#include <a_samp> // Our main include; rather obvious why we need this.
#include <a_mysql> // Our MySQL include; R39-2

#include <sscanf2> // Sscanf2 our /create command.
#include <zcmd> // And ZCMD as our command processor.


// Defining the size of the enum we'll be storing info in.
#define MAX_SOMETHING      (50)

// As for the enumerator...
enum SomethingInfo
{
	s_ID, // Our ID, you should probably always add this into your enumerator.

	s_Text[64], // The variables we want to load/save...
	s_Number,
	Float: s_Float
};

// And create a variable for it using our previously defined expression as array size.
new Something[MAX_SOMETHING][SomethingInfo];

// Alongside that, create another variable called "xCount", or whatever you want to call it.
// This will be used in the "CreateSomething" function to determine how many "Somethings" have been created already.
new SomethingCount;

// Defining our MySQL handle.
new mysql;

main()
{
	// Setting up our MySQL settings...
    mysql_log(LOG_ALL); // Setting up our logging: LOG_ALL logs everything (errors, warnings, and debug messages) into a text file called mysql_log.txt.
    mysql = mysql_connect("localhost", "root", "server", ""); // Storing our MySQL data into our handle.
    // Please note: this was used on a local host (as the hostname suggests). If you are using a host, you should use a different name than of "root".

	// Using mysql_tquery, we'll format a query which will load data from the database, and which is retrieved from an alternate callback using cache.
	// Params: 		(mysql handle, query, callback, specifier)
	// Please note: you're not actually obliged to use every param unless you use them. As such:
	mysql_tquery(mysql, "SELECT * FROM Somethings", "SQL_LoadSomething"); // Only using three params instead of all.

	// To finish up our MySQL callback, we'll add two debug messages which will tell us if we connected to the database.
    if(mysql_errno(mysql) != 0) return print("Could not connect to database!"); // If mysql_errno (error message) isn't 0/false, print a debug message.
	return print("Connected to database!"); // Else, print another debug message but this time confirming our connection.
}

// Start with a public function and forward it...
forward SQL_LoadSomething();
public SQL_LoadSomething()
{
	// If cache_num_rows returns false or '0', return an error message which let's us know there are no rows to load.
	if(!cache_get_row_count()) return printf("cache_get_row_count returned false. There are no rows to load.");

	// Create a loop which does not stop unless "i" is equal to "j", which holds the same value as "cache_get_row_count()". You should use a variable because it is faster than calling a native function.
 	for(new i, j = cache_get_row_count(); i < j ; i++)
	{
	    // For our "ID" variable, we will use our previous variable. (SomethingCount);
	   	Something[i][s_ID] =                	SomethingCount;

		// For the rest of our variables, simply store the cache data into the variable.
		// The params are as following: (row, field name). Alternatively, you can store your mysql handle.

		// Your mysql handle has to be stored as the last param, as so:
	   	Something[i][s_Number] = 				cache_get_field_content_int(i, "sNumber", mysql);
	   	

	   	// This is not necessary unless you have more than one mysql handle, so let's use the two params for now.
        Something[i][s_Float] = 				cache_get_field_content_float(i, "sFloat");

		// When retrieving strings, our function is a little bit different. The params are as following:
		//      (row, field name, variable to store it in, mysql handle, string length)
	    cache_get_field_content(i, 	"sText", 	Something[i][s_Text], 	mysql, 	64);

		// Send a debug message...
		printf("Loaded something ID %d with text %s, number %d and float %f.", i, Something[i][s_Text], Something[i][s_Number], Something[i][s_Float]);

		// Make our variable count up.
        SomethingCount ++;
	}

	// After everything has loaded, send the last debug message telling us how many rows have been loaded:
	printf("%d somethings have been loaded!", SomethingCount);

	// Start the saving timer which will tick every 15 minutes.
	SetTimer("SaveSomethings", 30000, true);

	return true;
}

// For our saving function, simply create another public function and forward it.

// Functions like this aren't the best method to save stuff though, and you don't need them in MySQL.
// You should save something when you update it instead.
forward SaveSomethings();
public SaveSomethings()
{
	new query[200]; // Create a variable to store our query in.

	// Loop through the value of MAX_SOMETHING the same way we looped in our load function.
	for(new i; i != MAX_SOMETHING; i ++)
	{
		// Using mysql_format to format our query... Params: (connectionhandle, query, query length)
	    mysql_format(mysql, query, sizeof(query), "UPDATE Somethings SET sText = '%s', sNumber = %d, sFloat = %f WHERE ID = %d",
		Something[i][s_Text], Something[i][s_Number], Something[i][s_Float], Something[i][s_ID]);

	    // Sending the threaded query.
	    mysql_tquery(mysql, query); // Only using our handle and the query, as we are not calling a callback.
	}

	// Send a confirmation message...
	print("Saved all somethings!");

	return true;
}
CMD:savetest(playerid, params[])
{
	new query[200]; // Create a variable to store our query in.

	// Loop through the value of MAX_SOMETHING the same way we looped in our load function.
	for(new i; i != MAX_SOMETHING; i ++)
	{
		// Using mysql_format to format our query... Params: (connectionhandle, query, query length)
	    mysql_format(mysql, query, sizeof(query), "UPDATE Somethings SET sText = '%s', sNumber = %d, sFloat = %f WHERE ID = %d",
		Something[i][s_Text], Something[i][s_Number], Something[i][s_Float], Something[i][s_ID]);

	    // Sending the threaded query.
	    mysql_tquery(mysql, query); // Only using our handle and the query, as we are not calling a callback.
	}

	// Send a confirmation message...
	print("Saved all somethings!");

	return true;
}


// For "something" creation, simply start a command...
CMD:createsomething(playerid, params[])
{
	// Define a string for our confirmation message and variables to store data in. Keep in mind; you don't need to add a sscanf parameter
	// for every single variable you use in whatever you're trying to save. If you can pull data using an alternative way, do it.
	new string[80], text[64], number, floatval;

	// Setting up our sscanf specifiers and variables and an error message incase a param is left open.
	if(sscanf(params, "s[64]if", text, number, floatval))
	    return SendClientMessage(playerid, -1, "/createsomething [text] [number] [float]");

	// Format our confirmation message into "string" and send it to the player.
	format(string, sizeof(string), "Created something ID %d with text %s number %d and float %f!", SomethingCount, text, number, floatval);
	SendClientMessage(playerid, -1, string);

	// Call our custom function for "something" creation using the data we just stored.
	SQL_AddSomething(text, number, floatval);

	return true;
}

// Creating a simple function:
SQL_AddSomething(stext[64], number, floatval)
{
	new query[200]; // Create another query..

	// Format it like you usually would, this time using the "INSERT" and "VALUES" commands.
	// You don't have to put any accents around your rows, except for when you are trying to store strings.

	// Also, please keep in mind, my first format specifier is "query", but only because I used two seperate strings to keep my query organized.
	mysql_format(mysql, query, sizeof(query), "INSERT INTO Somethings (sText, sNumber, sFloat)");
	mysql_format(mysql, query, sizeof(query), "%s VALUES ('%s', %d, %f)", query, stext, number, floatval);

	// This isn't mandatory and alternatively you can format queries like this as well:
	mysql_format(mysql, query, sizeof(query), "INSERT INTO Somethings (sText, sNumber, sFloat) VALUES ('%s', %d, %f)", query, stext, number, floatval);

	// After formatting, send a threaded query...
	mysql_tquery(mysql, query);

	// Also, do not forget to store the data you just stored into your database into your variables. This allows for
	// immediate usage of the enum without needing a server restart or "something" reload.
   	Something[SomethingCount][s_ID] 	= 	SomethingCount;

   	Something[SomethingCount][s_Text] 	= 	stext;
   	Something[SomethingCount][s_Number] = 	number;
    Something[SomethingCount][s_Float] 	= 	floatval;

	// Send a confirmation message...
	printf("Created something ID %d with with text %s, number %d and float %f.", SomethingCount,
	Something[SomethingCount][s_Text], Something[SomethingCount][s_Number], Something[SomethingCount][s_Float]);

	// And count up our variable again.
	SomethingCount ++;

	return true;
}
public OnGameModeInit()
{

	mysql_tquery(mysql, "CREATE TABLE IF NOT EXISTS `DynamicLabels` (\
	`ID` int(5) NOT NULL AUTO_INCREMENT UNIQUE KEY,\
	`Text` varchar(74) NOT NULL,\
	`Color` int(8) NOT NULL,\
	`Interior` int(5) NOT NULL,\
	`VirtualWorld` int(5) NOT NULL,\
	`PosX` float NOT NULL,\
	`PosY` float NOT NULL,\
	`PosZ` float NOT NULL)");


	// Don't use these lines if it's a filterscript
	SetGameModeText("Blank Script");
	AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
	return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)