Saving into sqlite.
#1

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;
}
Reply
#2

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

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;
}
Reply
#4

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.
Reply
#5

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
Reply
#6

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()
Reply
#7

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


Forum Jump:


Users browsing this thread: 1 Guest(s)