stock CreateFaction(fname[], Float:PlayerPosX, Float:PlayerPosY, Float:PlayerPosZ, ftype)
{
DB::CreateRow(FactionTable, "FactionName", fname);
new year, month, day;
getdate(year, month, day);
new fcID = GetAvailableFactionID();
FactionInfo[fcID][dbID] = ReturnLastKey();
FactionInfo[fcID][fID] = fcID;
FactionInfo[fcID][fType] = ftype;
FactionInfo[fcID][fX] = PlayerPosX;
FactionInfo[fcID][fY] = PlayerPosY;
FactionInfo[fcID][fZ] = PlayerPosZ;
new cdate[30];
format(cdate, sizeof cdate, "%02d/%02d/%d", day, month, year);
DB::SetLastIntEntry(FactionTable, "fType", ftype, "Reference");
DB::SetLastFloatEntry(FactionTable, "LockerX", PlayerPosX, "Reference");
DB::SetLastFloatEntry(FactionTable, "LockerY", PlayerPosY, "Reference");
DB::SetLastFloatEntry(FactionTable, "LockerZ", PlayerPosZ, "Reference");
DB::SetLastStringEntry(FactionTable, "CreationDate", cdate, "Reference");
LoadFactions();
#if defined DEBUG_MODE
printf("[FACTION CREATION] %s has been created today (%s). [IG ID: %d | SQL ID: %d]", fname, cdate, fcID, FactionInfo[fcID][dbID]);
#endif
return 1;
}
stock GetAvailableFactionID()
{
new id;
for(new i; i < MAX_FACTIONS; i++)
{
if(FactionInfo[i][dbID] == 0)
{
id = i;
break;
}
}
return id;
}
stock ReturnLastKey()
{
return DB::GetRowsPoolSize(FactionTable);
}
I just don't know how to create /makefaction command, to be more precise, how to check if
there is available ID in table "FACTIONS" and use that id for faction I created. |
CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID) );
format(Query, sizeof(Query), "INSERT INTO `Persons` (`ID`, `LastName`, `FirstName`, `Age`) VALUES(NULL,'%s', '%s', '%d')", LastNameVar[playerid], FirstNameVar[playerid], AgeVar[playerid]);
SQL has nice options called autoincrement and primary key.
You can set these as fieldname ID for example. Take a look at this query to create a table: Код:
CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID) ); This feature allows you to have an unique ID for each row in the database and you are not required to count it scriptwise. For example when there are 4 rows in the table with id 1,2,3 and 4, SQL automatically created ID 5 at the next row and 6 for the one after that and go on.. All you have to do in your query is putt "null" as ID and SQL will do the rest for you: PHP код:
|