[SQLite] INSERT INTO doesn't work properly.
#1

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 код:
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 код:
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!
Reply
#2

does your logs says "SERVER: My job is done here!" ? if yes, than check your SQL Structure
Reply
#3

Quote:
Originally Posted by Mugala
Посмотреть сообщение
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 код:
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());" ); 
Reply
#4

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

Reply
#6

Quote:
Originally Posted by ChristolisTV
Посмотреть сообщение
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 код:
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 код:
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).
Reply
#7

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! )
Reply
#8

yeah exactly, problem was in structure
Reply
#9

Quote:
Originally Posted by Mugala
Посмотреть сообщение
yeah exactly, problem was in structure
I guess it was, but hey.. rep to you too!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)