27.06.2014, 13:41
Lets face it Konstantinos the way he is trying to do it is not a very good way this is not how to design a dynamic system in your server with sqlite.
He doesn't need to have a primary integer key for this kind of system here is what I would do.
He doesn't need to have a primary integer key for this kind of system here is what I would do.
pawn Код:
#include <YSI\y_hooks>
#define MAX_HOUSES 1000
#define HOUSEBOUNDSCHECK(%0) if(%0 < 0 || %0 >= MAX_HOUSES) return -1
enum HOUSEENUM
{
Name[21],
Owner[21],
Float:EnterX,
Float:EnterY,
Float:EnterZ,
Float:InterX,
Float:InterY,
Float:InterZ,
Cost,
Interior,
World,
EnPickup,
ExPickup,
}
static HouseInfo[MAX_HOUSES][HOUSEENUM]
// Keep track of houses
static Iterator:HouseIter<MAX_HOUSES>;
hook OnGameModeInit()
{
// Create table
db_query(BFU, "CREATE TABLE IF NOT EXISTS `HouseData` (HouseID INTEGER, name TEXT, owner, TEXT, enterx REAL, entery REAL, enterz REAL, interx REAL, intery REAL, interz REAL, cost INTEGER, interior INTEGER, world INTEGER)");
// Load all data
new DBResult:r = db_query(BFU, "SELECT * FROM `HouseData`);
if(db_num_rows®)
{
// Load data into HouseInfo
for(new i = 0; i < db_num_rows®; i++)
{
new Field[32];
db_get_field_assoc(Result, "HouseID", Field, 9); hid = strval(Field);
db_get_field_assoc(Result, "name", Field, 21); format(HouseInfo[hid][Name], 21, "%s", Field);
db_get_field_assoc(Result, "owner", Field, 21); format(HouseInfo[hid][Owner], 21, "%s", Field);
db_get_field_assoc(Result, "enterx", Field, 20); HouseInfo[hid][EnterX] = floatstr(Field);
db_get_field_assoc(Result, "entery", Field, 20); HouseInfo[hid][EnterY] = floatstr(Field);
db_get_field_assoc(Result, "enterz", Field, 20); HouseInfo[hid][EnterZ] = floatstr(Field);
db_get_field_assoc(Result, "interx", Field, 20); HouseInfo[hid][InterX] = floatstr(Field);
db_get_field_assoc(Result, "intery", Field, 20); HouseInfo[hid][InterY] = floatstr(Field);
db_get_field_assoc(Result, "interz", Field, 20); HouseInfo[hid][InterZ] = floatstr(Field);
db_get_field_assoc(Result, "cost", Field, 8); HouseInfo[hid][Cost] = strval(Field);
db_get_field_assoc(Result, "interior", Field, 5); HouseInfo[hid][Interior] = strval(Field);
db_get_field_assoc(Result, "world", Field, 5); HouseInfo[hid][World] = strval(Field);
db_next_row®;
// Create the house providing the index will ignore inserting into database
CreateHouse(HouseInfo[hid][Name], HouseInfo[hid][Owner],
HouseInfo[hid][EnterX], HouseInfo[hid][EnterY], HouseInfo[hid][EnterZ],
HouseInfo[hid][InterX], HouseInfo[hid][InterY], HouseInfo[hid][InterZ],
HouseInfo[hid][Cost], HouseInfo[hid][Interior], HouseInfo[hid][World], hid)
db_next_row®;
}
}
db_free_result®;
return 1;
}
// Create a house
CreateHouse(hname[], howner[], Float:henterx, Float:hentery, Float:henterz, Float:hinterx, Float:hintery, Float:hinterz, hcost, hinterior, hworld, hloadindex=-1)
{
// Load
new index;
// No load index provided find a new load index
if(hloadindex == -1)
{
// Get free index
index = Iter_Free(HouseIter);
// Too many houses
if(index == -1)
{
print("Error::CreateHouse::Too many houses");
return -1;
}
// Initialize house variables for this index
format(HouseInfo[index][Name], 21, "%s", hname);
format(HouseInfo[index][Owner], 21, "%s", howner);
HouseInfo[index][EnterX] = henterx;
HouseInfo[index][EnterY] = hentery;
HouseInfo[index][EnterZ] = henterz;
HouseInfo[index][InterX] = henterx;
HouseInfo[index][InterY] = hentery;
HouseInfo[index][InterZ] = henterz;
HouseInfo[index][Cost] = hcost;
HouseInfo[index][Interior] = hinterior;
HouseInfo[index][World] = hworld;
// Insert into database
new q[128];
format(q, sizeof(q), "INSERT INTO houses (HouseID, name, owner, enterx, entery, enterz, interx, intery, interz, cost, interior, world) VALUES ('%s', '%s', '%f', '%f', '%f', '%f', '%f', '%f', %d, %d, %d)",
index, hname, howner, henterx, hentery, henterz, hinterx, hintery, hinterz, hcost, hinterior, hworld);
db_query(BFU, q);
}
else
{
// Make sure an OOB index was not given
HOUSEBOUNDSCHECK(hloadindex);
index = hloadindex;
}
// Create the house pickups
HouseInfo[index][EnPickup] = CreatePickup(1273, 1, HouseInfo[index][EnterX], HouseInfo[index][EnterY], HouseInfo[index][EnterZ], 0);
HouseInfo[index][ExPickup] = CreatePickup(1273, 1, HouseInfo[index][InterX], HouseInfo[index][InterY], HouseInfo[index][InterZ], 0);
return index;
}
CMD:createhouse(playerid, params[])
{
new Float:EX, Float:EY, Float:EZ, Float:IX, Float:IY, Float:IZ, Cast, Int, Word, name[21], owna[12], line[128];
if(sscanf(params, "fffddds[21]s[21]", IX, IY, IZ, Cast, Int, Word, name, owna)) return SendClientMessage(playerid, RED, "/createhouse IX, IY, IZ, Cost, InteriorID, WorldID, name, owner");
new hid = CreateHouse(name, owna, EX, EY, EZ, IX, IY, IZ, Cast, Int, World);
new str[128];
format(str,sizeof(str), "created house id %d", hid);
SendClientMessage(playerid, 0xFF0000AA, str);
return 1;
}