attachments saving
#1

Hello everyone i tried to edit the default attachments fs that comes with the server package to make it save the attachments using sqlite i have done this so far:

PHP Code:
//-------------------------------------------------
//
// This is an example of using the EditAttachedObject functions
// to allow the player to customize their character.
//
// h02 2012
//
// SA-MP 0.3e and above
//
//-------------------------------------------------
#include <a_samp>
#define DIALOG_ATTACH_INDEX             13500
#define DIALOG_ATTACH_INDEX_SELECTION   DIALOG_ATTACH_INDEX+1
#define DIALOG_ATTACH_EDITREPLACE       DIALOG_ATTACH_INDEX+2
#define DIALOG_ATTACH_MODEL_SELECTION   DIALOG_ATTACH_INDEX+3
#define DIALOG_ATTACH_BONE_SELECTION    DIALOG_ATTACH_INDEX+4
new DBAttachDB;
new 
pID;
enum AttachmentEnum
{
    
attachmodel,
    
attachname[24]
}
public 
OnFilterScriptInit()
{
    if ((
AttachDB db_open("attachments.db")) == DB0)
    {
        print(
"Failed to open a connection to \"attachments.db\"");
    }
    else
    {
        
db_query(AttachDB"PRAGMA synchronous = OFF");
        
db_query(AttachDB"CREATE TABLE IF NOT EXISTS attachments (ownerid INTEGER DEFAULT -1 NOT NULL, index INTEGER DEFAULT 0 NOT NULL, modelid INTEGER DEFAULT 0 NOT NULL, boneid INTEGER DEFAULT 0 NOT NULL, \
        fOffsetX FLOAT, \
        fOffsetY FLOAT, \
        fOffsetZ FLOAT, \
        fRotX FLOAT, \
        fRotY FLOAT, \
        fRotZ FLOAT, \
        fScaleX FLOAT, \
        fScaleY FLOAT, \
        fScaleZ FLOAT)"
);
    }
    return 
1;
}
public 
OnFilterScriptExit()
{
    return 
1;
}
public 
OnPlayerConnect(playerid)
{
    
pID 72;
    return 
1;
}
public 
OnPlayerSpawn(playerid)
{
    new 
buf[200], DBResultResult;
    
format(bufsizeof buf"SELECT * FROM attachments WHERE ownerid = %d"pID);
    
Result db_query(AttachDBbuf);
    if (
db_num_rows(Result))
    {
        
SetPlayerAttachedObject(playerid,db_get_field_assoc_int(Result"index"),db_get_field_assoc_int(Result"modelid"),db_get_field_assoc_int(Result"boneid"),db_get_field_assoc_float(Result"fOffsetX"), db_get_field_assoc_float(Result"fOffsetY"), db_get_field_assoc_float(Result"fOffsetZ"), db_get_field_assoc_float(Result"fRotX"), db_get_field_assoc_float(Result"fRotY"), db_get_field_assoc_float(Result"fRotZ"), db_get_field_assoc_float(Result"fScaleX"), db_get_field_assoc_float(Result"fScaleY"), db_get_field_assoc_float(Result"fScaleZ"));
    }
    
db_free_result(Result);
    return 
1;
}
public 
OnPlayerCommandText(playeridcmdtext[])
{
    if(!
strcmp(cmdtext"/attachments"true))
    {
        new 
string[128];
        for(new 
x;x<MAX_PLAYER_ATTACHED_OBJECTS;x++)
        {
            if(
IsPlayerAttachedObjectSlotUsed(playeridx)) format(stringsizeof(string), "%s%d (Used)\n"stringx);
            else 
format(stringsizeof(string), "%s%d\n"stringx);
        }
        
ShowPlayerDialog(playeridDIALOG_ATTACH_INDEX_SELECTIONDIALOG_STYLE_LIST, \
        
"{FF0000}Attachment Modification - Index Selection"string"Select""Cancel");
        return 
1;
    }
    return 
0;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_ATTACH_INDEX_SELECTION:
        {
            if(
response)
            {
                if(
IsPlayerAttachedObjectSlotUsed(playeridlistitem))
                {
                    
ShowPlayerDialog(playeridDIALOG_ATTACH_EDITREPLACEDIALOG_STYLE_MSGBOX, \
                    
"{FF0000}Attachment Modification""Do you wish to edit the attachment in that slot, or delete it?""Edit""Delete");
                }
                else
                {
                    new 
string[4000+1];
                    for(new 
x;x<sizeof(AttachmentObjects);x++)
                    {
                        
format(stringsizeof(string), "%s%s\n"stringAttachmentObjects[x][attachname]);
                    }
                    
ShowPlayerDialog(playeridDIALOG_ATTACH_MODEL_SELECTIONDIALOG_STYLE_LIST, \
                    
"{FF0000}Attachment Modification - Model Selection"string"Select""Cancel");
                }
                
SetPVarInt(playerid"AttachmentIndexSel"listitem);
            }
            return 
1;
        }
        case 
DIALOG_ATTACH_EDITREPLACE:
        {
            if(
responseEditAttachedObject(playeridGetPVarInt(playerid"AttachmentIndexSel"));
            else
            {
                new 
Query[128];
                
RemovePlayerAttachedObject(playeridGetPVarInt(playerid"AttachmentIndexSel"));
                
format(Querysizeof Query"DELETE FROM attachments WHERE ownerid = %d, index = %d" pIDGetPVarInt(playerid"AttachmentIndexSel"));
                
db_query(AttachDBQuery);
            }
            
DeletePVar(playerid"AttachmentIndexSel");
            return 
1;
        }
        case 
DIALOG_ATTACH_MODEL_SELECTION:
        {
            if(
response)
            {
                if(
GetPVarInt(playerid"AttachmentUsed") == 1EditAttachedObject(playeridlistitem);
                else
                {
                    
SetPVarInt(playerid"AttachmentModelSel"AttachmentObjects[listitem][attachmodel]);
                    new 
string[256+1];
                    for(new 
x;x<sizeof(AttachmentBones);x++)
                    {
                        
format(stringsizeof(string), "%s%s\n"stringAttachmentBones[x]);
                    }
                    
ShowPlayerDialog(playeridDIALOG_ATTACH_BONE_SELECTIONDIALOG_STYLE_LIST, \
                    
"{FF0000}Attachment Modification - Bone Selection"string"Select""Cancel");
                }
            }
            else 
DeletePVar(playerid"AttachmentIndexSel");
            return 
1;
        }
        case 
DIALOG_ATTACH_BONE_SELECTION:
        {
            if(
response)
            {
                
SetPlayerAttachedObject(playeridGetPVarInt(playerid"AttachmentIndexSel"), GetPVarInt(playerid"AttachmentModelSel"), listitem+1);
                
EditAttachedObject(playeridGetPVarInt(playerid"AttachmentIndexSel"));
                
SendClientMessage(playerid0xFFFFFFFF"Hint: Use {FFFF00}~k~~PED_SPRINT~{FFFFFF} to look around.");
            }
            
DeletePVar(playerid"AttachmentIndexSel");
            
DeletePVar(playerid"AttachmentModelSel");
            return 
1;
        }
    }
    return 
0;
}
public 
OnPlayerEditAttachedObjectplayeridresponseindexmodelidboneid,
                                   
Float:fOffsetXFloat:fOffsetYFloat:fOffsetZ,
                                   
Float:fRotXFloat:fRotYFloat:fRotZ,
                                   
Float:fScaleXFloat:fScaleYFloat:fScaleZ )
{
    new 
Query[200], DBResultResult;
    new 
debug_string[256+1];
    
format(debug_string,256,"SetPlayerAttachedObject(playerid,%d,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f)",
           
index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);
    print(
debug_string);
    
SendClientMessage(playerid0xFFFFFFFFdebug_string);
    
    
SetPlayerAttachedObject(playerid,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);
    
SendClientMessage(playerid0xFFFFFFFF"You finished editing an attached object");
    
format(Querysizeof Query"SELECT index FROM attachments WHERE ownerid = %d, index = %d"pIDindex);
    
Result db_query(AttachDBQuery);
    if (
db_num_rows(Result))
    {
        
format(Querysizeof Query"UPDATE attachments SET fOffsetX = %f, fOffsetY = %f, fOffsetZ = %f, fRotX = %f, fRotY = %f, fRotZ = %f, fScaleX = %f, fScaleY = %f, fScaleX = %Z WHERE ownerid = %d, index = %d"pIDindex);
        
db_query(AttachDBQuery);
    }
    else
    {
        
format(Querysizeof Query"INSERT INTO attachments (ownerid,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ) VALUES (%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f)"pID,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);// Insert into users the name and the password. The userid gets increased automatically and the admin is by default 0 value. We don't have to escape password as the hashed output provided by Whirlpool contains only alphabet characters and numbers.
        
db_query(AttachDBQuery);
    }
    
db_free_result(Result);
    return 
1;

the pID is random just to check script

this is what i get when i spawn

Code:
[02:24:14] [db_log_queries]: SELECT * FROM attachments WHERE ownerid = 72
[02:24:14] [Warning] db_query: Query failed: no such table: attachments
[02:24:14] [Error] db_num_rows(): invalid result handle 0
[02:24:14] [Error] db_free_result(): invalid result handle 0
Reply
#2

alright everyone i have managed to fix table issue myself but now i cant get over this one so now it saves the dataeverything is fine but i get error in the console these are the errors:

Code:
[db_log_queries]: SELECT indexindex FROM attachments WHERE ownerid = 72, indexin
dex = 3
[Warning] db_query: Query failed: near ",": syntax error
[Error] db_num_rows(): invalid result handle 0
[db_log_queries]: INSERT INTO attachments (ownerid,indexindex,modelid,boneid,fOf
fsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ) VALUES (72,3,
19093,6,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,1.000000,1.000000,
1.000000)
[Error] db_free_result(): invalid result handle 0
here is the saving code under onplayereditattachedobject:
PHP Code:

    
new Query[400], DBResultResult;
    
format(Querysizeof Query"SELECT indexindex FROM attachments WHERE ownerid = %d, indexindex = %d"pIDindex);
    
Result db_query(AttachDBQuery);
    if (
db_num_rows(Result))
    {
        
format(Querysizeof Query"UPDATE attachments SET fOffsetX = %f, fOffsetY = %f, fOffsetZ = %f, fRotX = %f, fRotY = %f, fRotZ = %f, fScaleX = %f, fScaleY = %f, fScaleX = %Z WHERE ownerid = %d, indexindex = %d"pIDindex);
        
db_query(AttachDBQuery);
    }
    else
    {
        
format(Querysizeof Query"INSERT INTO attachments (ownerid,indexindex,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ) VALUES (%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f)"pID,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);// Insert into users the name and the password. The userid gets increased automatically and the admin is by default 0 value. We don't have to escape password as the hashed output provided by Whirlpool contains only alphabet characters and numbers.
        
db_query(AttachDBQuery);
    }
    
db_free_result(Result); 
and another problem is that even tho 2 objects saved but only 0 index loads let me show you my loading code:

PHP Code:
public OnPlayerSpawn(playerid)
{
    new 
buf[200], DBResultResult;
    
format(bufsizeof buf"SELECT * FROM attachments WHERE ownerid = %d"pID);
    
Result db_query(AttachDBbuf);
    if (
db_num_rows(Result))
    {
        
SetPlayerAttachedObject(playerid,db_get_field_assoc_int(Result"indexindex"),db_get_field_assoc_int(Result"modelid"),db_get_field_assoc_int(Result"boneid"),db_get_field_assoc_float(Result"fOffsetX"), db_get_field_assoc_float(Result"fOffsetY"), db_get_field_assoc_float(Result"fOffsetZ"), db_get_field_assoc_float(Result"fRotX"), db_get_field_assoc_float(Result"fRotY"), db_get_field_assoc_float(Result"fRotZ"), db_get_field_assoc_float(Result"fScaleX"), db_get_field_assoc_float(Result"fScaleY"), db_get_field_assoc_float(Result"fScaleZ"));
    }
    
db_free_result(Result);
    return 
1;

thanks in advance
Reply
#3

It is an expression in WHERE clause.
pawn Code:
ownerid = %d AND indexindex = %d
Move to the next row using db_next_row

EDIT:
Create a multi-column index.
pawn Code:
CREATE INDEX ownerid_indexindex ON attachments (ownerid, indexindex);
https://www.sqlite.org/queryplanner....column_indices

which will also work as a single-index for `ownerid` column.
Reply
#4

Quote:
Originally Posted by Calisthenics
View Post
It is an expression in WHERE clause.
[pawn]
EDIT:
Create a multi-column index.
pawn Code:
CREATE INDEX ownerid_indexindex ON attachments (ownerid, indexindex);
https://www.sqlite.org/queryplanner....column_indices

which will also work as a single-index for `ownerid` column.
thank you very much everything is fixed it works fine now but i would like to learn more about the Create a multi-column index. you talked about i checked the link i didnt understand can you please give me an example using my code above i will be grateful thanks
Reply
#5

Indexes help speeding up the queries when used correctly, especially to avoid full-table scans.

Even a simple top 10 kills
pawn Code:
SELECT ... ORDER BY kills DESC LIMIT 10;
should have `kills` indexed in descending order.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)