SkQuery MySQL question

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

dudle

Active Member
Jan 31, 2017
135
0
16
45
So I have this:
When I perform a command, it syncs the stats I have with the mysql database, but it ain't working. I have the code over here, what's the issue though?:

code_language.skript:
loop all players:
    update "UPDATE `Stats` SET `Stats`.`Deaths` = `Deaths`+%{deaths.%loop-player%}% WHERE `Deaths` = '%loop-player%'"

code_language.skript:
on death:
    attacker is player
    victim is player
    add 1 to {deaths.%victim%}
 
Okay well firstly, never use . in Column names, keep them one word.

At the top of your script, you'll want:

code_language.skript:
script options:
    $ init com.mysql.jdbc.Driver
    $ db url jdbc:mysql://host:port/Database?autoReconnect=true
    $ db username Username
    $ db password Password

You'll also need to make the table:

code_language.skript:
on load:
    update "CREATE TABLE IF NOT EXISTS Stats(uuid varchar(100), deaths int)"

Make sure to create a new row for each player who joins:

code_language.skript:
on first join:
    update "INSERT INTO `Stats` (`uuid`, `deaths`) VALUES ('%uuid of player%', '0')"

As you're syncing the stats by command, you might as well just set the field to the {deaths.%player%} variable (Would highly recommend using UUID's for this to make sure a player, if they change their name, still keeps their data: {deaths.%uuid of victim%})

code_language.skript:
on death: #Try using 'on death of player' if you wish
    if attacker is player:
        if victim is player:
            add 1 to {deaths.%uuid of victim%}

As for the command:

code_language.skript:
Command /syncstats:
    Trigger:
        Loop all players:
            $ thread
            update "UPDATE `Stats` SET `Deaths` = '%{deaths.%uuid of loop-player%}%' WHERE `uuid` = '%uuid of loop-player%'"
// Make sure that you have at least two columns in your table, uuid(VARCHAR(100)) and deaths(int)

This should do the trick, untested though
 
Last edited by a moderator:
It says this:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '[MYDBNAME].Stats' doesn't exist

edit: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(int))' at line 1
 
Your SQL Syntax is invalid.
You want to update "Deaths" declared as an integer, but in your WHERE Condition your asking a text instead of the integer. So SQL doesn't update because it does not find any data.
 
Your SQL Syntax is invalid.
You want to update "Deaths" declared as an integer, but in your WHERE Condition your asking a text instead of the integer. So SQL doesn't update because it does not find any data.
Not sure what you mean, it's setting the deaths column (int) where the uuid column(VARCHAR) is the loop-player's uuid

EDIT: Never mind, you were answering the original post. We've already sorted that issue.

It says this:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table '[MYDBNAME].Stats' doesn't exist

edit: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(int))' at line 1

Try this:

code_language.skript:
update "CREATE TABLE IF NOT EXISTS Stats(uuid varchar(100), deaths int)"
 
Status
Not open for further replies.