[Include] PVar database - The easiest way for a user database - SQLITE, MYSQL
#1

Vars.inc

Version: 3 [BETA] (last updated on 22 March, 2016)

This include lets you save and load in memory variables at anytime in any script.
Now newest version support MySQL and SQLite both at the same time!!

What's the advantage?
In memory variables let you read their values across multiple scripts, and saving/loading them makes it powerful and fascinating.

Also, make use of MySQL and SQLite both at the same time without any hassle or errors.

Functions/stocks
Player Variables:
Code:
SavePVar(playerid, varname[], bool:db_type_sqlite = true);
LoadPVar(playerid, varname[], bool:db_type_sqlite = true);
Server Variables:
Code:
SaveSVar(varname[], bool:db_type_sqlite = true);
LoadSVar(varname[], bool:db_type_sqlite = true);
Example
This is a simple stats saving and loading filterscript, which saves the user data from PVars to DB under OnPlayerDisconnect and loads in OnPlayerConnect.

Nothing could be as simple as this user database. Yes, you may also create a whole admin script/user database with this include, its pretty safe and faster according to the standards.
Code:
#define FILTERSCRIPT

#include <a_samp>
#include <vars>

public OnPlayerConnect(playerid)
{
	// Load user data to PVars
	LoadPVar(playerid, "kills");
	LoadPVar(playerid, "deaths");
	LoadPVar(playerid, "score");
	LoadPVar(playerid, "money");
	
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
	// Save user PVar values to database
	SavePVar(playerid, "kills");
	SavePVar(playerid, "deaths");
	SavePVar(playerid, "score");
	SavePVar(playerid, "money");

	return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
	// Add +1 to kills
	SetPVarInt(killerid, "kills", GetPVarInt(killerid, "kills") + 1);
	// Add +1 to deaths
	SetPVarInt(playerid, "deaths", GetPVarInt(playerid, "deaths") + 1);

	// Add +1 to score
	SetPVarInt(killerid, "score", GetPVarInt(killerid, "score") + 1);

	// Add +500 to money
	GivePlayerMoney(killerid, 500);
	SetPVarInt(killerid, "money", GetPVarInt(killerid, "money") + 500);

	return 1;
}
Switching database types
You can switch to any database by just setting the optional boolean parameter "db_type_sqlite" in all PVar/SVar functions of this include.

db_type_sqlite = false
(Works on MySQL database, so all the data will be written or read from MySQL database)

db_type_sqlite = true
(Works on SQLite database, so all the data will be written or read from SQLite <.db file> database)

Download
https://github.com/Gammix/Vars-Database
Reply
#2

This looks good, I'll be watching this.
Reply
#3

* Requires you to Save and Load only once (recommended - OnPlayerConnect/Disconnect)

This is not recommended, because of a server crash or host problem or some other situation, user data would be lost. It would be better to save once the variable is changed.
Reply
#4

PVars are never the way to go except in a few circumstances they are simply too slow and offer no error checking for anything practical.
Reply
#5

If anything, they should automatically save, like this: https://sampforum.blast.hk/showthread.php?tid=545163

Emmet_ also said he was adding more saving types (SQLite, MySQL, Y_INI) to that.
Reply
#6

Quote:
Originally Posted by Gigi-The-Beast
View Post
* Requires you to Save and Load only once (recommended - OnPlayerConnect/Disconnect)

This is not recommended, because of a server crash or host problem or some other situation, user data would be lost. It would be better to save once the variable is changed.
If you'd read the code, or even thought further than a few lines of code, that's not hard to fix at all.
Reply
#7

Quote:
Originally Posted by Sew_Sumi
View Post
If you'd read the code, or even thought further than a few lines of code, that's not hard to fix at all.
I know it is an easy fix, just commented on his statement in the first post.
Reply
#8

Quote:
Originally Posted by Gigi-The-Beast
View Post
I know it is an easy fix, just commented on his statement in the first post.
Actually your reply wasn't relevant. If you see the main purpose of this include, its to use PVars directly instead of running queries every instant, and to save on disconnection between the subject.
Reply
#9

Could you make a MySQL version? I would greatly appreciate it! (I'd suggest using BueG MySQL; since that is the most actively developed one)

Thanks!
Reply
#10

Updated!
- Added new support for Server variables (SVars).
- New form of code, now pre creates columns.
(also checkout the new example)

MySQL version to be added soon.
Reply
#11

Very interesting, good job Gammix!
However I have some questions, is it saved through .ini or something? And how about the speed?
Keep it up!
Reply
#12

Are if Server Restarted the PVar Keep Saved ?
Anyway Very simple
Reply
#13

Quote:
Originally Posted by Uberanwar
View Post
Very interesting, good job Gammix!
However I have some questions, is it saved through .ini or something? And how about the speed?
Keep it up!
It works using SQLite and speed of it pretty usual to what SQL speed is.

Quote:
Originally Posted by Amunra
View Post
Are if Server Restarted the PVar Keep Saved ?
Anyway Very simple
It depends upon you, if you save the PVars/SVars they get stored into the database, abut it is also necessary to load their values so yes, you can do that.

And by the way, PVars shouldn't be saved when server restart, no relevance. (i assume you meant SVars).

Also, check the example which demonstrates how you can save/load PVars.
Reply
#14

I am a bit curious.. is this suitable for login/registration system as a whole?
And is account offline banning system possible with this include?
Reply
#15

Quote:
Originally Posted by Uberanwar
View Post
I am a bit curious.. is this suitable for login/registration system as a whole?
And is account offline banning system possible with this include?
Yes definitely, but you have to use db_query to handle offline users.
Reply
#16

Update v3 [BETA]:
  • Important bug fixes (for saving issues)
  • Support for MySQL added (all in one, use MySQL and SQLite at once)
Reply
#17

So by doing what's below will execute 4 queries?
Code:
SavePVar(playerid, "kills");
SavePVar(playerid, "deaths");
SavePVar(playerid, "score");
SavePVar(playerid, "money");
Reply
#18

Quote:
Originally Posted by SickAttack
View Post
So by doing what's below will execute 4 queries?
Code:
SavePVar(playerid, "kills");
SavePVar(playerid, "deaths");
SavePVar(playerid, "score");
SavePVar(playerid, "money");
Yes, and i will add a new version soon which will save all inserted PVars in an execute interval.
Reply
#19

Quote:
Originally Posted by Gammix
View Post
Yes, and i will add a new version soon which will save all inserted PVars in an execute interval.
Instead of execution intervals, you should just add transaction functions.

Like, StartPVarQuery and EndPVarQuery (I can't think of better names atm).

StartPVarQuery would start a 'BEGIN' query, and EndPVarQuery would be 'COMMIT'.
Reply
#20

Quote:
Originally Posted by Crayder
View Post
Instead of execution intervals, you should just add transaction functions.

Like, StartPVarQuery and EndPVarQuery (I can't think of better names atm).

StartPVarQuery would start a 'BEGIN' query, and EndPVarQuery would be 'COMMIT'.
What i meant was sort of similar to this logic.
Example:
Code:
SavePVar(playerid, "Test");
// The execute interval has started

// New variables added (query isn't executed)
SavePVar(playerid, "Test2");
SavePVar(playerid, "Test3");
SavePVar(playerid, "Test4");

// After 'X' seconds (say 2 secs), the query is executed and result is free!
(similar with loading functions)
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)