SA-MP Forums Archive
Saving into sqlite. - 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)
+--- Thread: Saving into sqlite. (/showthread.php?tid=466414)



Saving into sqlite. - Unri - 27.09.2013

Hello, i just want to save one value into db with SQLite, but i dont know how to write the save command. Its reading them fine, if that makes a difference. :S

This is what i tried.
Код:
stock SaveFactoryDB()
{
	new query[512];
	format(query, sizeof(query), "UPDATE `Factory` SET `fMineral` = '%s', WHERE `ffid` = '1'", FactoryInfo[1][fMineral]);
	db_query(FactoryDB, query);
	return 0;
}



Re: Saving into sqlite. - Dragonsaurus - 27.09.2013

If you are using zcmd:
pawn Код:
CMD:savefactory(playerid, params[])
{
     SaveFactoryDB();
     return 1;
}



Re: Saving into sqlite. - Konstantinos - 27.09.2013

That query won't be executed correctly because of the error in the syntax. It also can be improved a little bit and the most important - use DB_Escape to prevent SQL Injection.
pawn Код:
new
    query[ 48 + /* fMineral's max size */ ]
;
format( query, sizeof( query ), "UPDATE Factory SET fMineral = '%s' WHERE ffid = 1", DB_Escape( FactoryInfo[ 1 ][ fMineral ] ) );
pawn Код:
stock DB_Escape(text[])
{
    new
        ret[80 * 2],
        ch,
        i,
        j;
    while ((ch = text[i++]) && j < sizeof (ret))
    {
        if (ch == '\'')
        {
            if (j < sizeof (ret) - 2)
            {
                ret[j++] = '\'';
                ret[j++] = '\'';
            }
        }
        else if (j < sizeof (ret))
        {
            ret[j++] = ch;
        }
        else
        {
            j++;
        }
    }
    ret[sizeof (ret) - 1] = '\0';
    return ret;
}



Re: Saving into sqlite. - Pottus - 27.09.2013

Ya you fucked up your query here....

Should be:

pawn Код:
format(query, sizeof(query), "UPDATE `Factory` SET `fMineral` = '%s' WHERE `ffid` = '1'", FactoryInfo[1][fMineral]);
And of course as Konstantinos said use DB_escape() you may not always need to use it only when there is actual input but it's typically good practice to use it anytime your dealing with strings.


Re: Saving into sqlite. - Unri - 27.09.2013

Its working now, but when it gets saved back into db, it gets saved as some question mark or a letter sometimes. No idea where the problem is now. It gets read as a number, used as a number and saved as some symbol? wtf


Re: Saving into sqlite. - Pottus - 27.09.2013

lol looks like your trying to save integer as a string use this...

pawn Код:
format(query, sizeof(query), "UPDATE `Factory` SET `fMineral` = '%i' WHERE `ffid` = '1'", FactoryInfo[1][fMineral]);
You won't need DB_Escape()


Re: Saving into sqlite. - Unri - 27.09.2013

I love you. Finally, after 3 days of staring at the screen, i can proceed at that annoying gm im making..