SA-MP Forums Archive
Saving car information - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Saving car information (/showthread.php?tid=190782)



Saving car information - Typhome - 16.11.2010

sadasdasdasd


Re: Saving car information - JaTochNietDan - 16.11.2010

What exactly do the "MySQLUpdatePlayer*" functions do?


Re: Saving car information - Grim_ - 16.11.2010

- Why do you connect the database inside the function?
- Why do you store a result for no reason at all, when you haven't run a query?
- As JaTochNietDan said, what does "MySQLUpdatePlayer" function do?


Re: Saving car information - Typhome - 16.11.2010

sadasdasdasd


Re: Saving car information - Grim_ - 16.11.2010

You should connect tot he database at the beginning of server start-up.

And you are destroying the point of MySQL basically - you are using it like a file function, writing one variable to the table at a time. Look into more structural uses of MySQL.


Re: Saving car information - JaTochNietDan - 16.11.2010

Ok well it would appear that using your system, you need to execute the query string then at the very end, which you have not done, therefore it's not saving.

Also there's no need for

mysql_store_result();
mysql_free_result();

As you're not selecting any information from the database in that entire snippet of code.

Additionally it appears you create a string in the local scope and it is never used.

The end result is this:

pawn Код:
public SaveCar() // mySQL
{
    MySQLConnect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,MYSQL_DB);
    for(new idx = 115; idx < CARINFOMAX; idx++)
    {
        MySQLCheckConnection();
        new query[MAX_STRING];
        format(query, MAX_STRING, "UPDATE cars SET ");
        MySQLUpdatePlayerStr2(query, idx, "Model", CarInfo[idx][cModel]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationx", CarInfo[idx][cLocationx]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationy", CarInfo[idx][cLocationy]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationz", CarInfo[idx][cLocationz]);
        MySQLUpdatePlayerFlo2(query, idx, "Angle", CarInfo[idx][cAngle]);
        MySQLUpdatePlayerStr2(query, idx, "ColorOne", CarInfo[idx][cColorOne]);
        MySQLUpdatePlayerStr2(query, idx, "ColorTwo", CarInfo[idx][cColorTwo]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Owner", CarInfo[idx][cOwner]);
        MySQLUpdatePlayerStr2(query, idx, "Description", CarInfo[idx][cDescription]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Value", CarInfo[idx][cValue]);
        MySQLUpdatePlayerStr2(query, idx, "License", CarInfo[idx][cLicense]);
        MySQLUpdatePlayerStr2(query, idx, "Owned", CarInfo[idx][cOwned]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Paintjob", CarInfo[idx][cPaintjob]);
        MySQLUpdatePlayerStr2(query, idx, "mod1", CarInfo[idx][cmod1]);
        MySQLUpdatePlayerStr2(query, idx, "mod2", CarInfo[idx][cmod2]);
        MySQLUpdatePlayerStr2(query, idx, "mod3", CarInfo[idx][cmod3]);
        MySQLUpdatePlayerStr2(query, idx, "mod4", CarInfo[idx][cmod4]);
        MySQLUpdatePlayerStr2(query, idx, "mod5", CarInfo[idx][cmod5]);
        MySQLUpdatePlayerStr2(query, idx, "mod6", CarInfo[idx][cmod6]);
        MySQLUpdatePlayerStr2(query, idx, "mod7", CarInfo[idx][cmod7]);
        MySQLUpdatePlayerStr2(query, idx, "mod8", CarInfo[idx][cmod8]);
        MySQLUpdatePlayerStr2(query, idx, "mod9", CarInfo[idx][cmod9]);
        MySQLUpdatePlayerStr2(query, idx, "mod10", CarInfo[idx][cmod10]);
        MySQLUpdatePlayerStr2(query, idx, "mod11", CarInfo[idx][cmod11]);
        MySQLUpdatePlayerStr2(query, idx, "mod12", CarInfo[idx][cmod12]);
        MySQLUpdatePlayerStr2(query, idx, "mod13", CarInfo[idx][cmod13]);
        MySQLUpdatePlayerStr2(query, idx, "mod14", CarInfo[idx][cmod14]);
        MySQLUpdatePlayerStr2(query, idx, "mod15", CarInfo[idx][cmod15]);
        MySQLUpdatePlayerStr2(query, idx, "mod16", CarInfo[idx][cmod16]);
        MySQLUpdatePlayerStr2(query, idx, "mod17", CarInfo[idx][cmod17]);
        //----------------------------------------------------------------------
        mysql_query(query);
    }
}
Note to Grim_: Actually he is not executing a query each time, I thought that at first also which is indeed quite silly, but that is not what he is doing, those functions actually build a large query string, which should then be executed, which is what he forgot to do.

Also of course Grim_ is correct, you really should have a connection open when the server is started, each time a function is called.


Re: Saving car information - Typhome - 16.11.2010

sadasdasdasd


Re: Saving car information - JaTochNietDan - 16.11.2010

What is the contents of the MySQLCheckConnection() function?

I highly suspect having that in a loop is going to cause a problem.


Re: Saving car information - Typhome - 16.11.2010

sadasdasdasd


Re: Saving car information - JaTochNietDan - 16.11.2010

Okay, I recommend trying the following:
pawn Код:
public SaveCar() // mySQL
{
    MySQLCheckConnection();
    for(new idx = 115; idx < CARINFOMAX; idx++)
    {
        new query[MAX_STRING];
        format(query, MAX_STRING, "UPDATE cars SET ");
        MySQLUpdatePlayerStr2(query, idx, "Model", CarInfo[idx][cModel]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationx", CarInfo[idx][cLocationx]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationy", CarInfo[idx][cLocationy]);
        MySQLUpdatePlayerFlo2(query, idx, "Locationz", CarInfo[idx][cLocationz]);
        MySQLUpdatePlayerFlo2(query, idx, "Angle", CarInfo[idx][cAngle]);
        MySQLUpdatePlayerStr2(query, idx, "ColorOne", CarInfo[idx][cColorOne]);
        MySQLUpdatePlayerStr2(query, idx, "ColorTwo", CarInfo[idx][cColorTwo]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Owner", CarInfo[idx][cOwner]);
        MySQLUpdatePlayerStr2(query, idx, "Description", CarInfo[idx][cDescription]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Value", CarInfo[idx][cValue]);
        MySQLUpdatePlayerStr2(query, idx, "License", CarInfo[idx][cLicense]);
        MySQLUpdatePlayerStr2(query, idx, "Owned", CarInfo[idx][cOwned]);
        //----------------------------------------------------------------------
        MySQLUpdatePlayerStr2(query, idx, "Paintjob", CarInfo[idx][cPaintjob]);
        MySQLUpdatePlayerStr2(query, idx, "mod1", CarInfo[idx][cmod1]);
        MySQLUpdatePlayerStr2(query, idx, "mod2", CarInfo[idx][cmod2]);
        MySQLUpdatePlayerStr2(query, idx, "mod3", CarInfo[idx][cmod3]);
        MySQLUpdatePlayerStr2(query, idx, "mod4", CarInfo[idx][cmod4]);
        MySQLUpdatePlayerStr2(query, idx, "mod5", CarInfo[idx][cmod5]);
        MySQLUpdatePlayerStr2(query, idx, "mod6", CarInfo[idx][cmod6]);
        MySQLUpdatePlayerStr2(query, idx, "mod7", CarInfo[idx][cmod7]);
        MySQLUpdatePlayerStr2(query, idx, "mod8", CarInfo[idx][cmod8]);
        MySQLUpdatePlayerStr2(query, idx, "mod9", CarInfo[idx][cmod9]);
        MySQLUpdatePlayerStr2(query, idx, "mod10", CarInfo[idx][cmod10]);
        MySQLUpdatePlayerStr2(query, idx, "mod11", CarInfo[idx][cmod11]);
        MySQLUpdatePlayerStr2(query, idx, "mod12", CarInfo[idx][cmod12]);
        MySQLUpdatePlayerStr2(query, idx, "mod13", CarInfo[idx][cmod13]);
        MySQLUpdatePlayerStr2(query, idx, "mod14", CarInfo[idx][cmod14]);
        MySQLUpdatePlayerStr2(query, idx, "mod15", CarInfo[idx][cmod15]);
        MySQLUpdatePlayerStr2(query, idx, "mod16", CarInfo[idx][cmod16]);
        MySQLUpdatePlayerStr2(query, idx, "mod17", CarInfo[idx][cmod17]);
        //----------------------------------------------------------------------
        mysql_query(query);
    }
}
Now this won't fully work either, I think the issue here is how you're using it to modify car information. It's originally intended to modify player information, and it's counting on the fact that there is an ID column, and that the ID column is the ID of the player. For this to work, you need an ID column in your car information table, and you need to change the idx variable in the loop to the ID's of the rows in the table with the car information.


Re: Saving car information - Typhome - 17.11.2010

sadasdasdasd


Re: Saving car information - Typhome - 18.11.2010

buuumpppp.........


Re: Saving car information - NewbBeginner - 18.11.2010

This fails ...


Re: Saving car information - Scenario - 18.11.2010

@ JaTochNietDan; Wouldn't using "sscanf" be a better option though? Instead of 20+ lines for one function, why not ~7 lines, most likely going a hell of a lot faster?

Example;

pawn Код:
stock LoadVehiclesFromDatabase()
{
    new str[256], index;
    mysql_query("SELECT * FROM `Vehicles`");
    mysql_store_result();
    if(mysql_num_rows() > 0)
    {
        while(mysql_fetch_row(str))
        {
            sscanf(str, "e<p<|>dffffdd>", Vehicles[index]);
            CreateVehicle(Vehicles[index][vModelID], Vehicles[index][vPositionX], Vehicles[index][vPositionY], Vehicles[index][vPositionZ], Vehicles[index][vAngleZ], Vehicles[index][vColor1], Vehicles[index][vColor2], -1);
            index++;
        }
    }
    mysql_free_result();
    print("\n");
    printf("SERVER: Loaded %d MySQL vehicles successfully.", index);
    return 1;
}



Re: Saving car information - JaTochNietDan - 18.11.2010

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
@ JaTochNietDan; Wouldn't using "sscanf" be a better option though? Instead of 20+ lines for one function, why not ~7 lines, most likely going a hell of a lot faster?

Example;

pawn Код:
stock LoadVehiclesFromDatabase()
{
    new str[256], index;
    mysql_query("SELECT * FROM `Vehicles`");
    mysql_store_result();
    if(mysql_num_rows() > 0)
    {
        while(mysql_fetch_row(str))
        {
            sscanf(str, "e<p<|>dffffdd>", Vehicles[index]);
            CreateVehicle(Vehicles[index][vModelID], Vehicles[index][vPositionX], Vehicles[index][vPositionY], Vehicles[index][vPositionZ], Vehicles[index][vAngleZ], Vehicles[index][vColor1], Vehicles[index][vColor2], -1);
            index++;
        }
    }
    mysql_free_result();
    print("\n");
    printf("SERVER: Loaded %d MySQL vehicles successfully.", index);
    return 1;
}
Sure, if he was trying to select information from the database, but in fact that is not what this topic is about, this topic is about updating information in the database, and the method he uses is just fine, all it does is build a string and execute it all at the same time.

Anyway as for the original poster, I think you need to look into MySQL a bit more and understand that the functions you're using are created for certain table designs and won't work with any random new table you've created, you'll have to create separate similar functions for that this particular method you're using to work with your new car information table, as the columns, table name etc are different.


Re: Saving car information - Scenario - 19.11.2010

Quote:
Originally Posted by JaTochNietDan
Посмотреть сообщение
Sure, if he was trying to select information from the database, but in fact that is not what this topic is about, this topic is about updating information in the database, and the method he uses is just fine, all it does is build a string and execute it all at the same time.

Anyway as for the original poster, I think you need to look into MySQL a bit more and understand that the functions you're using are created for certain table designs and won't work with any random new table you've created, you'll have to create separate similar functions for that this particular method you're using to work with your new car information table, as the columns, table name etc are different.
Hm, maybe I was wrong. I still think using "sscanf" to save data would work though.


Re: Saving car information - Typhome - 20.11.2010

sadasdasdasd


Re: Saving car information - Typhome - 21.11.2010

Bump!


Re: Saving car information - NewbBeginner - 21.11.2010

Код:
stock SaveVehicleToDatabase(vehicleid)
{
	new ModelID, Color1, Color2;
	new Float:PositionX, Float:PositionY, Float:PositionZ, Float:AngleZ;
	ModelID = GetVehicleModel(vehicleid);
	GetVehiclePos(vehicleid, PositionX, PositionY, PositionZ);
	GetVehicleZAngle(vehicleid, AngleZ);
	GetVehicleColor(vehicleid, Color1, Color2);

    format(Query, sizeof(Query), "INSERT INTO `Vehicles` (ModelID, PositionX, PositionY, PositionZ, AngleZ, Color1, Color2) VALUES(%d, %f, %f, %f, %f, %d, %d)", ModelID, PositionX, PositionY, PositionZ, AngleZ, Color1, Color2);
	mysql_query(Query);
	return 1;
}



Re: Saving car information - Typhome - 21.11.2010

sadasdasdasd