03.11.2015, 02:24
(
Последний раз редактировалось ThePhenix; 06.11.2015 в 03:13.
)
Easy - MySQL
En general:
Bueno, este include ya lo habнa publicado en la secciуn de inglйs, pero bueno como se tambiйn espaсol pense en por quй no publicar en esta secciуn tambiйn.
Bбsicamente, este include simplifca algunos queries que son muy empleados por la mayorнa(ojo hay algunas cosas que obviamente faltan), con este include te ahorrarбs el uso de strcat para concatenar strings y obtener ese error que te dice que alguna string es demasiado larga o alguna cosa por el estilo.
Ventajas:
- Primero que todo, como ya dije arriba realmente simplifica el uso de queries pues este include une las partes que conforman al query internamente.
- Si alguna vez usaste Y_INI o algъn otro sistema parecido esto te resultarб muy simple.
- Puedes guardar una gran cantidad de datos en la base de datos sin preocuparte en concatenar varias partes a la vez.
- Puedes crear y manejar tablas desde el script.
- No necesitas ser un genio en MySQL para usar esto.
Funciones:
Код:
Added in v2.0 native SQL::Open(SQL::qtypes:type, const table[], const column[] = "", columnID = -1, connectionHandle = 1); native SQL::OpenEx(SQL::qtypes:type, const table[], const column[] = "", columnID[] = "", connectionHandle = 1) native SQL::ToggleAutoIncrement(handle, bool:toggle); native SQL::WriteFloat(handle, const field[], Float:value); native SQL::WriteInt(handle, const field[], value); native SQL::WriteString(handle, const field[], const value[]); native SQL::ReadInt(handle, const field[], &dest); native SQL::ReadFloat(handle, const field[], &Float:dest); native SQL::ReadString(handle, const field[], dest[], len = sizeof(dest)); native SQL::Close(handle); native SQL::Connect(const host[], const user[], const database[], const password[], bool:debugging = false, port = 3306, bool:autoreconnect = true, pool_size = 2); native SQL::DeleteRow(const table[], const column[], columnID, connectionHandle = 1); native SQL::DeleteRowEx(const table[], const column[], columnID[], connectionHandle = 1); native SQL::GetIntEntry(const table[], const field[], const column[], columnID, connectionHandle = 1); native Float:SQL::GetFloatEntry(const table[], const field[], const column[], columnID, connectionHandle = 1); native SQL::GetStringEntry(const table[], const field[], const column[], columnID, dest[], len = sizeof(dest), connectionHandle = 1); native SQL::GetStringEntryEx(const table[], const field[], const column[], const scolumn[], dest[], len = sizeof(dest), connectionHandle = 1) native SQL::GetIntEntryEx(const table[], const field[], const column[], scolumn[], connectionHandle = 1); native Float:SQL::GetFloatEntryEx(const table[], const field[], const column[], scolumn[], connectionHandle = 1); native SQL::CreateTable(const tablename[], connectionHandle = 1); native SQL::AddTableEntry(handle, const field[], SQL::datatypes: type = SQL_TYPE_INT, maxlength = 11, bool:auto_increment = false, bool:setprimary = false); native SQL::SetIntEntry(const table[], const field[], value, const column[], columnID, connectionHandle = 1); native SQL::SetIntEntryEx(const table[], const field[], value, const column[], columnID[], connectionHandle = 1); native SQL::SetFloatEntry(const table[], const field[], Float:value, const column[], columnID, connectionHandle = 1); native SQL::SetFloatEntryEx(const table[], const field[], Float:value, const column[], columnID[], connectionHandle = 1); native SQL::SetStringEntry(const table[], const field[], const value[], const column[], columnID, bool:use_real_escape = true, connectionHandle = 1); native SQL::SetStringEntryEx(const table[], const field[], const value[], const column[], columnID[], bool:use_real_escape = true, connectionHandle = 1); native SQL::ExistsTable(const tablename[], connectionHandle = 1); native SQL::CountRows(const tablename[], connectionHandle = 1); native SQL::CountTables(connectionHandle = 1); native SQL::DropTable(const tablename[], connectionHandle = 1); native SQL::DeleteColumn(const table[], const column[], connectionHandle = 1); native SQL::Begin(connectionHandle = 1) native SQL::Commit(connectionHandle = 1)
PHP код:
/*
* Simple sistema de registraciуn e inicio de sesiуn usando easy-mysql.inc
*/
#include <a_samp>
#include <easy-mysql>
main()
{
}
#define mysql_host "localhost"
#define mysql_user "root"
#define mysql_db "server"
#define mysql_pass ""
#define mysql_debugging_enabled (true)
#define DIALOG_LOGIN 0
#define DIALOG_REGISTER 1
enum p_info
{
p_id,
p_name[24],
p_password[64],
p_score,
Float:p_posx,
Float:p_posy,
Float:p_posz,
p_loggedin
};
new UserInfo[MAX_PLAYERS][p_info];
stock ret_pName(playerid)
{
new name[24];
GetPlayerName(playerid, name, sizeof(name));
return name;
}
public OnGameModeInit()
{
//Conecciуn a la base MySQL
SQL::Connect(mysql_host, mysql_user, mysql_db, mysql_pass);
//Verificando si la table "players" existe
if(!SQL::ExistsTable("players"))
{
//Si no existe, entonces el script crearб una tabla llamada "players"
new handle = SQL::Open(SQL::CREATE, "players"); //Abrimos un 'handle' para crear la tabla.
SQL::AddTableEntry(handle, "p_id", SQL_TYPE_INT, 11, true);
SQL::AddTableEntry(handle, "p_name", SQL_TYPE_VCHAR, 24);
SQL::AddTableEntry(handle, "p_password", SQL_TYPE_VCHAR, 64);
SQL::AddTableEntry(handle, "p_score", SQL_TYPE_INT);
SQL::AddTableEntry(handle, "p_posx", SQL_TYPE_FLOAT);
SQL::AddTableEntry(handle, "p_posy", SQL_TYPE_FLOAT);
SQL::AddTableEntry(handle, "p_posz", SQL_TYPE_FLOAT);
SQL::Close(handle);//Cerramos el 'handle' que abrimos previamente.
}
return 1;
}
public OnPlayerConnect(playerid)
{
UserInfo[playerid][p_loggedin] = 0; UserInfo[playerid][p_score] = 0; UserInfo[playerid][p_posx] = 1958.3783;
UserInfo[playerid][p_posy] = 1343.1572; UserInfo[playerid][p_posz] = 15.3746;
if(SQL::RowExistsEx("players", "p_name", ret_pName(playerid))) //Verificamos si el nombre del jugador existe en la tabla 'players'
{
//Obtenemos la contraseсa y el ID en la base de datos del jugador.
new handle = SQL::OpenEx(SQL::READ, "players", "p_name", ret_pName(playerid));
SQL::ReadString(handle, "p_password", UserInfo[playerid][p_password], 64);
SQL::ReadInt(handle, "p_id", UserInfo[playerid][p_id]);
SQL::Close(handle);
//Mostramos el diбlogo de ingreso.
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{0080FF}Login", "Please input your password below to log in.", "Login", "Exit");
}
else
{
//Si no estб registrado le mostramos el diбlogo de registro.
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{0080FF}Register", "Please input a password below to register in.", "Login", "Exit");
}
return 1;
}
public OnPlayerSpawn(playerid)
{
SetPlayerPos(playerid, UserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(UserInfo[playerid][p_loggedin] == 1)
{
//Si el jugador ingresу a su cuenta, guardamos su informaciуn.
GetPlayerPos(playerid, UserInfo[playerid][p_posx], UserInfo[playerid][p_posy], UserInfo[playerid][p_posz]);
new handle = SQL::Open(SQL::UPDATE, "players", "p_id", UserInfo[playerid][p_id]);
SQL::WriteInt(handle, "p_score", GetPlayerScore(playerid));
SQL::WriteFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
SQL::WriteFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
SQL::WriteFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
SQL::Close(handle);
}
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_REGISTER:
{
if(!response) return Kick(playerid);
if(strlen(inputtext) < 5)
{
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{0080FF}Register", "Please input a password below to register in.", "Login", "Exit");
return 1;
}
SHA256_PassHash(inputtext, "", UserInfo[playerid][p_password], 64);
new handle = SQL::Open(SQL::INSERT, "players");
SQL::ToggleAutoIncrement(handle, true);//Activa el auto incremento es decir que esta la funciуn SQL::Close retornarб cache_insert_id();
SQL::WriteString(handle, "p_name", ret_pName(playerid));
SQL::WriteString(handle, "p_password", UserInfo[playerid][p_password]);
SQL::WriteInt(handle, "p_score", 0);
SQL::WriteFloat(handle, "p_posx", 0.0);
SQL::WriteFloat(handle, "p_posy", 0.0);
SQL::WriteFloat(handle, "p_posz", 0.0);
UserInfo[playerid][p_id] = SQL::Close(handle);
SendClientMessage(playerid, -1, "Successfully registered in!");
UserInfo[playerid][p_loggedin] = 1;
}
case DIALOG_LOGIN:
{
if(!response) Kick(playerid);
new hash[64];
SHA256_PassHash(inputtext, "", hash, 64);
if(!strcmp(hash, UserInfo[playerid][p_password]))
{
//Cargamos la informaciуn del jugador.
new handle = SQL::Open(SQL::READ, "players", "p_id", UserInfo[playerid][p_id]);
SQL::ReadInt(handle, "p_score", UserInfo[playerid][p_score]);
SQL::ReadFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
SQL::ReadFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
SQL::ReadFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
SQL::Close(handle);//You must close the handle.
SetPlayerScore(playerid, UserInfo[playerid][p_score]);
UserInfo[playerid][p_loggedin] = 1;
SendClientMessage(playerid, -1, "Successfully logged in!");
}
else
{
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{0080FF}Login", "Please input your password below to log in.", "Login", "Exit");
}
}
}
return 1;
}
Код:
SQL_TYPE_INT //Se lo usa para enteros SQL_TYPE_VCHAR //Se lo usa para strings SQL_TYPE_FLOAT //Se lo usa para como flotante SQL_INVALID_HANDLE //Un 'handle' invбlido. SQL::UPDATE //Lo usas cuando quieres guardar informaciуn, es decir que ya existe un record en la base de datos. SQL::INSERT //Lo usas para ingresar nueva informaciуn a la base de datos, es decir que no existe un record aun en la base de datos. SQL::READ //Lo usas para obtener o cargar informaciуn de la base de datos. SQL::CREATE //Lo usas para crear una nueva tabla.
Creando una tabla.
PHP код:
new handle = SQL::Open(SQL::CREATE, "players");
SQL::AddTableEntry(handle, "p_id", SQL_TYPE_INT, 11, true);
SQL::AddTableEntry(handle, "p_name", SQL_TYPE_VCHAR, 24);
SQL::AddTableEntry(handle, "p_password", SQL_TYPE_VCHAR, 64);
SQL::AddTableEntry(handle, "p_score", SQL_TYPE_INT);
SQL::AddTableEntry(handle, "p_posx", SQL_TYPE_FLOAT);
SQL::AddTableEntry(handle, "p_posy", SQL_TYPE_FLOAT);
SQL::AddTableEntry(handle, "p_posz", SQL_TYPE_FLOAT);
SQL::Close(handle);
PHP код:
new handle = SQL::Open(SQL::INSERT, "players");
SQL::ToggleAutoIncrement(handle, true);//Toggles auto increment, SQL::Close will return cache_insert_id();
SQL::WriteString(handle, "p_name", ret_pName(playerid));
SQL::WriteString(handle, "p_password", UserInfo[playerid][p_password]);
SQL::WriteInt(handle, "p_score", 0);
SQL::WriteFloat(handle, "p_posx", 0.0);
SQL::WriteFloat(handle, "p_posy", 0.0);
SQL::WriteFloat(handle, "p_posz", 0.0);
UserInfo[playerid][p_id] = SQL::Close(handle);
PHP код:
new handle = SQL::Open(SQL::UPDATE, "players", "p_id", UserInfo[playerid][p_id]);
SQL::WriteInt(handle, "p_score", GetPlayerScore(playerid));
SQL::WriteFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
SQL::WriteFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
SQL::WriteFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
SQL::Close(handle);
"Leyendo informaciуn de la base de datos.
PHP код:
new handle = SQL::Open(SQL::READ, "players", "p_id", UserInfo[playerid][p_id]);
SQL::ReadInt(handle, "p_score", UserInfo[playerid][p_score]);
SQL::ReadFloat(handle, "p_posx", UserInfo[playerid][p_posx]);
SQL::ReadFloat(handle, "p_posy", UserInfo[playerid][p_posy]);
SQL::ReadFloat(handle, "p_posz", UserInfo[playerid][p_posz]);
SQL::Close(handle);//Debes cerrar el 'handle' abierto..
Код:
native SQL::Close(handle)
Obviamente me falta documentar las demбs funciones pero lo harй luego.
He aсadido dos funciones mбs para realizar transacciones:
Код:
native SQL::Begin(connectionHandle = 1) native SQL::Commit(connectionHandle = 1)
V 2.0
Necesitas el plugin de MySQL de BlueG
MySQL Plugin
Si tienes alguna duda, sugerencia, reporte o alguna cosa publica abajo, gracias.