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

[SQL] Invalid Database Handle


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Pyro_
Senior Member
Join Date: Oct 2013
Old 10-20-2013 , 16:28   [SQL] Invalid Database Handle
Reply With Quote #1

I'm new to SourceMod/SourcePawn, and especially using SQL with it.

I'm trying to make a plugin that uses SQL neatly (and the best way (if this isn't the best way; what is the best way?)).

PHP Code:
public Handle:GetConnection()
{
    new 
Handle:db SQL_Connect(NAME_OF_DATABASE_CONFIGtrueerrorStringsizeof(errorString));

    if(
db == INVALID_HANDLE)
    {
        
LogAction(-1, -1"[TG Loyalty] Could not connect: %s"errorString);
    }
    return 
db;

The above is a function that returns a database handle, which is used in a lot of functions.
Example:

PHP Code:
public bool:QuickQuery(Handle:dbString:query[])
{
    if(
db == INVALID_HANDLE)
    {
        
db GetConnection();
    }
    
//This isn't the same, the first one checks if there already is a db connection, while this one checks if the possible new one didn't work
    
if(db == INVALID_HANDLE)
    {
        
CloseHandle(db);
        return 
false;
    }
    if(!
SQL_FastQuery(dbquery))
    {
        
SQL_GetError(dberrorStringsizeof(errorString));
        
LogAction(-1, -1"Failed to query (error: %s)"errorString);
        
CloseHandle(db);
        return 
false;
    }
    else
    {
        
CloseHandle(db);
        return 
true;
    }

I'm sorry if I'm doing extremely noobish things.

Whenever I call QuickQuery, or a function that uses QuickQuery
e.g.
PHP Code:
if(!QuickQuery(dbqueryString)) 
It gives me this error in the console: Native "SQL_FastQuery" reported: Invalid database Handle b(random numbers) (error: 3)

What am i doing wrong? And if possible, how can I do it better?
Pyro_ is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-20-2013 , 17:34   Re: [SQL] Invalid Database Handle
Reply With Quote #2

When you CloseHandle a variable, you should set it to INVALID_HANDLE on the next line.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-20-2013 at 17:34.
Powerlord is offline
Pyro_
Senior Member
Join Date: Oct 2013
Old 10-20-2013 , 18:01   Re: [SQL] Invalid Database Handle
Reply With Quote #3

Quote:
Originally Posted by Powerlord View Post
When you CloseHandle a variable, you should set it to INVALID_HANDLE on the next line.
Thanks for the response! However it didn't work, the error hasn't changed
Pyro_ is offline
Dr. Greg House
Professional Troll,
Part-Time Asshole
Join Date: Jun 2010
Old 10-21-2013 , 04:48   Re: [SQL] Invalid Database Handle
Reply With Quote #4

Which game?
I didn't look closely enough but it could be an issue with hibernation.
__________________
Santa or Satan?

Watch out when you're paying people for private requests! Most stuff already exists and you can hardly assess the quality of what you'll get, and if it's worth the money.
Dr. Greg House is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 10-21-2013 , 07:16   Re: [SQL] Invalid Database Handle
Reply With Quote #5

why don't you use a global db and do the connection once? also, printtoserver the errorstring of the connection to see easily what's going on.
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
Pyro_
Senior Member
Join Date: Oct 2013
Old 10-21-2013 , 17:10   Re: [SQL] Invalid Database Handle
Reply With Quote #6

Quote:
Originally Posted by Bimbo1 View Post
why don't you use a global db and do the connection once? also, printtoserver the errorstring of the connection to see easily what's going on.
Thanks!
Pyro_ 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 05:28.


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