简体   繁体   中英

AttributeError: 'tuple' object has no attribute 'strftime' issue on postgres

Hi I'm trying to get a timestamp which is string stored in the time column on my postgres database table. However when trying to get this in my code it is returning the error

dateonlystring = stamp.strftime("%Y-%m-%w-%W %H:%M:%S") AttributeError: 'tuple' object has no attribute 'strftime'

I'm not sure why or how to covert the tuple to a string.

My code:

async def check_mute(self):
    guild = self.bot.get_guild(750744359650109661)
    for member in list(guild.members):
        member_role = discord.utils.get(member.guild.roles, name="Members")
        restricted_role = get(guild.roles, name="Restrict")
        conn = psycopg2.connect(DATABASE_URL, sslmode='require')
        cursor = conn.cursor()
        cursor.execute("SELECT time FROM blacklist WHERE username=%s", (member.id, ))
        stamp = cursor.fetchall()[0]
        if restricted_role in member.roles:
            if stamp:
                datestamp = datetime.now()
                datetimestring = str(datestamp.now().strftime("%Y-%m-%w-%W %H:%M:%S"))
                dateonlystring = stamp.strftime("%Y-%m-%w-%W %H:%M:%S")
                if (datetimestring > dateonlystring):
                    await member.add_roles(member_role)
                    await member.remove_roles(restricted_role)
                    print("Done.")

my table setup:

table

Help or some direction on where to go from here would be appreciated.

fetchall() returns an iterable of tuples, ie a list/tuple of tuples. Each outer iterable being a row in the DB, and each element in an inner tuple being a value of a column.

stamp = cursor.fetchall()[0]

stamp is a tuple (as the error suggests). To get the value of the time column you need to access stamp[0] , so

stamp[0].strftime("%Y-%m-%w-%W %H:%M:%S")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM