SA-MP Forums Archive
Sqlite help - 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: Sqlite help (/showthread.php?tid=408704)



Sqlite help - new121 - 19.01.2013

I am making a clan system using sqlite. Everything is saving correctly but when it loads it only loads data from the first row.Anyone see why that is?

pawn Код:
new DBResult:qResult, query[256];
    //format(query,sizeof(query),"SELECT * FROM `Clans` WHERE `ID` = '%d'",d);
    qResult = db_query(gangs,"SELECT * FROM `Clans`");
    if(db_num_rows(qResult))
    {
        new Field[128];
        db_get_field_assoc(qResult, "ID", Field, 12);
        new i = strval(Field);

        db_get_field_assoc(qResult, "Name", Field, 32);
        strmid(gInfo[i][gName],Field,0,32);

        db_get_field_assoc(qResult, "Members", Field, 12);
        gInfo[i][gMembers] = strval(Field);

        db_get_field_assoc(qResult, "Color", Field, 32);
        strmid(gInfo[i][gColor],Field,0,32);

        db_get_field_assoc(qResult, "Leader", Field, MAX_PLAYER_NAME);
        printf("%s",Field);
    }
    db_free_result(qResult);



Re: Sqlite help - iggy1 - 19.01.2013

You need to loop through the rows.

pawn Код:
while( db_num_rows(qResult) )
{
    new Field[128];
    db_get_field_assoc(qResult, "ID", Field, 12);
    new i = strval(Field);

    db_get_field_assoc(qResult, "Name", Field, 32);
    strmid(gInfo[i][gName],Field,0,32);

    db_get_field_assoc(qResult, "Members", Field, 12);
    gInfo[i][gMembers] = strval(Field);

    db_get_field_assoc(qResult, "Color", Field, 32);
    strmid(gInfo[i][gColor],Field,0,32);

    db_get_field_assoc(qResult, "Leader", Field, MAX_PLAYER_NAME);
    printf("%s",Field);
   
    db_next_row(qResult);//move on to next row
}
db_free_result(qResult);
That will probably need editing to work with your code.

EDIT: Also probably not the best idea to load values from a database, then use them as an index to an array without bounds checking. That could lead to serious problems. Unless your absolutely sure it wont go out of bounds.


Re: Sqlite help - new121 - 19.01.2013

Quote:
Originally Posted by iggy1
Посмотреть сообщение
Also probably not the best idea to load values from a database, then use them as an index to an array without bounds checking. That could lead to serious problems. Unless your absolutely sure it wont go out of bounds.
Oh wow, thank you for that I didn't even think about that.

Okay I tested that and it seems to work, it reads all the rows in the DB, however it spams the bottom row in the console, and doesn't stop.


Re: Sqlite help - iggy1 - 19.01.2013

That might be because it's a while loop, i never tested that sorry try this.

pawn Код:
new num_rows = db_num_rows(qResult);

for( new i=0; i < num_rows; ++i )
{
    new Field[128];
    db_get_field_assoc(qResult, "ID", Field, 12);
    new i = strval(Field);

    db_get_field_assoc(qResult, "Name", Field, 32);
    strmid(gInfo[i][gName],Field,0,32);

    db_get_field_assoc(qResult, "Members", Field, 12);
    gInfo[i][gMembers] = strval(Field);

    db_get_field_assoc(qResult, "Color", Field, 32);
    strmid(gInfo[i][gColor],Field,0,32);

    db_get_field_assoc(qResult, "Leader", Field, MAX_PLAYER_NAME);
    printf("%s",Field);
   
    db_next_row(qResult);//move on to next row
}
db_free_result(qResult);



Re: Sqlite help - new121 - 19.01.2013

That works!, thank you for your help.