O MySQL й um sistema de gerenciamento de banco de dados (SGBD), que utiliza a linguagem SQL (Linguagem de Consulta Estruturada, do inglкs Structured Query Language) como interface. Й atualmente um dos bancos de dados mais populares, com mais de 10 milhхes de instalaзхes pelo mundo. Entre os usuбrios do banco de dados MySQL estгo: NASA, Friendster, Banco Bradesco, Dataprev, HP, Nokia, Sony, Lufthansa, U.S. Army, U.S. Federal Reserve Bank, Associated Press, Alcatel, Slashdot, Cisco Systems, ****** e outros. ver mais |
I have only ran tests with the house loading code, and here are some results (500 rows)...
With mysql_fetch_row, no caching 1. mysql_fetch_row calls took 3065 milliseconds 2. sscanf (parsing) took 27ms in total (500 calls to function) 3. the rest of the house loading code took 129ms (irrelevant) Total: 3221ms With caching 1. cache functions (loading and parsing) took 166ms 2. the rest of the house loading code took 108ms (irrelevant) Total: 274ms From this, we can tell that using the caching functions is about 12 times faster in such example (with loops and huge rows of data being returned). |
#include <a_mysql>
new const
mysql_host[] = "localhost",
mysql_user[] = "root",
mysql_database[] = "server",
mysql_password[] = "1234";
new Query[500];
new MysqlC;
#define MAX_SOLTS (25)
#define D_LOGIN 1
#define D_REGISTER 2
new PlayerName[25];
enum pInfo{
pLevel,
pMoney,
pBank,
pSkin,
pWarn
};
new PlayerInfo[MAX_SOLTS][pInfo];
forward MySQL_Start();
forward r@MySQL_Start(text[]);
forward MySQL_CheckAccount(playerid);
forward r@MySQL_CheckAccount(playerid);
forward MySQL_CheckPassword(playerid,password[]);
forward r@MySQL_CheckPassword(playerid);
forward MySQL_CreateAccount(playerid,password[]);
forward r@MySQL_CreateAccount(playerid);
forward MySQL_LoadAccount(playerid);
forward r@MySQL_LoadAccount(playerid);
forward MySQL_SaveAccount(playerid);
forward r@MySQL_SaveAccount(playerid);
forward PlayerSpawn(playerid);
public MySQL_Start(){
MysqlC = mysql_connect(mysql_host,mysql_user,mysql_database,mysql_password);
if(!MysqlC)
return print("Nгo foi possivel conectar a database, verifique as definiзхes novamente.");
else {
mysql_function_query(MysqlC,
"CREATE TABLE IF NOT EXISTS `users` (\
`id` int(11) NOT NULL, AUTO_INCREMENT,\
`name` varchar(25) NOT NULL, \
`password` varchar(25) NOT NULL, \
`level` int(11) NOT NULL, \
`bank` int(11) NOT NULL, \
`skin` int(11) NOT NULL, \
`money` int(11) NOT NULL, \
PRIMARY KEY(`id`)\
)",false,"r@MySQL_Start","s","users");
}
return 1;
}
public r@MySQL_Start(text[])
return printf("Tabela %s verificada com sucesso.",text);
pawn Код:
|
Originally Posted by MySQL
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
Um nъmero de ponto flutuante de tamanho normal (dupla-precisгo). Valores permitidos estгo entre -1.7976931348623157E+308 e -2.2250738585072014E-308, 0 e entre 2.2250738585072014E-308 e 1.7976931348623157E+308. Se UNSIGNED for especificado, valores negativos nгo sгo permitidos. O M й a largura do display e o D й nъmero de casa decimais. DOUBLE sem argumento ou FLOAT(X) onde 25 <= X <= 53 sгo nъmeros de ponto flutuante de dupla-precisгo. |
public OnFilterScriptInit()
{
MySQL_Start();
return 1;
}
public OnGameModeInit()
{
MySQL_Start();
return 1;
}
public OnPlayerConnect(playerid)
{
MySQL_CheckAccount(playerid);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
MySQL_SaveAccount(playerid);
return 1;
}
public MySQL_CheckAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckAccount","d",playerid);
return 1;
}
public r@MySQL_CheckAccount(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows){
ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
} else {
ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
}
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid){
case D_LOGIN:{
if(!response)
Kick(playerid);
else {
if(strlen(inputtext) < 4)
return ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
else
MySQL_CheckPassword(playerid,inputtext);
}
}
case D_REGISTER:{
if(!response)
Kick(playerid);
else {
if( 5 < (strlen(inputtext)) > 25)
return ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
else
MySQL_CreateAccount(playerid,inputtext);
}
}
}
return 1;
}
public MySQL_CheckPassword(playerid,password[]){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s' AND password='%s'",PlayerName,password);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckPassword","ds",playerid);
return 1;
}
public r@MySQL_CheckPassword(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows)
MySQL_LoadAccount(playerid);
else
ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
return 1;
}
public MySQL_CreateAccount(playerid,password[]){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"INSERT INTO `users` (name,password,level,money,bank,skin) VALUES ('%s','%s','2','4500','0','240');",PlayerName,password);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CreateAccount","s",playerid);
return 1;
}
public r@MySQL_CreateAccount(playerid){
SendClientMessage(playerid,-1,"Registrado com sucesso, aguarde um pouco...");
MySQL_LoadAccount(playerid);
return 1;
}
public MySQL_LoadAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
mysql_function_query(MysqlC,Query,true,"r@MySQL_LoadAccount","d",playerid);
return 1;
}
public r@MySQL_LoadAccount(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows){
cache_get_field_content(0,"level",Query,MysqlC);
PlayerInfo[playerid][pLevel] = strval(Query);
cache_get_field_content(0,"money",Query,MysqlC);
PlayerInfo[playerid][pMoney] = strval(Query);
cache_get_field_content(0,"bank",Query,MysqlC);
PlayerInfo[playerid][pBank] = strval(Query);
cache_get_field_content(0,"skin",Query,MysqlC);
PlayerInfo[playerid][pSkin] = strval(Query);
PlayerSpawn(playerid);
}
else
SendClientMessage(playerid,-1,"Houve um erro com a sua conta"),Kick(playerid);
return 1;
}
public PlayerSpawn(playerid){
SpawnPlayer(playerid);
GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]);
SetPlayerScore(playerid,PlayerInfo[playerid][pLevel]);
SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]);
SendClientMessage(playerid,-1,"Seja bem vindo!");
return 1;
}
public MySQL_SaveAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"UPDATE `users` SET level='%d',money='%d',bank='%d',skin='%d' WHERE name='%s'",PlayerInfo[playerid][pLevel],PlayerInfo[playerid][pMoney],PlayerInfo[playerid][pBank],PlayerInfo[playerid][pSkin],PlayerName);
mysql_function_query(MysqlC,Query,false,"r@MySQL_SaveAccount","d",playerid);
return 1;
}
public r@MySQL_SaveAccount(playerid)
return printf("Conta %d salva com sucesso.",playerid);
///////////////////////////////////////////////////////////////////////////////
//
//
// #### ######## ###### ######## ######## ### ## ##
// ## ## ## ## ## ## ## ## ## ### ###
// ## ## ## ## ## ## ## ## #### ####
// ## ######## ###### ## ###### ## ## ## ### ##
// ## ## ## ## ## ######### ## ##
// ## ## ## ## ## ## ## ## ## ##
// #### ## ###### ## ######## ## ## ## ##
//
//
// Criado por @BlueX
//
// Seja membro da melhor equipe de programaзгo
// http://ips-team.forumeiros.com/t2-informacao-inscricao-na-ips
//
// [iPs]TeaM soluзхes de programaзгo em geral
//
//
// [MySQL Tutorial}
//
//
/////////////////////////////////////////////////////////////////////////////////
#define FILTERSCRIPT
#include <a_samp>
#include <a_mysql>
#define MAX_SOLTS (25)
#define D_LOGIN 1
#define D_REGISTER 2
new const
mysql_host[] = "localhost",
mysql_user[] = "root",
mysql_database[] = "server",
mysql_password[] = "1234";
new Query[500];
new MysqlC;
new PlayerName[25];
enum pInfo{
pLevel,
pMoney,
pBank,
pSkin,
pWarn
};
new PlayerInfo[MAX_SOLTS][pInfo];
forward MySQL_Start();
forward r@MySQL_Start(text[]);
forward MySQL_CheckAccount(playerid);
forward r@MySQL_CheckAccount(playerid);
forward MySQL_CheckPassword(playerid,password[]);
forward r@MySQL_CheckPassword(playerid);
forward MySQL_CreateAccount(playerid,password[]);
forward r@MySQL_CreateAccount(playerid);
forward MySQL_LoadAccount(playerid);
forward r@MySQL_LoadAccount(playerid);
forward MySQL_SaveAccount(playerid);
forward r@MySQL_SaveAccount(playerid);
forward PlayerSpawn(playerid);
public OnFilterScriptInit()
{
print("\n--------------------------------------");
print(" MySQL Tutorial @BlueX");
print("--------------------------------------\n");
MySQL_Start();
return 1;
}
public OnFilterScriptExit()
{
return 1;
}
public OnPlayerConnect(playerid)
{
MySQL_CheckAccount(playerid);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
MySQL_SaveAccount(playerid);
return 1;
}
public OnPlayerSpawn(playerid)
{
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid){
case D_LOGIN:{
if(!response)
Kick(playerid);
else {
if(strlen(inputtext) < 4)
return ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
else
MySQL_CheckPassword(playerid,inputtext);
}
}
case D_REGISTER:{
if(!response)
Kick(playerid);
else {
if( 5 < (strlen(inputtext)) > 25)
return ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
else
MySQL_CreateAccount(playerid,inputtext);
}
}
}
return 1;
}
public MySQL_Start(){
MysqlC = mysql_connect(mysql_host,mysql_user,mysql_database,mysql_password);
if(!MysqlC)
return print("Nгo foi possivel conectar a database, verifique as definiзхes novamente.");
else {
mysql_function_query(MysqlC,
"CREATE TABLE IF NOT EXISTS `users` (\
`id` int(11) NOT NULL, AUTO_INCREMENT,\
`name` varchar(25) NOT NULL, \
`password` varchar(25) NOT NULL, \
`level` int(11) NOT NULL, \
`bank` int(11) NOT NULL, \
`skin` int(11) NOT NULL, \
`money` int(11) NOT NULL, \
PRIMARY KEY(`id`)\
)",false,"r@MySQL_Start","s","users");
}
return 1;
}
public r@MySQL_Start(text[])
return printf("Tabela %s verificada com sucesso.",text);
public MySQL_CheckAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckAccount","d",playerid);
return 1;
}
public r@MySQL_CheckAccount(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows){
ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
} else {
ShowPlayerDialog(playerid,D_REGISTER,3,"registro","insira uma password para registrar","registrar","sair");
}
return 1;
}
public MySQL_CheckPassword(playerid,password[]){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s' AND password='%s'",PlayerName,password);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CheckPassword","ds",playerid);
return 1;
}
public r@MySQL_CheckPassword(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows)
MySQL_LoadAccount(playerid);
else
ShowPlayerDialog(playerid,D_LOGIN,3,"login","insira a sua password para logar","logar","sair");
return 1;
}
public MySQL_CreateAccount(playerid,password[]){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"INSERT INTO `users` (name,password,level,money,bank,skin) VALUES ('%s','%s','2','4500','0','240');",PlayerName,password);
mysql_function_query(MysqlC,Query,true,"r@MySQL_CreateAccount","s",playerid);
return 1;
}
public r@MySQL_CreateAccount(playerid){
SendClientMessage(playerid,-1,"Registrado com sucesso, aguarde um pouco...");
MySQL_LoadAccount(playerid);
return 1;
}
public MySQL_LoadAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"SELECT * FROM `users` WHERE name='%s'",PlayerName);
mysql_function_query(MysqlC,Query,true,"r@MySQL_LoadAccount","d",playerid);
return 1;
}
public r@MySQL_LoadAccount(playerid){
new rows,fields;
cache_get_data(rows,fields,MysqlC);
if(rows){
cache_get_field_content(0,"level",Query,MysqlC);
PlayerInfo[playerid][pLevel] = strval(Query);
cache_get_field_content(0,"money",Query,MysqlC);
PlayerInfo[playerid][pMoney] = strval(Query);
cache_get_field_content(0,"bank",Query,MysqlC);
PlayerInfo[playerid][pBank] = strval(Query);
cache_get_field_content(0,"skin",Query,MysqlC);
PlayerInfo[playerid][pSkin] = strval(Query);
PlayerSpawn(playerid);
}
else
SendClientMessage(playerid,-1,"Houve um erro com a sua conta"),Kick(playerid);
return 1;
}
public PlayerSpawn(playerid){
SpawnPlayer(playerid);
GivePlayerMoney(playerid,PlayerInfo[playerid][pMoney]);
SetPlayerScore(playerid,PlayerInfo[playerid][pLevel]);
SetPlayerSkin(playerid,PlayerInfo[playerid][pSkin]);
SendClientMessage(playerid,-1,"Seja bem vindo!");
return 1;
}
public MySQL_SaveAccount(playerid){
GetPlayerName(playerid,PlayerName,25);
format(Query,sizeof(Query),"UPDATE `users` SET level='%d',money='%d',bank='%d',skin='%d' WHERE name='%s'",PlayerInfo[playerid][pLevel],PlayerInfo[playerid][pMoney],PlayerInfo[playerid][pBank],PlayerInfo[playerid][pSkin],PlayerName);
mysql_function_query(MysqlC,Query,false,"r@MySQL_SaveAccount","d",playerid);
return 1;
}
public r@MySQL_SaveAccount(playerid)
return printf("Conta %d salva com sucesso.",playerid);
Muito bem explicado cara. Deve ter demorado um tempгo !! Parabйns pelo tutorial Blue
|
Excelente Gonзalo, hб pouco tempo fiz um tutorial contendo um sistema de registro, mas parece que nгo foi muito ъtil, porйm este й bem explicativo e aparentemente possui cуdigos funcionais, parabйns.
@OFF Tambйm tenho essa mania do r@. Acho que peguei com o Miki lendo o MyRPG xD. |