Beim MySQL Plugin von StrickenKid sind dafür ein paar Ansätze:
example.pwn
#include <a_samp>
#include <mysql>
// Finally updated this - 3/16/2011
// SA:MP MySQL Plugin 2.1.1
new playerKills[MAX_PLAYERS];
new playerDeaths[MAX_PLAYERS];
new playerMoney[MAX_PLAYERS];
new playerBank[MAX_PLAYERS];
main()
{
print("SA:MP MySQL Plugin Test Script - By: StrickenKid");
}
stock CreateCheckpoint(Float:X, Float:Y, Float:Z)
{
#pragma unused X, Y, Z
return 1;
}
public OnGameModeInit()
{
SetGameModeText("Test Plugin Script");
mysql = mysql_init(LOG_ALL);
// Connect to the database.
mysql_connect("host", "username", "password", "db", mysql);
// This is an example to load checkpoints from your database.
new data[256], field[3][32];
mysql_query("SELECT x, y, z FROM `checkpoints`");
mysql_store_result();
while (mysql_fetch_row(data))
{
split(data, field, '|');
CreateCheckpoint(floatstr(field[0]), floatstr(field[1]), floatstr(field[2]));
}
mysql_free_result();
return 1;
}
public OnGameModeExit()
{
// Close mysql connection.
mysql_close(mysql);
return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
new data[256];
// Example 1:
if (!strcmp("/login", cmdtext, true, 6))
{
new field[4][32], query[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(query, sizeof(query), "SELECT `password`, kills, deaths, money, bank FROM `accounts` WHERE `username`='%s'", pname);
mysql_query(query);
mysql_store_result();
if (mysql_fetch_row(data))
{
split(data, field, '|');
if (!strcmp(field[0], cmdtext[7], false))
{
playerKills[playerid] = strval(field[0]);
playerDeaths[playerid] = strval(field[1]);
playerMoney[playerid] = strval(field[2]);
playerBank[playerid] = strval(field[3]);
}
else
{
// Password incorrect.
}
}
else
{
// Account does not exist.
}
mysql_free_result();
}
// Example 2:
if (!strcmp("/login", cmdtext, true, 6))
{
new query[128], pname[MAX_PLAYER_NAME];
GetPlayerName(playerid, pname, sizeof(pname));
format(query, sizeof(query), "SELECT `password`, kills, deaths, money, bank FROM `accounts` WHERE `username`='%s'", pname);
mysql_query(query);
mysql_store_result();
if (mysql_fetch_field("password", data))
{
if (!strcmp(data, cmdtext[7], false))
{
mysql_fetch_field("kills", data);
playerKills[playerid] = strval(data);
mysql_fetch_field("deaths", data);
playerDeaths[playerid] = strval(data);
mysql_fetch_field("money", data);
playerMoney[playerid] = strval(data);
mysql_fetch_field("bank", data);
playerBank[playerid] = strval(data);
}
else
{
// Password incorrect.
}
}
else
{
// Account does not exist.
}
mysql_free_result();
}
return 0;
}
callback_examples.pwn
// SA:MP MySQL Plugin Example Callback Usage
// Created By StrickenKid
// Finally updated this - 3/16/2011
// SA:MP MySQL Plugin 2.1.1
#include <a_samp>
#include <mysql>
#define MYSQL_HOST ""
#define MYSQL_USER ""
#define MYSQL_PASS ""
#define MYSQL_DB ""
#define MY_RESULT 1
#define MY_ARRAY_RESULT 2
public OnGameModeInit()
{
SetGameModeText("Test Plugin Script");
mysql = mysql_init(LOG_ALL);
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, mysql);
mysql_query("CREATE TABLE IF NOT EXISTS `test_table` (\
`test_int` INT( 2 ) NOT NULL DEFAULT '0',\
`test_char` VARCHAR( 15 ) NOT NULL ,\
`test_float` DECIMAL( 10, 4 ) NOT NULL DEFAULT '0.0000'\
) ENGINE = MYISAM ;", -1, 0, mysql);
mysql_query("INSERT INTO `test_table` (test_int, test_char, test_float) VALUES (5, 'Hello There!', 5.4232);", -1, 0, mysql);
mysql_query("INSERT INTO `test_table` (test_int, test_char, test_float) VALUES (7, 'Whats Up?', 7.4249);", -1, 0, mysql);
mysql_query("INSERT INTO `test_table` (test_int, test_char, test_float) VALUES (2, 'Wow Cool!', 252.5213);", -1, 0, mysql);
mysql_query("INSERT INTO `cawk` (test_int, test_char, test_float) VALUES (2, 'Wow Cool!', 252.5213);", -1, 0, mysql);
//----------
mysql_query_array("SELECT * FROM `test_table` WHERE `test_int` = 5", MY_ARRAY_RESULT, {5, 6, 7, 8, 9, 10}, mysql);
mysql_query("SELECT * FROM `test_table`", MY_RESULT, 0, mysql);
return 1;
}
public OnGameModeExit()
{
mysql_close(mysql);
return 1;
}
public OnMysqlQuery(resultid, spareid, MySQL:handle)
{
new buffer[64];
new field[3][15];
switch(resultid)
{
case MY_RESULT:
{
mysql_store_result(handle);
while (mysql_fetch_row(buffer, "|", handle))
{
split(buffer, field, '|');
printf("---------------------------------------");
printf("test_int: %d", strval(field[0]));
printf("test_char: %s", field[1]);
printf("test_float: %.4f", floatstr(field[2]));
}
mysql_free_result(handle);
}
}
return 1;
}
public OnMysqlQueryArray(resultid, extravars[], MySQL:handle)
{
new buffer[64];
switch(resultid)
{
case MY_ARRAY_RESULT:
{
mysql_store_result(handle);
if(mysql_fetch_row(buffer, "|", handle))
{
print(buffer);
printf("extravars[0]: %d", extravars[0]); //--> would print 5
printf("extravars[1]: %d", extravars[1]); //--> would print 6
printf("extravars[2]: %d", extravars[2]); //--> would print 7
printf("extravars[3]: %d", extravars[3]); //--> would print 8
printf("extravars[4]: %d", extravars[4]); //--> would print 9
printf("extravars[5]: %d", extravars[5]); //--> would print 10
}
mysql_free_result(handle);
}
}
return 1;
}
public OnMysqlError(error[], errorid, MySQL:handle)
{
// log errors to another database table maybe?
mysql_query("CREATE TABLE IF NOT EXISTS `mysql_errors` (\
`data` VARCHAR( 256 ) NOT NULL ,\
`errno` INT( 5 ) NOT NULL DEFAULT '0',\
`connection` INT( 2 ) NOT NULL DEFAULT '0'\
) ENGINE = MYISAM ;", -1, 0, handle);
new query[256], buffer[256];
// ALWAYS escape the raw data from error[], or you will get an error while trying to log an error, which will cause a loop of erros. D:
mysql_real_escape_string(error, buffer, handle);
// use _: to remove the MySQL: tag to avoid tag error.
format(query, sizeof(query), "INSERT INTO `mysql_errors` (data, errno, connection) VALUES ('%s', %d, %d);", buffer, errorid, _:handle);
mysql_query(query, -1, 0, handle);
// and maybe print it?
printf("MySQL Error! msg: \"%s\", error id: %d, connection handle: %d.", error, errorid, _:handle);
return 1;
}
Mit freundlichen Grüßen
eXchange