SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 12/02/2018, 04:25 PM   #1
ChristolisTV
Big Clucker
 
ChristolisTV's Avatar
 
Join Date: Feb 2015
Location: Florida, USA
Posts: 160
Reputation: 12
Exclamation [SQLite] INSERT INTO doesn't work properly.

Hello there SA-MP community, and thank you for clicking my thread! As the title says, INSERT INTO is not working properly. Let me give you more details. As you can see below, there is a stock that inserts the player's record, and a stock that loads player data from that record. (Registering the player more specifically and loading it's data).

This is the register_user() stock:
PHP Code:
forward register_userplayeridpasswd[], boolpassword );
stock register_userplayeridpasswd[], bool:password )
{
    print(
"SERVER: Okay Chris, 'register_user()' was called.\n");
    new
        
pName[MAX_PLAYER_NAME],
        
Ip[16],
        
Query1[512],
        
Query2[256/2],
        
DBResultResult1
    
;
    print(
"SERVER: Variables registered.\n");
    
GetPlayerNameplayeridpNamesizeofpName ) );
    
printf("SERVER: I tried to get the playerid's name. (It returned '%s')\n"pName);
    
GetPlayerIpplayeridIpsizeofIp ) );
    
printf("SERVER: Cool, now I got the IP of the playerid. (It returned '%s'. I am also going to get the date!)\n"Ip);
    
getdateYearMonthDay );
    print(
"SERVER: Now we are into the password boolean check-up, let me see what it will return!\n");
    if(
password)
    {
        print(
"SERVER: Looks like there is an actual password, I am going to format the query now. If I don't say 'Test', that means that I failed in formatting it!\n");
        
formatQuery1sizeofQuery1 ), "INSERT INTO `Users` (Username, Password, IP, Job, Skin, Money, Health, Armor, AdminLevel) VALUES ('%s', '%s', '%s', 'undefined', '0', '25000', '100.0000', '0.0000', '0');"pNameDB_Escapepasswd ), Ip);
        print(
"SERVER: Test! Looks like 'format()' ran good.\n");
    }
    else
    {
        print(
"SERVER: NO PASSWORD DETECTED, I am going to format the query now. If I don't say 'Test', that means that I failed in formatting it!\n");
        
formatQuery1sizeofQuery1 ), "INSERT INTO `Users` (Username, Password, IP, Job, Skin, Money, Health, Armor, AdminLevel) VALUES ('%s', '%s', '%s', 'undefined', '0', '25000', '100.0000', '0.0000', '0'');"pName"null"Ip);
        print(
"SERVER: Test! Looks like 'format()' ran good.\n");
    }
    print(
"SERVER: Okayyy... so I formatted the passwords, null or not. Now I am going to run the query!\n");
    
db_queryDatabaseQuery1 );
    print(
"SERVER: Ran the query, I am going to try to reset the 'Query1' variable to '0' now.\n");
    
format(Query1sizeof(Query1), "");
    print(
"SERVER: Good, now time to put the inputtext as password in the enum thingy.\n");
    
formatPlayerData[playerid][Password], 65passwd );

    print(
"SERVER: Okay, now I am going to tell 'load_playerdata()' to do it's job!\n");
    
load_playerdataplayerid );
    print(
"SERVER: It looks like 'load_playerdata()' did it's job!\n");

    print(
"SERVER: If this message is still shown up then WOW! We reached at the 570th line of the script, it's time to change XYZAngle values to a random airport.\n");
    new 
rand randomsizeof Spawnpoints );
    print(
"SERVER: I made the 'rand' variable.");
    
PlayerData[playerid][X] = Spawnpoints[rand][0];
    
PlayerData[playerid][Y] = Spawnpoints[rand][1];
    
PlayerData[playerid][Z] = Spawnpoints[rand][2];
    
PlayerData[playerid][Angle] = Spawnpoints[rand][3];
    print(
"SERVER: Looks good, now it's time to announce to the player that he got registered in the fricking database! xD\n");
    
SendClientMessageplayerid, -1"I registered you in the fricking database now!\n" );
    print(
"SERVER: Told him it.\n");

    print(
"SERVER: Running another Query(2) to get the amount of records in the database, so we can tell the players registered number!\n");
    
formatQuery2sizeof Query2"SELECT * FROM `Users`;" );
    
Result1 db_queryDatabaseQuery2 );

    print(
"SERVER: It's time to tell everyone in the server that someone joined!\n");
    new 
str[256];
    
formatstrsizeofstr ), "{A8C7FF}SERVER:{FFFFFF} Welcome {F9EC00}%s{FFFFFF} to the server! We now have %d players registered!"pNamedb_num_rowsResult1 ) );
    
db_free_resultResult1 );
    
SendClientMessageToAll( -1str );


    print(
"\nSERVER: My job is done here!\n");

This is the load_playerdata() stock:
PHP Code:
stock load_playerdataplayerid )
{
    new
        
Query[256],
        
DBResultResult,
        
pName[MAX_PLAYER_NAME]
    ;
    
GetPlayerNameplayeridpNamesizeof pName );

    
formatQuerysizeof Query"SELECT * FROM `Users` WHERE `Username` = '%s';"pName );
    
Result db_queryDatabaseQuery );

    
PlayerData[playerid][Skin] =  db_get_field_assoc_intResult"Skin" );
    
PlayerData[playerid][X] = db_get_field_assoc_floatResult"X" );
    
PlayerData[playerid][Y] = db_get_field_assoc_floatResult"Y" );
    
PlayerData[playerid][Z] = db_get_field_assoc_floatResult"Z" );
    
PlayerData[playerid][Angle] = db_get_field_assoc_floatResult"Angle" );
    
PlayerData[playerid][Interior] = db_get_field_assoc_intResult"Interior" );
    
PlayerData[playerid][Money] = db_get_field_assoc_intResult"Money" );
    
PlayerData[playerid][Health] = db_get_field_assoc_floatResult"Health" ); SetPlayerHealthplayeridPlayerData[playerid][Health] );
    
PlayerData[playerid][Armor] = db_get_field_assoc_floatResult"Armor" ); SetPlayerArmourplayeridPlayerData[playerid][Armor] );
    
PlayerData[playerid][AdminLevel] = db_get_field_assoc_intResult"AdminLevel" );
    
db_get_field_assocResult"Username"PlayerData[playerid][Username], 26 );
    
db_get_field_assocResult"IP"PlayerData[playerid][IP], 16+);
    
formatPlayerData[playerid][Job], 32"undefined" );

    
db_free_resultResult );

Excuse me if the code is kinda confusing or noobish, I just want to register/load/save data with stocks, and I believe it is possible. (Or is it?). Anyways, so why INSERT INTO is not working?
I have tried:
  • Executing the query from my SQL Browser (DB Browser for SQLite), it DID work there!
  • Removing the 'if( password )', didn't work.
Thank you very much for taking your time to read this!
__________________
YouTube | GitHub
Player since 04.09.2010
"Stop looking down, and look up to success."
- Christolis
ChristolisTV is offline   Reply With Quote
Old 12/02/2018, 04:32 PM   #2
Mugala
Gangsta
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: behind u
Posts: 530
Reputation: 23
Default Re: [SQLite] INSERT INTO doesn't work properly.

does your logs says "SERVER: My job is done here!" ? if yes, than check your SQL Structure
__________________
here to help
Mugala is offline   Reply With Quote
Old 12/02/2018, 04:37 PM   #3
ChristolisTV
Big Clucker
 
ChristolisTV's Avatar
 
Join Date: Feb 2015
Location: Florida, USA
Posts: 160
Reputation: 12
Default Re: [SQLite] INSERT INTO doesn't work properly.

Quote:
Originally Posted by Mugala View Post
does your logs says "SERVER: My job is done here!" ? if yes, than check your SQL Structure
Yes, my logs do say this message. And I think my SQL structure is fine. Here is the query on how I create my table:
PHP Code:
db_queryDatabase"CREATE TABLE IF NOT EXISTS `Users` (Username, Password VARCHAR(64), IP, Job, Skin, X, Y, Z, Angle, Interior, Money, Health, Armor, AdminLevel, RegisterDate DEFAULT GETDATE());" ); 
__________________
YouTube | GitHub
Player since 04.09.2010
"Stop looking down, and look up to success."
- Christolis
ChristolisTV is offline   Reply With Quote
Old 12/02/2018, 06:09 PM   #4
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,646
Reputation: 202
Default Re: [SQLite] INSERT INTO doesn't work properly.

Firstly, remove "stock" keyword every where you use it. Don't use that on purpose.
Now, can you print the formatted query and run it into your 'SQL Broswer'. Come back with those results!
I don't understand why you load player's data who is recently registred. The player shouldn't have anything in the database except basic values like Name, SQLID, Password and e-mail or something like that.
__________________
actor_plus - v4.1.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 12/02/2018, 09:39 PM   #5
ChristolisTV
Big Clucker
 
ChristolisTV's Avatar
 
Join Date: Feb 2015
Location: Florida, USA
Posts: 160
Reputation: 12
Default Re: [SQLite] INSERT INTO doesn't work properly.

__________________
YouTube | GitHub
Player since 04.09.2010
"Stop looking down, and look up to success."
- Christolis
ChristolisTV is offline   Reply With Quote
Old 12/02/2018, 09:58 PM   #6
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,646
Reputation: 202
Default Re: [SQLite] INSERT INTO doesn't work properly.

Quote:
Originally Posted by ChristolisTV View Post
Yes, my logs do say this message. And I think my SQL structure is fine. Here is the query on how I create my table:
PHP Code:
db_queryDatabase"CREATE TABLE IF NOT EXISTS `Users` (Username, Password VARCHAR(64), IP, Job, Skin, X, Y, Z, Angle, Interior, Money, Health, Armor, AdminLevel, RegisterDate DEFAULT GETDATE());" ); 
Is this working ? It is completly false.
PHP Code:
CREATE TABLE IF NOT EXISTS `Users` ( `IDINT NOT NULL , `UsernameVARCHAR(24NOT NULL , `IPVARCHAR(16NOT NULL , `JobINT NOT NULL , `SkinINT NOT NULL , `XFLOAT NOT NULL , `YFLOAT NOT NULL , `ZFLOAT NOT NULL , `AngleFLOAT NOT NULL , `InteriorINT NOT NULL , `MoneyINT NOT NULL , `HealthFLOAT NOT NULL , `ArmorFLOAT NOT NULL , `RegisterDateDATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP); 
If I'm wrong, I apologize then but I'm probably sure your query nonsense (and yes, you need an ID which will auto-increment to provide an unique ID for players).

Also, you forget to show what the query looks like after formatting like I asked for (print the string/query and show us here).
__________________
actor_plus - v4.1.2
I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath
Dayrion is offline   Reply With Quote
Old 12/02/2018, 10:08 PM   #7
ChristolisTV
Big Clucker
 
ChristolisTV's Avatar
 
Join Date: Feb 2015
Location: Florida, USA
Posts: 160
Reputation: 12
Default Re: [SQLite] INSERT INTO doesn't work properly.

Hmm, I thought a datatype wasn't required since it saves as TEXT as default. I am going to try it out, thank you for your help! (I gave you a rep! )
__________________
YouTube | GitHub
Player since 04.09.2010
"Stop looking down, and look up to success."
- Christolis
ChristolisTV is offline   Reply With Quote
Old 12/02/2018, 10:23 PM   #8
Mugala
Gangsta
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: behind u
Posts: 530
Reputation: 23
Default Re: [SQLite] INSERT INTO doesn't work properly.

yeah exactly, problem was in structure
__________________
here to help
Mugala is offline   Reply With Quote
Old 12/02/2018, 10:33 PM   #9
ChristolisTV
Big Clucker
 
ChristolisTV's Avatar
 
Join Date: Feb 2015
Location: Florida, USA
Posts: 160
Reputation: 12
Default Re: [SQLite] INSERT INTO doesn't work properly.

Quote:
Originally Posted by Mugala View Post
yeah exactly, problem was in structure
I guess it was, but hey.. rep to you too!
__________________
YouTube | GitHub
Player since 04.09.2010
"Stop looking down, and look up to success."
- Christolis
ChristolisTV is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UCP doesn't work properly Sc0pion Scripting Help 12 28/04/2017 06:16 PM
KEY_YES doesn't work properly? [+Rep] Amit1998 Scripting Help 2 07/03/2016 01:16 AM
Attach3DTextLabelToVehicle doesn't work properly. Drago987 Scripting Help 2 09/10/2014 11:48 PM
[SQLite Help] Insert Into doesn't work? Tika Spic Scripting Help 2 27/08/2014 12:29 PM
[Help]My SA-MP doesn't work properly. Chloride Client Support 5 31/07/2014 12:06 PM


All times are GMT. The time now is 11:29 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.