Raised This Month: $404 Target: $400
 101% 

SQLite Helper


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-09-2018 , 19:29   SQLite Helper
Reply With Quote #1

I haven't tested this hard enough so I'll be glad to know if there are bugs.

I hated SQLite when I started so I created this snippet to make SQLite working as easy as client preferences.

This plugin uses sourcemod-local SQLite database for its function.

This plugin obviously doesn't match the potential of SQL querying as a whole because this allows only saving strings into the database and not multiple variables, but it's still quite useful.

If you believe a native is missing and can be added, please tell me.

List of forward and natives:

Code:
public SQLiteHelper_OnDatabaseConnected()
{
	// Called when the database is connected.
	
	// Note: All of the plugin's natives are useless before this is called.
}


native bool:SQLiteHelper_isDatabaseConnected(const String:Name[100], String:Error[50]);

// return: true if the database is connected ( forward SQLiteHelper_OnDatabaseConnected was called ), false otherwise.

// Note: if this returns false ( rare occasions ) then no other native will work.

native SQLiteHelper_RegisterValue(const String:Name[100], String:Error[50]);

// Registers a value, must be done in the SQLiteHelper_OnDatabaseConnected forward ONLY!

// String:Name - Name of value, must be extra-unique to avoid collisions. If your plugin is named "CSGO Stats" and you want to save kills, use the name as "CSGO-Stats_Kills" or something similar.

// String:Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// Note: Collision between two names may occur even if another plugin is not using this stock, so don't assume if you're the only plugin to use this stock collisions won't occur.

// Note: this function must be called only once in the lifetime of your plugin but it's always a good idea to keep this function because better safe than sorry.

native SQLiteHelper_SetClientValue(client, const String:Name[100], String:Error[50], const String:value[], timestamp=-1);

// Sets a client's value in the database based on value's name.

// client - Client to set the value for.

// String:Name - Name of value to set for the client.

// String:Error - Buffer for an error. See the sqliteHelper plugin for list of possible errors.

// String:value - Value to set for the client.

// timestamp - Optional timestamp in the value's "last updated". This timestamp is unix and is retrieved through GetClientValue.

native SQLiteHelper_AddClientValue(client, const String:Name[100], String:Error[50], const String:value[], timestamp=-1);

// Note: The exact same as SetClientValue in terms of arguments so peek there for info here, EXCEPT it won't set the value but instead add the existing value with the value you put in String:value[].

// Note: You can set String:value[] to be a negative int / float for subtracting.

// Note: If you add a float to an int, the database will store the entire value as a float.

native SQLiteHelper_GetClientValue(client, const String:Name[100], String:Error[50], String:buffer[], length, &timestamp=0);

// Gets the value of a client.

// client - Client to get value from.

// Name - Name of value to get from.

// Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// buffer - Buffer to store the value into.

// length - Length of buffer.

// timestamp - Copyback for the timestamp when last updated.

// Note: Buffer will be set to be empty if client was not found. This means that if you create the variable buffer to be non-empty and it becomes empty you can tell for sure that the client doesn't exist.

native SQLiteHelper_SetAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:value[], timestamp=-1);

native SQLiteHelper_AddAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:value[], timestamp=-1);

native SQLiteHelper_GetAuthIdValue(const String:AuthId[100], const String:Name[100], String:Error[50], String:buffer[], length, &timestamp=0);

// All these three natives about AuthId all act like their Client equivalents, except you provide the AuthId of an offline ( or online ) player.

// Note: For reference, if you were to use it on an online client, GetClientAuthId must use AuthId_Engine. This means the AuthId inside the database is the engine's and not any other AuthId types.

native SQLiteHelper_SortValue(const String:Name[100], bool:Ascending = false, String:Error[50], Handle:AuthIdArray, Handle:ValueArray, Handle:TimestampArray);

// Sorts values based on ascending or descending order.

// String:Name - Name of value to sort.

// bool:Ascending - true if to sort from lowest value to highest value. false to sort from highest value to lowest value. If false, the highest value will be 0 in the arrays.

// String:Error - Buffer in case of error. Go to sqliteHelper.sp to see the possible errors.

// Handle:AuthIdArray - An array which YOU created to contain the Auth IDs that were sorted. CreateArray(100);

// Handle:ValueArray - An array which YOU created to contain the values that were sorted. CreateArray(100);

// Handle:TimestampArray - An array which YOU created to contain the timestamps of the values that were sorted. CreateArray(1).

// Note: Always create the arrays or you will get errors from this snippet.

// Note: Arrays are filled equally so in a single index within all arays, the data will match a single client.

// Note: If necessary you can use this native to irate through the entire database.
Attached Files
File Type: sp Get Plugin or Get Source (SQLiteHelper.sp - 100 views - 14.6 KB)
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
Crasher_3637
AlliedModders Donor
Join Date: May 2012
Old 08-10-2018 , 10:54   Re: SQLite Helper
Reply With Quote #2

Your commenting style amuses and confuses me at the same time.
__________________
AFK like JFK
Crasher_3637 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-10-2018 , 11:06   Re: SQLite Helper
Reply With Quote #3

Quote:
Originally Posted by Crasher_3637 View Post
Your commenting style amuses and confuses me at the same time.
What is unclear?
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
Walgrim
Member
Join Date: Dec 2015
Location: France
Old 08-10-2018 , 18:19   Re: SQLite Helper
Reply With Quote #4

He's talking of all the double slashes you are using instead of
PHP Code:
/* */ 
__________________
Walgrim is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-10-2018 , 18:34   Re: SQLite Helper
Reply With Quote #5

Quote:
Originally Posted by Walgrim View Post
He's talking of all the double slashes you are using instead of
PHP Code:
/* */ 
I'll get to it later.
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
Ilusion9
Senior Member
Join Date: Jun 2018
Old 08-11-2018 , 06:01   Re: SQLite Helper
Reply With Quote #6

Use the new syntax and the database class which is threaded.

PHP Code:

public void OnPluginStart()
{
    
Database.Connect(SQL_ConnectToDatabase"storage-local");
}

public 
void SQL_ConnectToDatabase(Database db, const char[] errorany data)
{
    
db.Query(SQL_OnFastQuery"CREATE TABLE IF NOT EXISTS ...");
}

public 
void SQL_OnFastQuery(Database dbDBResultSet rs, const char[] errorany data)
{
    if(
rs == null)
    {
        
LogError("(SQL_OnFastQuery) %s"error);
    }

Ilusion9 is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 08-11-2018 , 09:13   Re: SQLite Helper
Reply With Quote #7

Quote:
Originally Posted by Ilusion9 View Post
Use the new syntax and the database class which is threaded.

PHP Code:

public void OnPluginStart()
{
    
Database.Connect(SQL_ConnectToDatabase"storage-local");
}

public 
void SQL_ConnectToDatabase(Database db, const char[] errorany data)
{
    
db.Query(SQL_OnFastQuery"CREATE TABLE IF NOT EXISTS ...");
}

public 
void SQL_OnFastQuery(Database dbDBResultSet rs, const char[] errorany data)
{
    if(
rs == null)
    {
        
LogError("(SQL_OnFastQuery) %s"error);
    }

Why threaded? This is sqlite.
__________________


Accepting private work. Contact me if you're interested.
eyal282 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 08-11-2018 , 11:33   Re: SQLite Helper
Reply With Quote #8

Quote:
Originally Posted by eyal282 View Post
Why threaded? This is sqlite.
Disk reads and writes are not instantaneous.

It's just good practice.
__________________
ddhoward is offline
Ilusion9
Senior Member
Join Date: Jun 2018
Old 08-12-2018 , 10:02   Re: SQLite Helper
Reply With Quote #9

Quote:
Originally Posted by eyal282 View Post
Why threaded? This is sqlite.
maybe someone will use sqlite on a remote server
Ilusion9 is offline
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-12-2018 , 10:56   Re: SQLite Helper
Reply With Quote #10

Quote:
Originally Posted by Ilusion9 View Post
maybe someone will use sqlite on a remote server
SQLite doesn't have any networking ability built into it, it's meant to be used only as a local database.
__________________

Last edited by KliPPy; 08-12-2018 at 10:56.
KliPPy is offline
Reply


Thread Tools
Display Modes

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 14:31.


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