0
    ```
    dbcard = await self.bot.pg_con.fetchrow("SELECT array (SELECT card_name FROM cardimages)")
    dbcard = str(dbcard) 
    dbcard = dbcard[15:-2]
    dbcard = dbcard.replace("\'","")
    dbcard = dbcard.split(",")
    await ctx.send(dbcard)```

I'm relatively new to python / databases so I'm sorry if this is a really easy question to solve! What I'm trying to do is to convert the information from a table column to a python list. In the original dbcard, I end up getting a value that's like this: To get rid of it, I change it to string (apparently it isn't string yet?), remove the sides, erase the quotations marks, and then split it into a list. I'm sure there's another much much more effective way of solving this!

Thanks! Below is the database.

2
  • Not sure how someone can help you without knowing the format of the information you are getting from table :-) Commented Jan 9, 2020 at 10:22
  • I edited it to show an image. Does that help? Thanks for the quick reply! Commented Jan 9, 2020 at 10:25

1 Answer 1

1

Assuming this is asyncpg, fetchrow should yield a record. If you have a single value you still get a record, with a single value.

So just get the relevant value from the record...

Your query doesn't make much sense though, you're making postgres convert your query into an array then trying to mess with that array getting out of postgres.

You should just use a normal select, then use fetch which yields a list of records, and extract the relevant data from a list of records to a list of items, something along the lines of

names = [
    record.card_name
    for record in await self.bot.pg_con.fetch("select card_name from cardimages")
]

(code not tested so I may have the async bits wrong)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.