SA-MP Forums Archive
SQLite - Insert INTO ... not working - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SQLite - Insert INTO ... not working (/showthread.php?tid=537755)



SQLite - Insert INTO ... not working - Kyance - 17.09.2014

So, I've been struggling with this command/stock for quite a time now, and each time i try to fix it, it just still fails to create.

Stuff;
pawn Code:
enum e_FactionInfo
{
    fID, //factionID
    fName[60], //faction Name
    Locked, Active, //Lock Mode(invite), Active
    MaxMembers, Members, Leader, HasLeader, //Max-Members, current members and leader.
    Ranks[5]
}
//Onfilterscriptinit
db_query(Factions, "CREATE TABLE IF NOT EXISTS `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`)");
//creating faction
CMD:createfaction(playerid, params[]) {
    if(PlayerInfo[playerid][fID]) return SendClientMessage(playerid, -1, "{FC4949}You're already in a faction!");
    new factionname[60];
    if(sscanf(params, "s[60]", factionname)) return SendClientMessage(playerid, COLOR_NOTES, "You must enter a faction-name!");

    CreateFaction(playerid, factionname);
    return 1;
}
stock CreateFaction(ownerid, name[])
{
    new DBResult:result, Query[360];
    new DBResult:count = db_query(Factions, "SELECT * FROM `FACTIONS`");
    if(db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('%d', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", db_num_rows(count)+1, DB_Escape(name), DB_Escape(GetName(ownerid)));
    else if(!db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('1', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", DB_Escape(name), DB_Escape(GetName(ownerid)));
    result = db_query(Factions, Query);
    if(result) SendClientMessage(ownerid, -1, "{64CC66}Faction successfully created!"), SendClientMessage(ownerid, -1, "{64CC66}You can use '/settings' to edit the faction info!");
    else SendClientMessage(ownerid, -1, "{FC4949}Failed to create faction, contact server owner(s).");
    db_free_result(result);
}
What could be wrong here? D:


Re: SQLite - Insert INTO ... not working - SKAzini - 17.09.2014

Do you use PHPMyAdmin? If so, try to run the SQL code in the SQL tab thingy to see if the queries work.


Re: SQLite - Insert INTO ... not working - KayJ - 17.09.2014

Well, its hard for some people to understand. I think you have to see in this:
pawn Code:
stock CreateFaction(ownerid, name[])
{
    new DBResult:result, Query[360];
    new DBResult:count = db_query(Factions, "SELECT * FROM `FACTIONS`");
    if(db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('%d', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", db_num_rows(count)+1, DB_Escape(name), DB_Escape(GetName(ownerid)));
    else if(!db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('1', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", DB_Escape(name), DB_Escape(GetName(ownerid)));
    result = db_query(Factions, Query);
    if(result) SendClientMessage(ownerid, -1, "{64CC66}Faction successfully created!"), SendClientMessage(ownerid, -1, "{64CC66}You can use '/settings' to edit the faction info!");
    else SendClientMessage(ownerid, -1, "{FC4949}Failed to create faction, contact server owner(s).");
    db_free_result(result);
}



Re: SQLite - Insert INTO ... not working - Kyance - 17.09.2014

Quote:
Originally Posted by SKAzini
View Post
Do you use PHPMyAdmin? If so, try to run the SQL code in the SQL tab thingy to see if the queries work.
no, SQLite doesn't have PHPMyAdmin.. afaik..
I just use the default SQLite Browser :d

Quote:
Originally Posted by SturtIndia
View Post
Well, its hard for some people to understand. I think you have to see in this:
pawn Code:
stock CreateFaction(ownerid, name[])
{
    new DBResult:result, Query[360];
    new DBResult:count = db_query(Factions, "SELECT * FROM `FACTIONS`");
    if(db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('%d', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", db_num_rows(count)+1, DB_Escape(name), DB_Escape(GetName(ownerid)));
    else if(!db_num_rows(count)) format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`ID`, `NAME`, `LOCKED`, `ACTIVE`, `MAXMEMBERS`, `MEMBERS`, `LEADER`, `HASLEADER`, `RANK1`, `RANK2`, `RANK3`, `RANK4`, `RANK5`) VALUES ('1', '%s', '1', '1', '10', '1', '%s', '1', 'Trial Member', 'Member', 'Senior Member', 'Assistant', 'Leader')", DB_Escape(name), DB_Escape(GetName(ownerid)));
    result = db_query(Factions, Query);
    if(result) SendClientMessage(ownerid, -1, "{64CC66}Faction successfully created!"), SendClientMessage(ownerid, -1, "{64CC66}You can use '/settings' to edit the faction info!");
    else SendClientMessage(ownerid, -1, "{FC4949}Failed to create faction, contact server owner(s).");
    db_free_result(result);
}
Eh? I don't see any changes there :l


Re: SQLite - Insert INTO ... not working - Pottus - 17.09.2014

Make sure you use SQLitei you can enable error checking.


Re: SQLite - Insert INTO ... not working - Kyance - 17.09.2014

Quote:
Originally Posted by Pottus
View Post
Make sure you use SQLitei you can enable error checking.
I use the default SQLite..
Uh, does only SQLitei have error checking, if not, how can you enable it on the default one? ;d


Re: SQLite - Insert INTO ... not working - Jefff - 17.09.2014

pawn Code:
stock CreateFaction(ownerid, const name[])
{
    new Query[128];
    format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`NAME`, `LEADER`) VALUES ('%s', '%s')", DB_Escape(name), DB_Escape(GetName(ownerid)));
    db_query(Factions, Query);
    SendClientMessage(ownerid, -1, "{64CC66}Faction successfully created!");
    SendClientMessage(ownerid, -1, "{64CC66}You can use '/settings' to edit the faction info!");
}

pawn Code:
CMD:createfaction(playerid, params[])
{
    if(PlayerInfo[playerid][fID]) SendClientMessage(playerid, -1, "{FC4949}You're already in a faction!");
    else if(isnull(params)) SendClientMessage(playerid, COLOR_NOTES, "You must enter a faction-name!");
    else if(strlen(params) > 59) SendClientMessage(playerid, COLOR_NOTES, "Faction-name is too long!");
    else CreateFaction(playerid, params);
    return 1;
}
OnGameModeInit
pawn Code:
new str[525 char];
    str = !"CREATE TABLE IF NOT EXISTS `FACTIONS` (\
        `ID` INTEGER NOT NULL PRIMARY KEY,\
        `NAME` VARCHAR(64),\
        `LOCKED` SMALLINT(1) NOT NULL DEFAULT 1,\
        `ACTIVE` SMALLINT(1) NOT NULL DEFAULT 1,\
        `MAXMEMBERS` SMALLINT(2) NOT NULL DEFAULT 10,\
        `MEMBERS` SMALLINT(2) NOT NULL DEFAULT 1,"

    ;
    strcat(str,!"`LEADER` VARCHAR(25),\
        `HASLEADER` SMALLINT(1) NOT NULL DEFAULT 1,\
        `RANK1` VARCHAR(15) DEFAULT 'Trial Member',\
        `RANK2` VARCHAR(15) DEFAULT 'Member',\
        `RANK3` VARCHAR(15) DEFAULT 'Senior Member',\
        `RANK4` VARCHAR(15) DEFAULT 'Assistant',\
        `RANK5` VARCHAR(15) DEFAULT 'Leader')"
);

    db_query(Factions, str);



Re: SQLite - Insert INTO ... not working - Kyance - 17.09.2014

Quote:
Originally Posted by Jefff
View Post
pawn Code:
stock CreateFaction(ownerid, const name[])
{
    new Query[128];
    format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`NAME`, `LEADER`) VALUES ('%s', '%s')", DB_Escape(name), DB_Escape(GetName(ownerid)));
    db_query(Factions, Query);
    SendClientMessage(ownerid, -1, "{64CC66}Faction successfully created!");
    SendClientMessage(ownerid, -1, "{64CC66}You can use '/settings' to edit the faction info!");
}

pawn Code:
CMD:createfaction(playerid, params[])
{
    if(PlayerInfo[playerid][fID]) SendClientMessage(playerid, -1, "{FC4949}You're already in a faction!");
    else if(isnull(params)) SendClientMessage(playerid, COLOR_NOTES, "You must enter a faction-name!");
    else if(strlen(params) > 59) SendClientMessage(playerid, COLOR_NOTES, "Faction-name is too long!");
    else CreateFaction(playerid, params);
    return 1;
}
OnGameModeInit
pawn Code:
new str[525 char];
    str = !"CREATE TABLE IF NOT EXISTS `FACTIONS` (\
        `ID` INTEGER NOT NULL PRIMARY KEY,\
        `NAME` VARCHAR(64),\
        `LOCKED` SMALLINT(1) NOT NULL DEFAULT 1,\
        `ACTIVE` SMALLINT(1) NOT NULL DEFAULT 1,\
        `MAXMEMBERS` SMALLINT(2) NOT NULL DEFAULT 10,\
        `MEMBERS` SMALLINT(2) NOT NULL DEFAULT 1,"

    ;
    strcat(str,!"`LEADER` VARCHAR(25),\
        `HASLEADER` SMALLINT(1) NOT NULL DEFAULT 1,\
        `RANK1` VARCHAR(15) DEFAULT 'Trial Member',\
        `RANK2` VARCHAR(15) DEFAULT 'Member',\
        `RANK3` VARCHAR(15) DEFAULT 'Senior Member',\
        `RANK4` VARCHAR(15) DEFAULT 'Assistant',\
        `RANK5` VARCHAR(15) DEFAULT 'Leader')"
);

    db_query(Factions, str);
Due to the 'const name[]' i get
Code:
error 035: argument type mismatch (argument 1)
at line;
pawn Code:
format(Query, sizeof(Query), "INSERT INTO `FACTIONS` (`NAME`, `LEADER`) VALUES ('%s', '%s')", DB_Escape(name), DB_Escape(GetName(ownerid)));
Should I just remove the const?


Re: SQLite - Insert INTO ... not working - Jefff - 17.09.2014

Wops xd yes remove 'const'


Re: SQLite - Insert INTO ... not working - Kyance - 17.09.2014

Quote:
Originally Posted by Jefff
View Post
Wops xd yes remove 'const'
Well, It works now, but when I check /factions, It only shows my faction in the 'offline list', and the other data is '0'( Members: 0, Max members: 0 .. ) D:
EDIT: Just checked the database via SQLite browser,
'FactionName', 'LeaderName' had values, but everything else was blank/empty.