SA-MP Forums Archive
Transfering vehicles to MySQL - 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: Transfering vehicles to MySQL (/showthread.php?tid=249224)



Transfering vehicles to MySQL [Solved] - Patrik356b - 17.04.2011

Hey, we are having issues about getting started with mysql..

Basically, we just want to transfer the content from vehicles.cfg to a mysql table

Several attempts has been made, here is an example:

pawn Код:
new V_Counter=0;

public OnFilterScriptInit()
{
    mysql_debug(1);
    if(!mysql_connect(SQL_Host, SQL_User, SQL_DB, SQL_Pass))
    {
        print("SQL connection attempt 1 FAILED!");
        if(!mysql_connect(SQL_Host, SQL_User, SQL_DB, SQL_Pass))
        {
            print("SQL connection attempt 2 FAILED!");
            if(!mysql_connect(SQL_Host, SQL_User, SQL_DB, SQL_Pass))
            {
                print("SQL connection attempt 3 FAILED!");
                return 1;
            }
        }
    }
    printf("Attempting to insert vehicles...\n");
    InjectVehicles();
    printf("\n%d Vehicles Inserted!\n", V_Counter);
    return 1;
}

public OnFilterScriptExit()
{
    mysql_close();
    return 1;
}

InjectVehicles()
{
    new arrCoords[7][46];
    new strFromFile2[256];
    new File: file = fopen("Vehicles.cfg", io_read);
    if(file)
    {
        new idv;
        while (idv < 730) // Highest vehicle id equals 729
        {
            fread(file, strFromFile2);
            split(strFromFile2, arrCoords, ',');
            new query[512], Float:V[4], M, C[2];

            M = strval(arrCoords[0])-400;
            if(M < 0 || M > 255) continue;
            V[0] = floatstr(arrCoords[1]);
            V[1] = floatstr(arrCoords[2]);
            V[2] = floatstr(arrCoords[3]);
            V[3] = floatstr(arrCoords[4]);
            C[0] = strval(arrCoords[5]);
            C[1] = strval(arrCoords[6]);
            //Saves all the info
            if(strval(arrCoords[5]) < 0 || strval(arrCoords[6]) < 0)
            {
                format(query, sizeof(query), "INSERT INTO vehicledata (ID, Model, vX, vY, vZ, A, ColorA, ColorB) VALUES ('%d', '%d', '%f' ,'%f', '%f', '%f', '%i', '%i') ON DUPLICATE KEY UPDATE ID='%d', Model='%d', vX='%f' ,vY='%f', vZ='%f', A='%f', ColorA='%i', ColorB='%i'", idv, M, V[0], V[1], V[2], V[3], random(128), random(128), idv, M, V[0], V[1], V[2], V[3], random(128), random(128));
            }else{
                format(query, sizeof(query), "INSERT INTO vehicledata (ID, Model, vX, vY, vZ, A, ColorA, ColorB) VALUES ('%d', '%d', '%f' ,'%f', '%f', '%f', '%i', '%i') ON DUPLICATE KEY UPDATE ID='%d', Model='%d', vX='%f' ,vY='%f', vZ='%f', A='%f', ColorA='%i', ColorB='%i'", idv, M, V[0], V[1], V[2], V[3], C[0], C[1], idv, M, V[0], V[1], V[2], V[3], C[0], C[1]);
            }
            printf(query);
            mysql_query(query); //queries
            mysql_free_result(); //Frees the result
            idv++;
            V_Counter++;
        }
    }
    fclose(file);
}

stock split(const strsrc[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;

    while(i <= strlen(strsrc)){
        if(strsrc[i]==delimiter || i==strlen(strsrc)){
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
    return 1;
}
This will insert/update 1 vehicle, then crash the server, so it's not exactly useful. How to do it correctly?


Re: Transfering vehicles to MySQL - Vince - 17.04.2011

You don't need mysql_free_result when doing an update or insert statement. I don't know if that's the cause of the problem, though. Check the mysql log.


Re: Transfering vehicles to MySQL [Solved] - Patrik356b - 17.04.2011

Well, i forgot to post that the log doesn't show any errors, but it works after removing mysql_free_result, thx