Python MySQL连接器未连接

时间:2018-09-02 03:21:04

标签: python mysql discord connector

我目前正在创建一些Discord机器人,并将一些数据存储在mysql中,这两个表位于同一数据库下。

使用来自MySQL连接器文档的相同示例(用于Python),cnx.is_connected返回false,并且我的“ for循环”未运行或错误,提示我未按正确的顺序使用它。

我与Discord bot和async的代码(自我的问题所在的idk起供参考):

import mysql.connector
import asyncio
import discord

client = discord.Client()
cnx = mysql.connector.connect(user='user', password=’password’',
                             host='IP',
                             database='creative role-play')
cursor = cnx.cursor()

@client.event
async def on_message(message):
   if message.author == client.user:
       return

   SteamID = 0
   messagelower = message.content.lower()
   if messagelower.startswith("!verify"):
       steamidnum = int(message.content[message.content.index(" ") + 1:])
       query = ("SELECT steamid64, SteamID, CharName, TotalPlayTime FROM playerinfo, member_list "
               "WHERE steamid64 AND SteamID = %d") % (steamidnum)

       cursor.execute(query)
       print (cursor)
       if cnx.is_connected():
           print('sucessfull...Connected to MySQL database')

       for (steamid64, SteamID, CharName, TotalPlayTime) in cursor:
           print("{}, {}, {}".format(SteamID, CharName, TotalPlayTime))
           CharName = str(CharName)
           TotalPlayTime = int(TotalPlayTime)
           print (SteamID, "", steamid64)
           print ("confirm 1")
           if steamid64 == SteamID:
               ifrole = "has already been entered, please contact <@232233468094840832> if this is you"
               print ("confirm2")

           else:
               TotalPlayTime = int(TotalPlayTime)
               print (TotalPlayTime)
               if TotalPlayTime >= 86400:
                   ifrole = "has become a Member of Modern Role-Play"
                   role = discord.utils.get(message.server.roles, name="Member")
                   await client.add_roles(message.author, role)

                   add = ("INSERT INTO member_list "
                            "(steamid64, discordname, charactername)"
                            "VALUES (%(steamid64)s, %(discordname)s, %(charactername)s)")
                   values = {"steamid64": SteamID,
                       "discordname": message.author.name,
                       "charactername": CharName,}
                   cursor.execute(add, values)
                   cnx.commit()
               else:
                   ifrole = "does not meet the time requirement of 24 hours to become a Member"

           msg = "Player %s has a Playtime of %d hour's and %s! (%s)".format(message) % (CharName, TotalPlayTime / 3600, ifrole, steamidnum)
           await client.send_message(message.channel, msg)



@client.event
async def on_ready():
   print('Logged in as')
   print(client.user.name)
   print(client.user.id)
   print('--Now Online--')

client.run('Token')

抱歉,如果您不希望在其中找到Discord.py的内容。

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

 password=’password’',

您有一个额外的引号(如果迟到则加引号)