Page 1 of 1

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

Posted: Sat Sep 26, 2026 5:23 pm
by Archive-Poster
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:

Code: Select all

SimpleSQLMySQLConnect( HostName[ ], UserName[ ], Password[ ], DataBase[ ] ) // Use this if you have a remote database
SimpleSQLSQLiteConnect( DataBase[ ] ) // 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
SimpleSQLQuery( Query[ ], Index, Data[ ] ) // Does the query. Set Index as zero for queries that are not including SELECT word
The results may be:

Code: Select all

0 -> SimpleSQLBad
1 -> SimpleSQLGood
An example plugin:

Code: Select all

// Project headers
#include < amxmodx >
#include < SimpleSQL >

// Do not define null values
#define QUERY_MAP_START    8192

// Global variables
new gData[ SimpleSQLBufferSize ];
new gQuery[ SimpleSQLBufferSize ];

// 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 )", 0, gData );

    // Insert current map name and current unix epoch time stamp into the table we created
    new Map[ 32 ];
    get_mapname( Map, sizeof Map - 1 );
    formatex( gQuery, sizeof gQuery - 1, "INSERT INTO Maps VALUES ( '%s', %d )", Map, get_systime( ) );
    SimpleSQLQuery( gQuery, 0, gData );

    // 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
    formatex( gData, sizeof gData - 1, "The maps we played since database exists." );
    SimpleSQLQuery( "SELECT Name FROM Maps", QUERY_MAP_START, gData );
}

// 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 SimpleSQLFunction( Query, Index, Data[ ] )
{
    // Get the query first
    static Handle: pQuery;
    pQuery = Handle: Query;

    // 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 Name[ 32 ];

            while( SQL_MoreResults( pQuery ) )
            {
                SQL_ReadResult( pQuery, 0, Name, sizeof Name - 1 );

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

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

// Our query errors handler
// This should be defined in all of your plugins that use SimpleSQL
public SimpleSQLErrorFunction( Error[ ] )
{
    log_amx( Error );
} 
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.