Raised This Month: $51 Target: $400
 12% 

SQL_FetchInt not working, please help!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-16-2015 , 19:20   SQL_FetchInt not working, please help!
Reply With Quote #1

Hey guys!
I am trying to store clients kills in a sqlite database. It is storing steamId correctly, creating table and creating new rows for new users properly. But the I can't read the kills with SQL_FetchInt and I can't understand what the problem is..

Here I am creating the table on plugin start:
// Create sql table

PHP Code:
new String:queryCreateTable[300];
    
    
Format(queryCreateTablesizeof(queryCreateTable), "CREATE TABLE killCounter(steamId tinytext, kills int)");
    
    new 
Handle:queryHCreateTable SQL_Query(DBqueryCreateTable); 
And here I am creating the rows on client authorized:
PHP Code:
// Check if row created
    
new String:queryCheck[300];
    
decl String:SteamId[64];
    
GetClientAuthString(clientSteamIdsizeof(SteamId));
    
    
Format(queryChecksizeof(queryCheck), "SELECT steamId FROM killCounter WHERE steamId='%s'"SteamId);
    new 
Handle:queryHCheck SQL_Query(DBqueryCheck);
    
    if(
queryHCheck != INVALID_HANDLE) {
        if(
SQL_FetchRow(queryHCheck)) {
            
PrintToServer("Client exists in db, no need to create a new row!")
        } else {
            
// Create sql row for new user
            
PrintToServer("Creating row for new user!");
            new 
String:queryCreateRow[300];
            
Format(queryCreateRowsizeof(queryCreateRow), "INSERT INTO killCounter (steamId, kills) VALUES ('%s', 99)"SteamId);
            new 
Handle:queryHCreateRow SQL_Query(DBqueryCreateRow);
        }
    } 
I am setting the kills to 99 just to get the reading and writing to the DB done since I am new to this.

Here I am looping out the table on command:

PHP Code:
PrintResults(HandlequeryH) {
        new 
String:SteamId[64];
        new 
Integer:kills;
        
        while(
SQL_FetchRow(queryH)) {
            
SQL_FetchString(queryH0SteamIdsizeof(SteamId));
            
SQL_FetchInt(queryH0kills); // Warning 213: tag mismatch 
            
PrintToServer("SteamId: %s, Kills: %n"SteamIdkills);
            
PrintToChatAll("SteamId: %s, Kills: %n"SteamIdkills);
        }
    } 
It loops out my steamId correctly, since im doing it offline its just mine. But the kills are just giving random numbers(probably not really random). Has been 2 or 3 kills so far that was printed out.
Note the warning I am getting above in the while loop.
I would really appreciate help since I have been staring at this one hour plus googling sooo much.
Thanks in advance guys!

Last edited by joeyeyey; 07-16-2015 at 19:24. Reason: Change code to php :)
joeyeyey is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 07-16-2015 , 19:44   Re: SQL_FetchInt not working, please help!
Reply With Quote #2

the second argument is an index into the row of results, you'd want it to be 1 instead of 0. 0 grabs the steamid again as an int (which would give some garbage address)

also the SQL_FetchTypeByName functions in smlib tend to be easier to use and maintain, e.g. SQL_FetchIntByName(queryH, "kills")

edit: also you use it like kills = SQL_FetchInt(queryH, 1), you're actually getting the dbresult

Last edited by Miu; 07-16-2015 at 19:46.
Miu is offline
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-16-2015 , 19:58   Re: SQL_FetchInt not working, please help!
Reply With Quote #3

Quote:
Originally Posted by Miu View Post
the second argument is an index into the row of results, you'd want it to be 1 instead of 0. 0 grabs the steamid again as an int (which would give some garbage address)

also the SQL_FetchTypeByName functions in smlib tend to be easier to use and maintain, e.g. SQL_FetchIntByName(queryH, "kills")

edit: also you use it like kills = SQL_FetchInt(queryH, 1), you're actually getting the dbresult
Oh okay, I knew it would be some simple thing like that! Oh so you can actually use it like a method to save in a variable like in java/php? That's great! You're awesome, once again you save the day! Haha
Btw, I am failing to use smlib. I have extracted it to the sourcemod folder like they said and manually moved all the files and folders to its places. Still I get an error when I try to simply #include smlib in my plugins. Do you know if there are some common mistake I am doing there? It was a while ago I tried it, maybe I will understand what I am doing wrong now when Im not that new to everything.
Anyway, thanks for the help!
joeyeyey is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 07-16-2015 , 20:15   Re: SQL_FetchInt not working, please help!
Reply With Quote #4

Quote:
Originally Posted by joeyeyey View Post
Oh so you can actually use it like a method to save in a variable like in java/php?
there aren't methods in sourcepawn because it doesn't have classes, it's just a function returning an integer. you can't return arrays so you need to pass one by reference in order to return strings instead, but you can return single primitives fine.

also php is insane, don't compare it to anything else

Quote:
Originally Posted by joeyeyey View Post
Btw, I am failing to use smlib. I have extracted it to the sourcemod folder like they said and manually moved all the files and folders to its places. Still I get an error when I try to simply #include smlib in my plugins.
you put it in the wrong place, you need to put it in /include relative to the compiler location

Last edited by Miu; 07-16-2015 at 20:20.
Miu is offline
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-16-2015 , 20:30   Re: SQL_FetchInt not working, please help!
Reply With Quote #5

Quote:
Originally Posted by Miu View Post
there aren't methods in sourcepawn because it doesn't have classes, it's just a function returning an integer. you can't return arrays so you need to pass one by reference in order to return strings instead, but you can return single primitives fine.

also php is insane, don't compare it to anything else



you put it in the wrong place, you need to put it in /include relative to the compiler location
Okay, I will try it. Now, it shows the starting value of kills properly. But I can't seem to change it. I am trying to add 1 kill every round just to find out how it is working. But it doesn't change the value.

PHP Code:
public OnRoundStart(client) {
    
    new 
Integer:kills;
    new 
String:query[300];
    new 
String:SteamId[64];
    
GetClientAuthString(clientSteamIdsizeof(SteamId));
    
Format(querysizeof(query), "SELECT kills FROM killCounter WHERE steamId = %s"SteamId);
    new 
Handle:queryH SQL_Query(DBquery);
    
kills SQL_FetchInt(queryH1);
    
kills += 1;
    
Format(querysizeof(query), "UPDATE killCounter SET kills = '%i' WHERE steamId = '%s'"killsSteamId);
    new 
Handle:queryH2 SQL_Query(DBquery);
    

Can you see what I am doing wrong this time?
Cheers

Last edited by joeyeyey; 07-16-2015 at 20:41.
joeyeyey is offline
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-16-2015 , 20:42   Re: SQL_FetchInt not working, please help!
Reply With Quote #6

Edited the code since I forgot the handler the first time. It isnt updating and addnig 1 kills every round start that its supposed to. What am I doing wrong?
joeyeyey is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 07-16-2015 , 22:42   Re: SQL_FetchInt not working, please help!
Reply With Quote #7

Quote:
Originally Posted by joeyeyey View Post
PHP Code:
public OnRoundStart(client) {
    
    new 
Handle:queryH SQL_Query(DBquery);
    new 
Handle:queryH2 SQL_Query(DBquery);
    

Can you see what I am doing wrong this time?
Cheers
BAD BAD BAD BAD BAD!

Use THREADED queries, SQL_TQuery.
Potato Uno is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 07-17-2015 , 07:36   Re: SQL_FetchInt not working, please help!
Reply With Quote #8

the index needs to be 0 there since the result set is just the kills, not (steamid, kills)

this is why sql_fetchintbyname is easier :)

Last edited by Miu; 07-17-2015 at 07:37.
Miu is offline
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-17-2015 , 08:50   Re: SQL_FetchInt not working, please help!
Reply With Quote #9

Quote:
Originally Posted by Potato Uno View Post
BAD BAD BAD BAD BAD!

Use THREADED queries, SQL_TQuery.
Will look that up, but that shouldn't be what causing it to not update right?

Also, is it that SQL_TQuery destroys itself afterwards. So that I can just use the same variable names over and over? Or why is it that TQuery is better? Just want to learn this stuff

Last edited by joeyeyey; 07-17-2015 at 08:57.
joeyeyey is offline
joeyeyey
Junior Member
Join Date: Jun 2015
Location: Sweden
Old 07-17-2015 , 08:53   Re: SQL_FetchInt not working, please help!
Reply With Quote #10

Quote:
Originally Posted by Miu View Post
the index needs to be 0 there since the result set is just the kills, not (steamid, kills)

this is why sql_fetchintbyname is easier
Okay, yeah will definitely fix that smlib. But it still ain't updating with index set to "0" in the query.
joeyeyey is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 20:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode