Raised This Month: $32 Target: $400
 8% 

[INC] Simple SQL Help Package (Last Updated in June 20, 2013)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 06-05-2013 , 13:01   [INC] Simple SQL Help Package (Last Updated in June 20, 2013)
Reply With Quote #1

I see that many persons know how to use MySQL because they know PHP but MySQL Pawn seems hard for them.
This package contains a simplified mode to use MySQL and SQLite.

If you use MySQL then you should only have in your modules INI configuration file string "mysql" (without quotes)
If you use SQLite then you should only have in your modules INI configuration file string "sqlite" (without quotes)

Never let your modules INI file contains both mysql and sqlite. Just one of them.

The functions are:

PHP Code:
SimpleSQLMySQLConnectHostName[ ], UserName[ ], Password[ ], DataBase[ ] ) // Use this if you have a remote database
SimpleSQLSQLiteConnectDataBase[ ] ) // Use this if you want to use a local database
SimpleSQLDisconnect( ) // Is not really required but is good if you use MySQL or SQLite only a few minutes per map
SimpleSQLQueryQuery[ ], IndexData[ ] ) // Does the query. Set Index as zero for queries that are not including SELECT word 
The results may be:

PHP Code:
-> SimpleSQLBad
-> SimpleSQLGood 
An example plugin:

PHP Code:
// Project headers
#include < amxmodx >
#include < SimpleSQL >

// Do not define null values
#define QUERY_MAP_START    8192

// Global variables
new gDataSimpleSQLBufferSize ];
new 
gQuerySimpleSQLBufferSize ];

// When map starts
public plugin_init( )
{
    
// Try connecting
    // Stop everything if connection fails
    // This is not a very common happening
    
if( !SimpleSQLSQLiteConnect"Database" ) )
        return;

    
// Try to create the table
    // The second parameter is null, we are not requiring things from this query
    // The third parameter should be used even if it is empty
    
SimpleSQLQuery"CREATE TABLE IF NOT EXISTS Maps ( Name TEXT, Time NUMERIC )"0gData );

    
// Insert current map name and current unix epoch time stamp into the table we created
    
new Map32 ];
    
get_mapnameMapsizeof Map );
    
formatexgQuerysizeof gQuery 1"INSERT INTO Maps VALUES ( '%s', %d )"Mapget_systime( ) );
    
SimpleSQLQuerygQuery0gData );

    
// Print the names of all maps we played since database exists
    // Let's try to assign a value to gData
    // The second parameter is QUERY_MAP_START so we'll know that this is the query into function SimpleSQLFunction
    
formatexgDatasizeof gData 1"The maps we played since database exists." );
    
SimpleSQLQuery"SELECT Name FROM Maps"QUERY_MAP_STARTgData );
}

// Our queries handler
// This should be defined in all of your plugins that use SimpleSQL
// In this function, you could use SimpleSQLQuery to update, delete, create or even select
public SimpleSQLFunctionQueryIndexData[ ] )
{
    
// Get the query first
    
static HandlepQuery;
    
pQuery HandleQuery;

    
// Switch between queries
    
switch( Index )
    {
        case 
QUERY_MAP_START:
        {
            
// This is the query with index QUERY_MAP_START
            // Data will be "The maps we played since database exists."
            // Retrieve the map names
            
static Name32 ];

            while( 
SQL_MoreResultspQuery ) )
            {
                
SQL_ReadResultpQuery0Namesizeof Name );

                
server_print"We played %s since database exists!"Name );

                
// For functions like SQL_NextRow you have to check SQLX INC file
                
SQL_NextRowpQuery );
            }
        }
    }
}

// Our query errors handler
// This should be defined in all of your plugins that use SimpleSQL
public SimpleSQLErrorFunctionError[ ] )
{
    
log_amxError );

If you don't want to handle your query into SimpleSQLFunction then just set the second parameter of SimpleSQLQuery as zero.
The third parameter (gData) is every time required.

Do not use the SMA file attached below. Just check it.
Place the INC header file into your include directory from scripting directory.
Attached Files
File Type: inc SimpleSQL.inc (4.0 KB, 310 views)
File Type: sma Get Plugin or Get Source (SimpleSQL.sma - 862 views - 2.3 KB)
__________________

Last edited by claudiuhks; 06-25-2013 at 02:01.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 06-05-2013 , 15:21   Re: Simple SQL Help Package
Reply With Quote #2

Thank's Hattrick, this is really useful , btw you can add a website to get free database
www.db4free.net

Last edited by TheDS1337; 06-05-2013 at 15:21.
TheDS1337 is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 06-05-2013 , 15:31   Re: Simple SQL Help Package
Reply With Quote #3

Quote:
Originally Posted by DeagLe.Studio View Post
Thank's Hattrick, this is really useful , btw you can add a website to get free database
www.db4free.net
I don't trust in free things.
I don't think they have any guarantee.
__________________

Last edited by claudiuhks; 06-05-2013 at 15:32.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
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 17:44.


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