#AFK Command with mysql

1 messages · Page 1 of 1 (latest)

robust flower
#

that code formatting is confusing af

#

full code often helps more than just a section

elfin heron
#

don't use f strings with sql

#

use your library's parser

robust flower
elfin heron
robust flower
#

yes ok so don't use it so you don't get used to it.

elfin heron
#

yep

#

and who knows if they're doing it that way in other parts of their code

elfin heron
#

Are you getting an error? Or just the bot doesn't send any message?

robust flower
#

I get that but often one needs the whole code to understand why one section isn't working.
Lets say this line throws an error in my program: x = 5/y
With just this the error can't be found. But with the context like y = 0 it is more clear what the problem is. Do you know what I mean?
Because I couldn't find any error in this section, I was asking for your full code as this often gives a deeper insight into what is going on 🙂

#

Also: Please don't make pictures of your code but paste it as a code block or use pastebin for longer sections. Makes it easier for troubleshooting.

robust flower
#

Do you get any errors?

#

any other console output?

#

well there you have your problem

#
await cursor.execute(f"SELECT userID FROM afk WHERE guildID = {message.guild.id}")
result = await cursor.fetchall()

somewhere there is the problem. Probably the sql query

#

Check if the query returns anything

#

Ich meine du sollst checken, ob der sql command überhaupt irgendwas zurück gibt.

robust flower
#

na z.B. mal direkt auf deinem Datenbankserver ausführen oder mal ein kleines skript schreiben mit genau dem Befehl

#

@still rain

#

welche sql python library nutzt du denn?

robust flower
#

Na ein einfaches python skript, was sich mit deinem sql server verbindet, diesen befehl sendet und das Ergebnis ausspuckt...
Ich hoffe du weißt wie das geht.

#

Und wie hast du dann das ganze in deinem code vom bot gemacht?

#

gib mir nen moment

#

weißt du wie du ein python skript ausführst?

#

war kurz weg

#

bin gerade dabei

#

pip install mysql-connector-python==8.0.29

#
import mysql.connector as MySQLdb

guild_id = ""  # Die ID vom Discord Server
hostname = ""  # Der Hostname des Servers (z.B: sql.domain.de oder eine ip)
user = ""  # Der Benutzername für die Datenbank
password = ""  # Das Passwort
database = ""  # Der Name der Datenbank

conn: MySQLdb.MySQLConnection = MySQLdb.connect(
    host=hostname, user=user, password=password, database=database
)
cursor: MySQLdb.connection.CursorBase = conn.cursor(dictionary=True)
cursor.execute("SELECT userID FROM afk WHERE guildID = %s", [guild_id])
result = cursor.fetchall()
print(result)
robust flower
robust flower
#

und natürlich die ganzen variablen oben angeben

#

du musst nur die "" ausfüllen hinter denen ein commentar steht

#

und die guild id

#

dann einfach ausführen und schreiben was in der konsole steht

#

also was geprintet wird

#

bin mal afk für 20 min

robust flower
#

gabs nen error?

#

wie hast du's ausgeführt?