Updating Mysql (Str,Int,Flo)
#1

I have my functions like that
Код:
public UpdateOCarInt(qCarId, qValue[], qInt)
{
	new query[140];
	format(query, sizeof(query), "UPDATE ownablecars SET %s=%d WHERE ID=%d", qValue, qInt, qCarId);
	mysql_function_query(gSQL, query, false, "SendQuery", "");
	return 1;
}
public UpdateOCarStr(qCarId, qValue[], Str)
{
	new query[140];
	format(query, sizeof(query), "UPDATE ownablecars SET %s=%s WHERE ID=%d", qValue, qInt, qCarId);
	mysql_function_query(gSQL, query, false, "SendQuery", "");
	return 1;
}
public UpdateOCarFlo(qCarId, qValue[], Float:qFlo)
{
	new query[140];
	format(query, sizeof(query), "UPDATE ownablecars SET %s=%f WHERE ID=%d", qValue, qFlo, qCarId);
	mysql_function_query(gSQL, query, false, "SendQuery", "");
	return 1;
}
But is it possible to do all this with only one function, so i could use it in that way?

Код:
UpdateOCar(car, "PosX", FloatToStr(ScriptCarInfo[car][PosX]));
UpdateOCar(car, "OwnerName", StringToStr(ScriptCarInfo[car][OwnerName]));
UpdateOCar(car, "OwnerID", IntToStr(ScriptCarInfo[car][OwnerID]));
I mean, that i wouldnt need to write this in that way:
Код:
UpdateOCarFlo(car, "PosX", ScriptCarInfo[car][PosX]);
UpdateOCarStr(car, "OwnerName", ScriptCarInfo[car][OwnerName]);
UpdateOCarInt(car, "OwnerID", ScriptCarInfo[car][OwnerID]);
If my code is wrong above, its okay, important is the point about that. :d
If it is possible, could you help me? Because i am tired of writing almost the same functions so many times :P
Thanks.
Reply
#2

Creating wrappers is almost never a good idea. You'll just be sending a lot more queries than would be strictly necessary (which is bad). Take a look at ORM: https://sampforum.blast.hk/showthread.php?tid=461766.
Reply
#3

Mm...But why it is a bad idea? That would be good to save things one by one, no? For example when i sell car, i can change the owner with that function and so i don`t have to save this anymore while gamemodeexit?

Actually i don`t know much about that, it seemed much easier and better. But how would u do then


//

When GameModeExit, then i could save all cars without that function, and when i for example buy a car, i could use that function to save stats one by one? If it is smarter, then back to my first question, how to do that :P
Reply
#4

I would've done it like this:

pawn Код:
function updateCar(carEnum) {
  format(str, sizeof(str), "UPDATE `TableName` SET `OwnerName`='%s', `OwnerID` = %d, `PosX` = %f", carEnum[OwnerName], carEnum[OwnerID], carEnum[PosX]);
  execute_query(str);
}
You can then call the function like this: updateCar(ScriptCarInfo[car]);
Reply
#5

Yeah thanks, but to my first post. How to update them as i showed above, one by one. Float, string and int all with one function :P
Reply
#6

Quote:
Originally Posted by Larry123
Посмотреть сообщение
Yeah thanks, but to my first post. How to update them as i showed above, one by one. Float, string and int all with one function :P
Well, if it was php then you could've checked what type the variable was, but pawn is strict in that. What I posted is the closest you'll get, I am afraid.
Reply
#7

Well, there is always tagof operator. Search for "utilising tagof" thread on this forum.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)