#databases

1 messages · Page 117 of 1

brazen charm
#

It just means it's the main indexible key and will always be unique

eager robin
#

is pandas good for fetching stuff from the db and return it to discord?

brazen charm
#

No becaus3 pandas is a in memory dataframe library not a dB driver

#

You're best bet is postgre sql and asyncpg

eager robin
#

ree

#

i use sqlite3

vernal pulsar
#

A recommendations for self hosted database with ready-to-use REST API?

velvet coyote
#

How many tables can u have in a database?

#

is there a limit?

shrewd geyser
#

Sorry if this is dumb but, I was working around with sqlite and kinda spammed rows that are just ? and it's not letting me delete them. Can anyone help delete them?

proven arrow
#

@velvet coyote as far as I'm aware there is no limit, (if there even is a limit its probably in the many millions or more). You will more likely be limited by your hardware than the database itself. Also if you are trying to store so many amount of tables (where you are having to consider the limit) you may want to reconsider your database design, unless you actually have a need to store so many tables.

velvet coyote
#

right right

shrewd geyser
#

How would I delete every row in sqlite

brazen charm
#

just drop the table would probably be quicker

#

otherwise just DELETE FROM table

shrewd geyser
#

How would I bundle an if statement into it, like I want ```py
if name in example.db:
t = (name,)
c.execute("DELETE FROM citizens WHERE name=?", t)

trim knoll
#

Hey guys so i am using postgres and i am working with arrays. I have a list in python and i want to subtract each element in that list from an array in my database.
So basically opposite of existing array_cat function. How would i be able to achieve this?

#

please @ me

late lake
#

@proven arrow How do I store a Python List into a Sqlite3 database?

proven arrow
#

@late lake You can't, since sqlite doesn't support it

late lake
#

I'm making a seperate table with only the values I need to store

#

And for each row I will store a value?

proven arrow
#

However you can use the json module to serialize it and store it as a string.

#

And then when you want to read it back deserialize it to turn back into a list

late lake
#

Can I use the method I said above?

quartz moon
#

why is this giving me an error?
@late lake I'm not sure but try to put text in lower case

#

And see if it works

late lake
#

Im using TEXT in my other table

#

oh I need to delete 2nd cursor lol I forgot to remove it (not causing the error btw)

proven arrow
#

Probably because table is a keyword

late lake
#

Ooh

#

Thats a good solution lol

placid flicker
#

What is the correct way to access my database models from another file?

late lake
#

Fixed 🙂

#

Thx

proven arrow
#

Btw although you can store the list by serialising as I said, you should consider reading this which another user sent few days ago, on why you should try to not. #databases message @late lake

#

@placid flicker are you referring to when using an ORM?

late lake
#

I'm just gonna go with my own solution lol

placid flicker
#

@proven arrow Yeah let me post an example of what I mean

late lake
#

I think storing in tables will help me learn databases more lol

#

@proven arrow how do I INSERT INTO a column into multiple values into seperate rows?

placid flicker
#

I'm using Flask-SQLAlchemy btw. So in my app.py I have the following model. class Store(db.Model): store_name = db.Column(db.String(100), primary_key=True) date_added = db.Column(db.DateTime, default=datetime.utcnow)

proven arrow
#

Ok in that case if it SQL alchemy then I'm not so sure what is the best way. Since I never have used it.

placid flicker
#

I want to query this table from another file called app2.py and I import it this way from app import Store

#

I'm trying to figure out if this is the best way

proven arrow
#

But that should be fine

placid flicker
#

Because this works fine on my local machine

#

but when I try to deploy it using Gunicorn on a remote server

#

this import causes an error and stops Gunicorn from running

nocturne basin
#

How do some of you work with SQL/Python on the fly? I'm using jupyter lab, wondering if there are some other better options out there?

placid flicker
#

@nocturne basin SQLAlchemy makes life pretty simple

shrewd geyser
#

Trying to make a check that makes sure that a name isn't already in the database but it only works the first time
Code:

c = (var, )
    c.execute('SELECT * FROM citizens WHERE citzen_num=?', c)
    cit_check = c.fetchone()

    while cit_check is not None:
        var = random.randrange(10000, 99999)
```Same thing just name instead of citzen_num
#

Every other time it still makes duplicates

proven arrow
#

I see well it might be a python thing. I only have experience with orms in php/java, and I would have loaded them in a similar manner. You may want to wait for someone who knows your issue.

odd dune
placid flicker
#

@proven arrow Thank you for the help still!

late lake
nocturne basin
#

@placid flicker , was mostly referring to IDE's . I do use SQL alchemy however 🙂

#

I like using Jupyter Notebook because I always see myself referring to other query's I created. But the jupyter lab enviroment get's a bit boring to look at.

#

Wondering about the softwares others use that helps them implement SQL using python!

shrewd geyser
#

@late lake all in one ()

#

Also try executemany

proven arrow
#

@late lake your missing the comma after each value

#

VALUES (..), (..), (..)

torn sphinx
#

Hi,
I have pymongo BulkWriteError.
I want someone with experience in this field to help me.

late lake
#

Ohhh

torn sphinx
#

I used insert_many func in pymongo.

late lake
#

Got this

#

Idk how executemany works lol im nub at databases so far

torn sphinx
#

Hi,
I have pymongo BulkWriteError.
I want someone with experience in this field to help me.
please help me.😫

late lake
#

I hope you get help 😦

#

Good Luck 🙂

torn sphinx
#

@late lake please help me

crisp ledge
#

Which module to use to change time in the mongo database
or make a loop?

late lake
#

@late lake please help me
@torn sphinx What do you need help with?

#

Idk PyMongo

#

I barely know Sqlite3

late lake
#

@proven arrow How do I update Sqlite3 table column updating each row with new list values?

#

Someone pls help me with this ^

spring panther
#

im moving from sqlite to Postgresql and having a hard time find good python docs for things. I have a column that stores an array and i want to be able to append to that array. Whats the correct syntax to do so? curs.execute(f"UPDATE ARRAY_APPEND(array_agg(TEXT 'new number'), 'python') WHERE 'id' = 'testing'")

quartz moon
#

Hello I want to substitute the remote mongodb connection for localhost connection, but whenever I try to do that I get the following error: https://hastebin.com/ejafoyicav.sql. Any help is very welcome ty in advance. Btw client.mongo = "mongodb://localhost:27017/"

spring panther
#

@quartz moon AttributeError: 'Bot' object has no attribute 'aircraft'

#

not sure if that has anything to do with your db

#

also, i've never used SQLAlchemy. Is that make things a lot easier for Postgres db manipulation?

glad sleet
#

is there a mongodb/pymongo server?

quartz moon
#

I have mongodb installed on my laptop

#

I set up a cluster remotely in mongodb atlas, but now I want to change database and have it locally

#

Hello I want to substitute the remote mongodb connection for localhost connection, but whenever I try to do that I get the following error: https://hastebin.com/ejafoyicav.sql. Any help is very welcome ty in advance. Btw client.mongo = "mongodb://localhost:27017/"
@quartz moon but whenever I change the URI to "mongodb://localhost:27017/" the following error pops up

glad sleet
#
            if result_two is not None:
                if result_two["toggle"] == "on":
                    try:
                        await member.send(f"You have been banned from **{ctx.guild.name} for: `{reason}` ")
                    except:
                        pass
                else:
                    pass
            else:
                pass```

guild:767932158865047552
name:"dmonban"
toggle:"on"```

#

the message isnt sending even though toggle is "on"

#
result_two = self.config.find_one({"guild": ctx.guild.id, "name": "dmonban"})```
pseudo cove
#

bruh

#

remove the try except

#

and all the else passes

#

@glad sleet

glad sleet
#

thats not the problem

#

but i fixed it

#

thank you though

late lake
#

Can someone help me with this ?

#

In sqlite3

proven arrow
torn sphinx
#

is there any golden rule when using transactions?

proven arrow
#

What kind of golden rule are you expecting?

#

When using transactions make them quick, make them count?

chrome fjord
#

Hello,
I'm looking for the most efficient way to run a query (but I cannot benchmark my queries because of access limitations).
I've a table "resources" with

id name manager_id

I need to fetch all the resources which are managers. I thought of :

SELECT
  name
FROM
  resources
WHERE
  id IN (SELECT manager_id FROM resources) -- But I hate subqueries

And

SELECT
  DISTINCT(mana.name)
FROM
  resources mana
  JOIN resources res
  ON res.manager_id = mana.id
torn sphinx
#

What kind of golden rule are you expecting?
When using transactions make them quick, make them count?
@proven arrow umm idk just general, maybe this is ok thanks

#

@chrome fjord use join which i am sure is more efficient i think anyways

chrome fjord
#

I actually was wondering because of the DISTINCT (and the need for a GROUP BY which I didnot put there for simplicity)

chrome fjord
#

Ok, I found an env I could benchmark. Results are similar, it's early useless over-optimization XD

torn sphinx
#

how should I store bcrypt hashed passwords in sqlalchemy? i tried strings, binaries, but on pythonanywhere.com it keeps throwing weird errors at me. seems like they misconfigured bcrypt or something. even the versions are the exact same as with local

#
import os

import bcrypt
from sqlalchemy import create_engine, Column, Integer, Binary
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


h = bcrypt.hashpw( 'hotmail'.encode('utf-8'), bcrypt.gensalt())

print("BCrypt test: ", bcrypt.checkpw('hotmail'.encode('utf-8'), h))


db_engine = create_engine('sqlite:///test.db', connect_args={'check_same_thread': False})
Session = sessionmaker(bind=db_engine)
db_session = Session()
EntityBase = declarative_base()


class User(EntityBase):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True, autoincrement=True)
    password = Column(Binary(60))

EntityBase.metadata.create_all(db_engine)


user = User()
user.password = h

db_session.add(user)
db_session.commit()

print(user.password)
print("DB PW Test 1: ", bcrypt.checkpw('hotmail'.encode('utf-8'), user.password))

db_session.close()


db_session = Session()

newuser: User = db_session.query(User).all()[0]

print(newuser.password)
print("DB PW Test 2: ", bcrypt.checkpw('hotmail'.encode('utf-8'), newuser.password))
db_session.close()

os.unlink('test.db')

this script succeds on my local, but not on pythonanywhere

torn sphinx
#

python 3.7

glass gorge
#

so if i have a flask app, and im loading a csv file located in an instance folder

#

do i still use load data local infile for mysql query?

#

im using docker

#

as well

#

but mostly my question is about load data infile

uneven lava
#

can I create a column to a table in the database using sqlalchemy ?

lime echo
#

When I do:
heroku pg:pull... the command logins as a user ayoub for my psql database and I want to login as posgtres.
I tried PGUSER=postgresbut it says that PGUSERis not a known command.
I wanted to know if I could just change the default user so that it logins as postgres by default?

glad drift
#

can someone help me maybe understand how to use flask a bit better?

I have a database with quite a few tables, and I want to create an api where the user can query by going /dbname/tablename/<args for specific data> to retrieve a json of all the table data (although probably default it to first 50 with options). every flask tutorial or guide ive looked at is for having a database with 1 table of data. what should i google to achieve what I want?

#

sorry this might be the wrong channel

crisp ledge
#

Counting time in mongo through loop or module?
and which module

torn sphinx
#

hi

real quarry
#

hey i need help

#

idk if anyone is here or not

burnt basin
#

hello all i'm currently a nba project in Django but i m lacking some knowledge on django model database. can you please share some feedback on my model please? thank you

bronze escarp
#

Hello, I try to use prepared statements in postrgres using asyncpg and I have this function:

    async def test(request, user_name):
        dp_pool = request.app["db_pool"]
        async with dp_pool.acquire() as connection:
            async with connection.transaction():
                stmt = await connection.prepare('''SELECT * FROM users WHERE name = $1''', user_name)

                print(stmt.get_query())

but I have an error --> prepare() takes 2 positional arguments but 3 were given

I don't understand why it were given 3 arguments if I passed 2? How can I fix this ?

teal fable
#

how use sql

#

what is sql

hoary sonnet
#

hi

pseudo cove
#

@bronze escarp prepare(query, *, timeout=None)

#

it only takes the query

#

as an arg

#

its supposed to be an optimization for when you're running the same query multiple times in a row

#

meaning you have to pass in new args each time you want to run it

#
>>> import asyncpg, asyncio
>>> loop = asyncio.get_event_loop()
>>> async def run():
...     conn = await asyncpg.connect()
...     stmt = await conn.prepare('''SELECT 2 ^ $1''')
...     print(await stmt.fetchval(10))
...     print(await stmt.fetchval(20))
...
>>> loop.run_until_complete(run())
1024.0
1048576.0
#

if you want to pass args in on only 1 call

#

just use conn.execute()

bronze escarp
#

just use conn.execute()
@pseudo cove yes I know that I can use this, I've already made with execute

#

but I wanna use prepared statements

pseudo cove
#

see example

lime echo
#

My discordbot can connect to my heroku database but it cannot find the tables, how can I fix this?

#

psycopg2.errors.UndefinedTable: relation does not exist

proven arrow
#

Does that table actually exist?

quartz moon
#

Hello I have a class in charge of the most common CRUD operations in monogdb and the following method triggers a find_by_id(id) query to the db.

lime echo
#

@proven arrow apparently not.
I did heroku pg:push database.dump DATABASE --app mybotapp btw.

quartz moon
#

And this is the output I have, it looks for the object but then when I print the object it does't appear

proven arrow
#

@lime echo It would be better if you check heroku first by logging in to your db from it to see if your tables are there, and thereafter proceed with trying to access it in python. Also note sometimes people make the mistake of when creating tables they double quote the table name, which if you did do then you would have to double quote it also when querying it.

#

For ex,

CREATE TABLE "A" ...
"SELECT * FROM A" # this would error saying relation does not exist
lime echo
#

I converted my sqlite3 database to a pg one, then dumped it then pushed it to Heroku.

#

Yeah I know that. I think that I didn't make that mistake.

#

@proven arrow

proven arrow
#

Have you login to your database from heroku and verify if those tables are actually there? To see if the import was succesful?

lime echo
#

Not yet

#

I will do so when im on my PC.

#

Btw, I tried a little but I found it a bit complicated

#

Like, how to use pgAdmin with Heroku seems a bit complicated.

scarlet cove
#

How to fix this issue
Help me

#

Any one

proven arrow
#

@scarlet cove cursor is a function, so you are missing the () after it

#

change it to my_db.cursor()

scarlet cove
#

Okk

#

I correct it

#

Then run

#

@proven arrow thanks bro works

quartz moon
#

Hello I'm struggling to substitude a remote mongodb connection (mongodb+srv) for localhost connection I have the following error. Any help is very welcome. Btw client.conexion = "mongodb://localhost:27017/"

glass gorge
#

so im using docker and mysql

#

i have a file that i load to a specific folder

#

but im getting a pymysql.err.OperationalError: (1290, 'The MySQL server is running with the --secure-file-priv option so it cannot execute this statement')

#

i cant move it to that file location

torn sphinx
#

Im new how do i use sql?

#

hello?

shell ocean
#

say I have the following data:

left_id | right_id
1       | 1
1       | 2
1       | 3
2       | 1 
2       | 2
2       | 3
3       | 4

how can I get to this:

left_id | right_id_count
1       | 3
2       | 0
3       | 1

(basically groupby count except each unique value of right_id is only counted once for the value of left_id it first appears with)

glass gorge
#

im trying to update a user file in mysql,specifically i am loading a csv file saved in a folder, it's in a docker container. mysql is giving me this error
pymysql.err.OperationalError: (1290, 'The MySQL server is running with the --secure-file-priv option so it cannot execute this statement')
I've googled the solution which suggested I somehow place my file in the location of the result of a show variables like "secure_file_priv; and im getting a

+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv | null  |
+------------------+-------+```
 I am no longer sure what to do
the other option was to change the config file of mysql
and restart the db
proven walrus
#

oke i try that

#

oke

#

wait

#

import wont show as purple in new file

#

how make it do that

#

nvm

proven walrus
#
#Import x (Imports)
import discord
import sqlite3
import aiosqlite
import asyncio
import random

#From x Import x (Imports)
from discord.ext import commands , tasks
from discord.utils import get

client = commands.Bot(command_prefix = "am.")
client = discord.Client()


@client.event
async def on_ready():
    db = sqlite3.connect('main.sqlite')
    cursor = db.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT ALREADY EXISTS main(
        guild_id TEXT,
        msg TEXT,
        channel_id TEXT,

        )
        ''')
        

Help it will not create a database file called main for me?!

#

@torn sphinx

#

wot

#

how

#

do it then

#

oke

#

i did

#

and ran the file

#

ez

#

wot is wrong

#

wot do you mean commit

#

ohhh

#

i was on tutorial

#

lmao i just need help storing info for a user specified channel

#

il

#

but how make the data base

#

i dont wanna learn another api lmao @torn sphinx

proven walrus
#

i mean idc where the data goes to it's just used for a user specifying a channel @torn sphinx

proven walrus
#

@torn sphinx dms

torn sphinx
#

Is exploiting database a break of ToS ?

earnest parcel
#

What does that even mean?

mellow vector
#

hello does anyone know about Matplotlib in python?

broken night
#

Hi Guys,

I have build a webcrawler and like to know how to handle the data.

I whould like 1 index of all websites.

And then i have 3 types of users:
User 1: Can crawl 25 pages
User 2: Can crawl 100 pages
User 3: Can crawl 5000 pages

On each crawl i update or insert new links.

My question is how do i handle old links that dosent exsist anymore?

flint zenith
#
mydb=sqlite3.connect('main.sqlite')
cursor=mydb.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS main (id TEXT, prefix TEXT, xp INT, level INT, donated INT)') 
if os.path.isfile('output.csv'):
    pandas.read_csv('output.csv').to_sql('main', mydb, if_exists='append',index=False)
mydb.commit()```
I get two rows in the database for each user everytime i reload...
plain pebble
#

how to sort documents with a specific key in mongoDB?

lime echo
#

@proven arrow I can finally see my Heroku database in PGAdmin and they are empty lol.
I tried to do pg:push, it said that the push was completed but still the database is still empty.

velvet coyote
#

Can u have subtabels

#

so if I have a table called subjects
and inside that I have math and science, can I have algebra inside maths and physics inside science

plucky robin
#

I have a huge table with over 100 million rows which I want to archive old entries from. I'm trying just to gauge how many of these old entries I have using:
SELECT * FROM my_table WHERE my_date < '2008-11-05';
where my_date is an indexed column with the DATETIME type. I've also tried DATE('2008-11-05') and CAST('2008-11-05' AS DATE).

The SELECT alone is taking over an hour to complete even though it's only returning thousands of rows.

Is there some way to speed this up? A friend tried in one of his company's test databases with ~15 million rows and it finishes in seconds, I think he said they don't even have it indexed.

proven arrow
#

@velvet coyote You can have relations between tables. The tables would be linked with a foreign key. And you can query them through joins.

#

You would have a One-Many relationship in yours, where each subject can have many modules.

lost mortar
#

Does anybody here have much experience with SQLAlchemy and SQL Server. I have a script that is trying to write 100 rows made of 5 columns of NVARCHARS at a time, and it is literally taking minutes for this write to occur. I was originally trying over 1000 but the wait was entirely too long.

#
                if count%100 == 0:

                    retry_flag = True
                    retry_count = 0

                    while retry_flag and retry_count < 5:
                        try:
                            print("attempting bulk save now")
                            session.bulk_save_objects(entries)
                            session.commit()
                            entries = []
                            retry_flag = False
                            print("bulk save successful")```
velvet coyote
#

when I do \l in psql I have this database, now how would I actually connect and insert stuff into this database?

#

oh i got it

gray sable
#

Hi I need some help with pymongo

#

anyone with some experience could you DM me?

dusky iris
#

guys im new to mysql-python connector, just learning in my school

#

can someone welp me

#

Can't connect to MySQL server on 'localhost' (61)

#

im getting this when i try to make a db

#

rather can someone help me setup the connector?

#

since i have anaconda i've installed it through the command "conda install -c anaconda mysql-connector-python"

proven arrow
#

@dusky iris mysql-connector-python is just a library that allows you to communicate/interact with your database through python. Your error is it cant connect to the server. So make sure your MySQL server is running on your local computer, and the connection details you provide are correct.

cedar needle
#

Hello, i need to compare string with all strings from row

#

How i can do it?

torn sphinx
#
@bot.event
async def on_guild_join(guild):
    await bot.db.execute("INSERT INTO prefix WHERE guild_id = $1 AND guild_prefix = $2", guild.id, "/")
    await bot.db.execute("INSERT INTO prefix WHERE guild_id = $1 AND guild_prefix = $2", guild.id, "/")
AttributeError: 'coroutine' object has no attribute 'execute'
wintry stream
#

@torn sphinx i assume your database requires a cursor object, also in this case i recommend against using your bot instance as a carrier for your database variable

torn sphinx
#

Huh

#

Can u give example how to do as I am new to dB

proven arrow
#

@torn sphinx Your function is async so you need to await it, hence why you are getting just the coroutine object error.

torn sphinx
#

I am awaiting it

proven arrow
#

No your not, you just have bot.db = start_db()

#

You have defined start_db as an async func also

#

Even if you did await it that would give you an error since you cant await outside an async function

torn sphinx
#

So what do I do?

proven arrow
#

You can do it like this.

async def start_db(bot):
    bot.db = await asyncpg.create_pool(connection_details)

bot.loop.create_task(start_db(bot))
#

@torn sphinx

torn sphinx
#

Alr

#

I'll try it Tom

proven arrow
#

You can do the same with asyncio run_until_complete

sacred tartan
#

hi guys anyone willing to help

#

Briefly select two transaction handling criteria that they may want to evaluate in selecting the appropriate RDBMS, and provide a comparison of how Oracle and PostgreSQL support these features

i found some answers but not sure, the first one is ACID
A- Atomicity
C - Consistency
I - Isolation
D - Durability

the second answer is from my notes
Backup, checkpoint and recovery facilities
–Database log facilities
–Concurrency methods and granularity
–Deadlock resolution strategies
–Advanced transaction management
–Parallel/distributed query processing

sacred tartan
#

this is the second question 👇
The purpose of Data Modelling can be described as: ‘to assist in the understanding of the meaning (semantics) of the data and to facilitate communication about the information requirements’. Discuss the key points of this statement

crystal aspen
#

what should i use if i want to execute multiple SQL statements with one call in postgresql like there is executescript in sql lite 3

swift fossil
#

For web dev is it necessary to learn PHP?

proven arrow
#

@crystal aspen You would have to use seperate db calls.

#

@swift fossil You should ask in #web-development but the answer is no. PHP is just a one of the many language for web programming.

brazen charm
#

@swift fossil jesus no

torn sphinx
#

What modules are suggested with async support that can access and modify MariaDB databases?

#

Please tag me upon reply.

tardy fulcrum
#

Hi guys, in mysql, can we use LIKE statement in ON clause when joining tables?

quaint yarrow
#

I believe it'll technically work? Though the best way is to just try it 🙂 Though from a performance perspective it probably isn't great to do that and you may end up with some weird results as well, depending on your data.

#

mysqls docs don't indicate any restrictions on the search condition, syntactically anyway.

#

@tardy fulcrum ☝️

torn sphinx
swift fossil
#

oh I see

torn sphinx
#

?

quaint yarrow
#

@torn sphinx You're getting a socket timeout connecting to your database. Check your connection config and make sure it's picking up the right values and that it's possible to correctly connect to the database with those (with like, psql for instance).

torn sphinx
#
sql_info = {"database": "FlanticDB",
            "user": "postgres",
            "password": "my_pass",
            "host": "my_ip"}
#

@quaint yarrow its like this

quaint yarrow
#

@torn sphinx is your postgres listening on port 5432?

torn sphinx
#

how do i check?

quaint yarrow
#

also is my_ip in this case local or remote? If it's remote, is it possible a firewall is preventing the connection?

torn sphinx
#

local as of now will soon update it to vps ip

quaint yarrow
#

And you're on Windows?

torn sphinx
#

yup

quaint yarrow
#

run netstat -an in a cmd prompt. and see if there's a listing for something like 0.0.0.0:5432

#

You may need to run cmd as administrator for that to work.

torn sphinx
#

it send an list of active connections

#

TCP 0.0.0.0:5432 0.0.0.0:0 LISTENING

#

its there

quaint yarrow
#

What happens if you run psql -h my_ip -p 5432

torn sphinx
#

@quaint yarrow

quaint yarrow
#

ah, looks like the windows installer doesn't include psql. Sorry haven't used postgres on windows before hmm.

#

looks like it has pgAdmin.

#

Can you connect with pgAdmin? (not sure how to do that)

torn sphinx
#

yup

#

i can connect with pgAdmin

quaint yarrow
#

🤔

#

@torn sphinx how long does it run before it throws that error?

torn sphinx
#

the bot?

quaint yarrow
#

Yea

torn sphinx
#

it dont even start

#

give throw error

#

in the starting

quaint yarrow
#

It just throws that error instantly?

#

or does it take a few seconds

torn sphinx
#

stuck for 10-15 sec then throw error

quaint yarrow
#

ok

#

@torn sphinx is your host configuration just localhost?

torn sphinx
#

idk lol how do i check

quaint yarrow
#

what i mean is, is the value of "host" in your sql_info dict "localhost"

torn sphinx
#

It's my ip address

#

My pc ip address

quaint yarrow
#

Try using "localhost" instead.

torn sphinx
#

asyncpg.exceptions.InvalidCatalogNameError: database "FlanticDB" does not exist

#

there is a FlanticDB

quaint yarrow
#

Well, it's progress, it connected this time. lol

torn sphinx
#

yh lol

#

so when im on my vps then i use my vps ip right

quaint yarrow
#

Depends. Is the database on the same VPS?

torn sphinx
#

i mean currently im learning db so i am not doing on vps rn just asking

quaint yarrow
#

ah ok. well it depends. if it's on the same VPS, then you can continue just using localhost. If they are on separate VPSs, then you would use the IP of the VPS that the database is on. Though I don't recommend putting them on separate VPS.

torn sphinx
#

hmm

#

also

asyncpg.exceptions.InvalidCatalogNameError: database "FlanticDB" does not exist

quaint yarrow
#

yea. so. makes me wonder if the user the bot is connecting as has permission for that db

torn sphinx
#

oof there are permission to connect to db

#

but i am local hosting so i dont think so it would block it

quaint yarrow
#

well the user permission thing doesn't really go away necessarily just because it's local. I think by default, a database is owned by the user which created it. How did you create that DB?

torn sphinx
#

via pgAdmin

quaint yarrow
#

Did you log in to pgAdmin with the same credentials that are in your sql_info dict?

torn sphinx
#

yup

#

it justs ask for pass when login in

quaint yarrow
#

oh

#

weird.

#

You may need to lowercase the db name

torn sphinx
#

on my on DB or in the config file

#

i changed both to flantic still not workin

#
sql_info = {"database": "flantic",
            "user": "postgres",
lime echo
#

I am using heroku pg:push to push my local database to Heroku.
I get the following error:

throw er; //unhandled error event```
it fails at `processing data for "public.ads"`
What's the best way to fix this?
inland maple
#

can anyone help me with sql querys

wooden crown
#

hello! Guys i am having trouble using the mysql connector in python It shows that the module is uninstalled even though its i installed it...

proven arrow
#

It's import mysql.connector

wooden crown
#

still the same error

#

its not even showing in the recommeded syntax

proven arrow
#

Try importing it inside just a python interpreter

wooden crown
#

how do i do that in vs code if u can help...

proven arrow
#

Not sure about vs code, but if you search in your installed programs for python it should come up and open in a command prompt

wooden crown
proven arrow
#

No I meant in a python shell, so it'll look like the command prompt except you can run python code in it

wooden crown
#

ohh i see

#

wait

#

nah

proven arrow
#

So try installing it again from a Windows command prompt, like this
python3 -m pip install mysql-connector-python

wooden crown
#

i did this already

#

couple 100 times

#

used all the variations of the pip install

proven arrow
#

It's possible you installed it to another location like an venv

wooden crown
#

I did it in inbuilt vscode terminal

#

I'll try in cmd as well

#

It also shows already installed

proven arrow
#

Show the output

wooden crown
#

wait

#

i tried uninstalling it and installing it also

#

but still

#

It's the same

#

It must be something silly i usually do that

torn sphinx
brazen charm
#

You sure you're not connected to the postgres 12 server

torn sphinx
#

how do i check

#

i deleted the postgres 12 server still same thing happening

brazen charm
#

In the query tool

#

If you do SHOW DATABASES

#

Wrong thing

torn sphinx
#

wdym?

#

SHOW DATABASES

brazen charm
#

SELECT datname FROM pg_database;

#

Show databases is mysql

torn sphinx
#

im so confused like what i do?

brazen charm
#

SELECT datname FROM pg_database;
@brazen charm try run that in the query tool in pg admin

torn sphinx
#

@brazen charm

forest hinge
#

guys?

#

need your answer on a question

#

I have a script that if I run manually, works completely fine but when I run it via cron it throws an error on the db connection builtins.AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

#

could be a pathing issue when running via cron?

#

the db connection uri is in the settings file of scrapy

brazen charm
#

@torn sphinx hmmm

#

I'd you tell asyncpg to just connect to the postgres database

#

And run the same statement form asyncpg

#

What does it return

torn sphinx
#

i mean the error comes when i start the bot

brazen charm
#

Yeah cuz it can't find the db

#

Make a new file and test it with asyncpg

lime echo
#

@brazen charm I am using heroku pg:push to push my local database to Heroku.
I get the following error:

throw er; //unhandled error event```
What's the best way to fix this?
can you help me? 😭
torn sphinx
#

new file?

brazen charm
#

I have no idea I don't use heroku

#

@torn sphinx make a new file, setup. A basic asyncpg system

lime echo
#

ooo ok!

brazen charm
#

Connect to postgres database not your target db

#

List dbs

torn sphinx
#

what do i pass in database?

#

in config file

brazen charm
#

postgres

torn sphinx
brazen charm
#

Yes

#

Now execute the sql I gave you earlier

torn sphinx
#

SELECT datname FROM pg_database;
?

#

how do i run it in code i was running in pgadmin

brazen charm
#

Do you know how to execute sql in asyncpg?

torn sphinx
#

i am new to DB thats why i am doing just basic stuff

#

btw

#

we do like

db.execute(query)```
#
bot.db = await asyncpg.create_pool(**sql_info)
abc = await bot.db.execute("SELECT datname FROM pg_database;")
brazen charm
#

Yeah that should work

torn sphinx
#

i tried printing abc

#

its printing this

brazen charm
#

I would do dB.fetchrows(query) I think it is

#

And then print the result

torn sphinx
#

🤔

#

abc = await bot.db.fetchrows("SELECT datname FROM pg_database;")?

#

<Record datname='postgres'> its printing this

velvet coyote
#

so i want to create a new table and insert values into it, but If a table already exists then all i want to do is insert the values

#

how can I do that

brazen charm
#

Hmm if you do execute can you do fetchall

#

Being on mobile I can't check any of this just btw

torn sphinx
#

use fetchall?

#

im confused what to use/do

brazen charm
#

Basically just execute query and fetch all the results

#

I'm pretty sure it's gonna just return its own name which is annoying

velvet coyote
#

what error will it raise if the table already exists?

wintry stream
#

what error will it raise if the table already exists?
@velvet coyote you can try creating the table, and look up in the docs of your driver on what error it raises when a table already exists

velvet coyote
#

docs of my driver whats' that?

wintry stream
#

the documentation of your database driver

velvet coyote
#

so look it up in asyncpg docs?

wintry stream
#

if asyncpg is your driver then yes

#

!d

delicate fieldBOT
velvet coyote
#

no asyncpg

#

sad

wintry stream
velvet coyote
#

oh thx

wintry stream
#

yeah docs don't work like google

velvet coyote
#

how do they work?

wintry stream
#

or wait

#

when you execute a query

#

do CREATE TABLE IF NOT EXISTS @velvet coyote

velvet coyote
#

hmmm

torn sphinx
#

It's returning its own name only

craggy girder
#

if i have 2 columns, user and msg, how can i make it so that the msg column gets info on every message, while user column will get it only once, if you know what i mean

#

for example- user: vinam | message: hi | message: bye and not user: vinam | message: hi user: vinam | message: bye

#

or maybe i am not supposed to make 2 columns for this or idk, how am i supposed to do this?

wintry stream
#

@velvet coyote I guess good news. Asyncpg is now part of the !d command

#

@craggy girder not sure what your plan is, but every row can only hold as much data as there are columns

craggy girder
#

wait i am ok with what i have, nvm

torn sphinx
#
    bot.db = await asyncpg.create_pool(**sql_info)
    abc = await bot.db.fetch("SELECT datname FROM pg_database;")
    print(abc)
[<Record datname='postgres'>, <Record datname='template1'>, <Record datname='template0'>]

@brazen charm

wintry stream
#

@torn sphinx if you replace fetch with fetchrow it will only return the first row

lime echo
#

Can someone help me? i've been trying to fix this error for 2 days already.

#

I am using heroku pg:push to push my local database to Heroku.
I get the following error:

throw er; //unhandled error event```
What's the best way to fix this?
wintry stream
#

And then you can do abc = abc[0] to get the actual result

lime echo
#

It looks like a node.js issue tho lol.

junior hound
#

hi guys, how I can get list of my entire databases in postgresql

wintry stream
#

you want all tables in a database?

junior hound
#

for example when I open my pgadmin I can see list of my databases

#

I will get name of them

wintry stream
#

oh you mean the actual databases

#

that's not possible afaik, that's database protected

junior hound
#

@wintry stream not table name of databases as shown in pgadmin

wintry stream
#

like in pgadmin you log in so you can access it

junior hound
#

ohh really 😲

#

it's some wired

#

isn't it

wintry stream
#

if you want the name of all databases in your server you need to log in to the pgadmin

#

but 1 second let me take a look

junior hound
#

actually I will get those name in python or node.js but python would be better

wintry stream
#

i have a feeling it might only work from the admin account (postgres)

velvet coyote
#

thx @wintry stream

junior hound
#

ahh, I should run command in terminal, may be I should get result through value that returned by library

#

@wintry stream thank you so much, However if you could find another way please let me know

wintry stream
#

you can try putting that SELECT datname FROM pg_database; into your python code

#

cannot tell if it will work

#

have not tried that myself

junior hound
#

very well I can run it as query in my python

#

yes, I was looking for something like this

brazen charm
#

@torn sphinx okay that probably means it's connecting to postgres 12 not 13

torn sphinx
#

i have already deleted postgres 12

#

what i do?

#

im so stuck over here cant do anything

craggy girder
#

how can i get the count of rows of a column in sql

proven arrow
#

@craggy girder You can use COUNT(*) for the column

#

Or if you you want to do it from python the cursor should have a rowcount attribute, or you can also do len() after fetching the rows

craggy girder
#

i see, thanks

#

ah

proven arrow
#

@junior hound Try this,

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
strong parrot
#

hi everyone, i have to read a big file and insert the contents of this big file in a postgreSQL table. do you have any trick to use as little ram as possible?

#

I use psycopg2 btw

brazen charm
#

@torn sphinx you've just deleted it from pgadmin

#

The server itself will still be running

torn sphinx
#

i deleted from pgadmin

#

what do i do create a new server and try?

#

i am reinstalling it

torn sphinx
#

bruh im stuck idk what to do im done

velvet coyote
#

How does CREATE TABLE IF NOT EXISTS work?

torn sphinx
#

deleted the postgresql 13 server oof

#

can anyone help me via teamviewer or something

violet axle
#

How does CREATE TABLE IF NOT EXISTS work?
If the table name you are giving after this is not present in your database it will be created or else nothing happens

velvet coyote
#

ohh, but I want something to happen if the table exists

#

how can I make do that?

violet axle
#

It differs from CREATE TABLE as for that if the table already exists then error is thrown

velvet coyote
#

i wish i knew whwt error it would throw

violet axle
#

ohh, but I want something to happen if the table exists
What do you want to happen?

#

i wish i knew whwt error it would throw
Table name already exists

#

Remember it just checks for name, so you can't have same name for two tables with different fields in a database

torn sphinx
#
@bot.event
async def on_guild_join(guild):
    await bot.db.execute("INSERT INTO prefix(guild_id, guild_prefix) VALUES ($1, $2)", guild.id, "/")
    print("DB updated!")

asyncpg.exceptions.DataError: invalid input for query argument $2: '/' (a bytes-like object is required, not 'str')

#

in db the guild_prefix is an char type

velvet coyote
#

What do you want to happen?
@violet axle If the table already exists then all I want to do is add values and update one of the values, if not then I would have to create the table and then add the values

violet axle
#

This will help I guess

#

There are two approaches, just need to write IF-ELSE construct for that

velvet coyote
#

hmm

violet axle
#

You insert values with INSERT INTO VALUES

velvet coyote
#

ik that

#

but the main part was checking if the table exists

torn sphinx
#

how to check your database data in postgresql?

#

in pgadmin

velvet coyote
#

and the values exist

violet axle
#

but the main part was checking if the table exists
The link will help you

velvet coyote
#

it indeed did.
Thankyou @violet axle

violet axle
#

Explained in detail there

#

You're welcome

torn sphinx
#

how to check your database data in postgresql? pgAdmin

violet axle
#

You mean you want to check tables present in the database?

#

Or want to check records in a table of the database?

torn sphinx
#

records

#

i want to check what all records are saved in my table

brittle galleon
#

anyone can suggest me a really good dataset for uni coursework?

violet axle
#

i want to check what all records are saved in my table
Simple. SELECT statement

#

This will help you further

#

SELECT * FROM <table_name> will show you all the records in the table you give in place of <table_name>

torn sphinx
#

i mean cant i see it in pgadmin?

#

something like db browser

violet axle
#

Yeah

#

You can

#

anyone can suggest me a really good dataset for uni coursework?
On which field do you need dataset

brittle galleon
#

On which field do you need dataset
@violet axle something that can get interest like health sector

violet axle
brittle galleon
#

or mental health as well could do too

violet axle
#

Search in kaggle. You will surely find many good datasets

brittle galleon
#

doing it

torn sphinx
#

what is list called in postgresql data type

wintry stream
#

i guess array

#

let me verify

torn sphinx
#
query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix varchar,
                PRIMARY KEY (guild_id)
                )"""
    await bot.db.execute(query)
    await bot.db.execute("INSERT INTO prefix(guild_id, guild_prefix) VALUES ($1, $2)", guild.id, "/")
#

in this "/"

#

i want it to append it in a list

#

currently its a string

#

i want to remove it from string and make it a list

wintry stream
#

i'd say define gild_prefix as a char of like length 5

#

you don't want super long prefixes

torn sphinx
#

but i want it a list so there can be multiple prefixes

wintry stream
#

guild_id VARCHAR[]

wild pivot
#

def binSearch(arr, 1, n, x):

#

what is the syntax error in this?

torn sphinx
#

my json
{"game87424449350601415": {"team1": ["204255221017214977", "720022112466894970", "757251560752545822"], "team2": ["547356090531315752", "764049047719510026", "532575064386961445"]}}
and i want it to remove that "game87424449350601415" completely all those team1 and team2, and i want it do with a command

wintry stream
#

@wild pivot your argument is a literal

#

your arguments must always be variables

violet axle
#

Oh yes, you used a 1 there that's the error. You only pass variable arguments in function definition

#

If you want that 1 is taken as default then you can write it as this

#

def binSearch(arr, n, x, low=1)

#

you can take any other variable.in place of low. I assume that one is for lower bound index of the list

mental quiver
#

So I made a Python module that wraps the sqlite3 module into a Object Oriented approach. It essentially allows anyone who doesnt know sql commands (such as me) to easily perform a large variety of actions, by creating and executing commands on the given database for you.

Are there other similar modules out there that I dont know of or no? @ me when responding, and I can show code if anyone is interested.

proven arrow
#

@mental quiver Database ORMs exist

mental quiver
#

ah right, true

brazen charm
#

missing SET

jovial notch
#

hmmmmm

#

where?

brazen charm
#

and also probably a WHERE unless you want it to override everything

jovial notch
#

i want it to overide old databse

brazen charm
#

read the link i sent you

jovial notch
#

query3 = """UPDATE mostwantedactivity WHERE (member_id, time) VALUES ($1, $2)"""

proven arrow
#

You've still not read it

#

And are you sure you want to overwrite everything, with the same value. What is the purpose of that?

jovial notch
#

Don't mind that it's a system im making

#

query3 = """UPDATE mostwantedactivity SET (member_id, time) WHERE VALUES ($1, $2)"""
ok so i read the link and im supposed to do that I think right?

#

it still dont work

brazen charm
#

thats not how it works

proven arrow
#

UPDATE mostwantedactivity
SET membe_id = $1, time = $2
WHERE condition;

jovial notch
#

where conditio?

proven arrow
#

You should be able to figure that out right?

#

See the link again, and the examples in it

jovial notch
#

query3 = """UPDATE mostwantedactivity SET member_id = $1, time = $2 WHERE member_id = $1"""

#

i get it now

#

thats it yeah?

proven arrow
#

Syntax looks fine, so now try it and see

jovial notch
#

query1 = """SELECT member_id FROM mostwantedactivity WHERE member_id = $1 RETURNING time"""

#

What's wrong with this now?

proven arrow
#

Why are you Returning time?

lime echo
#

I am using heroku pg:push to push my local database to Heroku.
I get the following error:

throw er; //unhandled error event```
What's the best way to fix this?
proven arrow
#

Looks like a heroku error, with "events.js" and throw

lime echo
#

mhm that looks like it

#

you think that it's not from my side?

proven arrow
#

Have you tried searching on the web?

lime echo
#

Yes, a lot.

#

I didn't find anything that I could use to solve it.

proven arrow
#

What does it do the command you are entering?

#

Nvm I see

#

Well are both the databases same type?

jovial notch
#

@proven arrow i just want to return that db info

proven arrow
#

Well I don't think you can use Return for select statement.

jovial notch
#

you can

#

i have it for other commands too.

#

thats the command.

lime echo
#

Well are both the databases same type?
@proven arrow udj what that means, but I have postgres 12.4 on both my local and on Heroku.

proven arrow
#

You can fetch the row instead after selecting

#

@proven arrow udj what that means, but I have postgres 12.4 on both my local and on Heroku.
@lime echo Yeah that's what I meant, however then I am unsure since I don't use heroku. And it seems a heroku issue. Maybe check their forums.

lime echo
#

I tried and found that slab.

#

I think that it's more of a node.js issue xD

#

events.js:287 throw er; //unhandled error event

proven arrow
#

Remove the *

jovial notch
#

oh ok

#

ye ye

#

mb

proven arrow
#

DELETE, deletes the entire row by default so you don't need to tell it to delete every column

jovial notch
#

ok last question and we done for today

#

From ctx.message_created_at which returns times like these 2020-11-06 18:40:22.942000 how can I make it so my command returns it like 2 hours & 3 minutes ago?

#

Y'all know?

proven arrow
#

You can use an external library or regex for that from python

#

I think there is a library called humanise, unless that is the one I used in php

#

Try searching on internet, I'm not entirely sure the name of it

jovial notch
#

what about this? @proven arrow

proven arrow
jovial notch
#

how does it work?

#

@proven arrow can u show me? first time using this

proven arrow
#

See the examples on that page, it's shown very clearly under the datetime section. You would just pass a datetime object to it

jovial notch
#

humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=3600))

#

that would be it so it returns hours?

hoary sonnet
#

ABstraCti¤n{B¤x =>(bindObservable = n2sFiles{network, security, storage}) Unb¤x =>(bindObservable = i2rData{reliability, integrity, redundancy})}

jovial notch
#

@hoary sonnet whats that

hoary sonnet
#

It's a template in technology describing abstraction

#

I'm an IT Contractor

#

I also made bind into base to derived, protocol to interface, namespaces to databases

#

You can see how it fits as an acronym, and observable into object, set, vector, table

#

@jovial notch in short, I'm binding a data structure, minimizing them into files, then maximizing into data types so to speak

#

I also took the A,B, and C in abstraction as it relates to pythagorean theorem and finding the quadratic formula with the golden ratio.

#

That's the mathematical explanation to abstraction

#

I like to call it boxing and unboxing instead of min and max as it relates to coding

#

And it also relates to virtual machines

#

The box model in CSS.

#

I also use alt code 0164 to symbolize the square or boolean logic

warped frigate
#

in postgresql 13, if you have a column typed as integer[] named data_points, how would I get the average of the array held within data_points for every row in a query?
ex:
data_points integer[]
[1, 2, 3, 4, 5, 6, 7] - query should return 4
[2, 3, 4, 5, 6, 7, 8] - returns 5
[3, 4, 5, 6, 7, 8, 9] - returns 6

hoary sonnet
#

You mean mathematically?

warped frigate
#

no

hoary sonnet
#

You apply an average formula to each row is what I would do.

warped frigate
#

averages are just sum(items)/len(items) in math, but idk how to do it in postgres because doing SUM(data_points) gives an error with groupby

hoary sonnet
#

Yeah

warped frigate
#

how would one apply an average formula to each row

hoary sonnet
#

I would like to learn that too. I'm new to postgress

warped frigate
#

o

hoary sonnet
#

It's a stat lib?

#

I would use a stat lib

warped frigate
#

what's a stat lib

hoary sonnet
#

Statistic lib

#

Library

warped frigate
#

well i'm storing data in a numpy array if that's what you mean

hoary sonnet
#

Or you can make the formula yourself

warped frigate
#

the data is for a financial market analyzer

hoary sonnet
#

I have heard of numpy

warped frigate
#

ok

hoary sonnet
#

Brb

#

I would look into using this @warped frigate

#

Gl

warped frigate
#

that library doesn't have any postgres functions

#

it's totally unrelated to what i'm looking to do

hoary sonnet
#

You use it separately

warped frigate
#

why?

#

i can calculate averages fine outside of postgres

#

just trying to do it in the query because it seems it'll be quicker

hoary sonnet
#

Oh I see...

#

No, I would use an ORM for fetching data, not doing averages

warped frigate
#

yeah but the average of the values is used for filtering what data i want to be returned

hoary sonnet
#

You would probably want nosql for that

warped frigate
#

yeah can't do that, client said it has to be postgresql

hoary sonnet
#

Well, gl

warped frigate
#

thanks

#

yeet figured it out

glass gorge
#

would having a lot of data in a table, impact the ability for mysql to add a column?

proven arrow
#

Yes since it would copy the entire table to a new table

#

See the section under Performance and Space Requirements

glass gorge
#

i tried to add a column to a table with 2k data points, and it's very slow; it's just sitting there.

#

hmm interesting section

#

so if i use the inplace algorithm, does that delete the content of the table?

#

but even so, 2k rows is not that many for a computer to handle

#

shouldn't be freezing like this

#

seems like it's in use

#

show open tables; shows that my table is In_use=1

#

@proven arrow suggestions?

#

i mean it processed everything finally

#

so if i use inplace that would help it process quicker, but does that impact the data in the table?

proven arrow
#

Yeah that shouldn't be the case for 2k rows

#

They are different algorithms used when using alter table

glass gorge
#

i see

idle storm
#
    "769732395309334568": [
        **"696684329178366002"**
    ],
    "773668635930525766": [
        "682319844368056331"
    ]
}```How could I remove the one specified from my JSON file (696684329178366002)?
#

pls ping

mystic acorn
#

Are there any good databases similar to CouchDB (as in that work over HTTP) ?

#

I've been hard pressed to find a decent document store that works over HTTP

torn sphinx
#
    query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix char[],
                PRIMARY KEY (guild_id)
                )"""
    await bot.db.execute(query)
    await bot.db.execute("INSERT INTO prefix(guild_id, guild_prefix) VALUES ($1, $2)", guild.id, "/")

earlier i was just using a char but now i want to make it a list and append the prefix to the list. How can i append the prefix?

woeful anchor
#

Hey. What is a good laptop to program on? I use python, java, and am trying to work with C++

fleet jasper
#
def availablebooks():
    cursor.execute("select BookName from BookInfo")
    b=cursor.fetchall()
    return b
def bookprice(a):
    if a in availablebooks():
        cursor.execute("select PriceinRs from bookinfo where bookname like '{}'".format(a))
        c=cursor.fetchall()
        return c

print(availablebooks())
print(bookprice("Physics"))
#
[('Mathematics',), ('Physics',), ('Chemistry',), ('Computer Science',), ('English',)]
None
short pulsar
#

@woeful anchor Any laptop that will support your favourites dev softwares 😄

radiant elbow
#

@fleet jasper You're checking if 'Physics' is in [('Mathematics',), ('Physics',), ('Chemistry',), ('Computer Science',), ('English',)]. It isn't. ('Physics',) is.

fleet jasper
#

sorry, forgot to mention earlier i wrote that only at first- but it didnt work either way

radiant elbow
#

fetchall returns a list of tuples, one tuple per row with one item in the tuple per column.

#

you probably want to unpack those tuples in availablebooks.

fleet jasper
#

unpack? how to do that?

radiant elbow
#
def availablebooks():
    cursor.execute("select BookName from BookInfo")
    books = []
    for book in cursor.fetchall():
        books.append(book)
    return books
fleet jasper
#

oh adding it in the tuple.

radiant elbow
#

Or if you're familiar with list comprehensions, you could do that all on one line, but basially the same thing.

#
def availablebooks():
    return [row[0] for row in cursor.execute("select BookName from BookInfo").fetchall()]
fleet jasper
#

ok- earlier i got no reply so now i m making modules at different directories and trying it other way 🙂

radiant elbow
#

to be fair, you didn't ask a question. 😛

torn sphinx
#
    query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix char[],
                PRIMARY KEY (guild_id)
                )"""
    await bot.db.execute(query)
    await bot.db.execute("INSERT INTO prefix(guild_id, guild_prefix) VALUES ($1, $2)", guild.id, "/")

earlier i was just using a char but now i want to make it a list and append the prefix to the list. How can i append the prefix?

fleet jasper
#

to be fair, you didn't ask a question. 😛
@radiant elbow oh ya- network issues,, messages got deleted idk how

proven arrow
#

@torn sphinx do you want to update or insert?

torn sphinx
#

so what i want to ask is

  1. how to append/remove/len and check if a element is there in a list
  2. samw with dict
#

@proven arrow

proven arrow
#

And then if you want examples you can always Google how to use that function, and you should see plenty.

velvet coyote
#

What does fetchrow do?

proven arrow
#

@velvet coyote It fetches and returns the first row from the matching results.

torn sphinx
#

why is it appending to an dict and not a list

velvet coyote
#

Hey why am I getting this error?

async def main():

    conn = await asyncpg.connect('postgresql://--------------------------')


    await conn.execute('''
    CREATE TABLE users(
        id BIGSERIAL PRIMARY KEY NOT NULL,
        first_name VARCHAR(50) NOT NULL,
        last_name VARCHAR(50) NOT NULL,
        dob DATE NOT NULL
    )
    '''
    )

    await conn.execute('''
        ALTER TABLE users ADD CONSTRAINT unique_name UNIQUE(first_name)
        '''
    )

    values = {'Abhigya':['Pokharel','1990-12-18'], 'Jan':['Doe','2006-3-15'], 'John': ['Doe', '2013-8-12']}

    for k, v in values.items():
        await conn.execute(f'''
        INSERT INTO users(first_name, last_name, dob)
        VALUES({k}, {v[0]}, {v[1]})
        '''
        )


    row = await conn.fetchall('''
    SELECT * FROM users
    ''')

    await conn.close()

asyncio.get_event_loop().run_until_complete(main())

why am I getting this error?

    await conn.execute(f'''
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/asyncpg/connection.py", line 295, in execute
    return await self._protocol.query(query, timeout)
  File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.UndefinedColumnError: column "abhigya" does not exist

when I try to do this through postgres it works but I can't get it to work using asyncpg could someone help please

proven arrow
#

@velvet coyote Dont use fstrings for the query, also your passing the datetime as a string when it should be datetime

velvet coyote
#

ohh ok

proven arrow
#

Change it to this:

await conn.execute('''
            INSERT INTO users(first_name, last_name, dob)
            VALUES($1, $2, $3)
            ''', k, v[0], datetime.strptime(v[1], '%Y-%m-%d'))
velvet coyote
#

what's $1, $2, $3?

proven arrow
#

And dont forget to import at the top, from datetime import datetime

#

$1 is just a placeholder for the argument that will go into it

velvet coyote
#

hmmmm

#

ohh ok

proven arrow
#

So $1 will be replaced by k , $2 with v[0] and so on

velvet coyote
#

first let me drop that table

#

Now it gives me this

 File "inserting.py", line 38, in main
    row = await conn.fetchall('''
AttributeError: 'Connection' object has no attribute 'fetchall'
proven arrow
#

Its fetch, not fetchall

velvet coyote
#

ohh

#

how can I get the data as a dict?

#

I am currently getting it as a list for some reason

<Record id=1 first_name='Abhigya' last_name='Pokharel' dob=datetime.date(1990, 12, 18)>, <Record id=2 first_name='Jan' last_name='Doe' dob=datetime.date(2006, 3, 15)>, <Record id=3 first_name='John' last_name='Doe' dob=datetime.date(2013, 8, 12)>]
#

also how does IF NOT EXISTS work?

sleek halo
#

hi guys who can talk DM?

velvet coyote
#

no, ask here

sleek halo
#

i am a beginner and i need help to learn more and expand 🙂

stark flax
#

There is no reason to use DMs

#

Help given in DMs can't be vetted and you can get seriously bad advce

#

it's best to just ask in the correct channel for the question you're needing help with. Or use #❓|how-to-get-help and create a help channel @sleek halo

ember depot
#

Hello everybody, has anyone one of you experimented with multi paradigm DBs like ArangoDB?

torn sphinx
velvet coyote
#

thx

#

hmmm

velvet coyote
#

I don't get it

torn sphinx
#

{"Sam": {"money": 100}, "Ava": {"money": 100}, "John": {"money": 1000}}

#

Now I want to get who has the most "money" (this is a json)

#

Like-

  1. John has 1000
  2. Ava has 100
    3.Sam has 100
#

Something like a leader board

torn sphinx
#
query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix char[],
                PRIMARY KEY (guild_id)
                )"""
        await self.bot.db.execute(query)

can anyone help how can i append something in guild_prefix list

#

earlier it was not an list but now i am using list so it can have multiple prefix

earlier code when i was not using list: await bot.db.execute("INSERT INTO prefix(guild_id, guild_prefix) VALUES ($1, $2)", guild.id, "/")

hoary sonnet
#

Morning

proven arrow
#

@torn sphinx You should change the array type to VARCHAR(50) so you are able to store more characters in that array.
Then to insert a new record the following will work. (The element ! is added to the array):

await your_connection.execute("INSERT INTO prefix VALUES($1, $2)", 1234, ['!'])

And to then update the following will do it. (The element . is added to the array):

await your_connection.execute("UPDATE prefix SET guild_prefix = array_append(guild_prefix, $1) WHERE guild_id=$2", '.', 1234)
torn sphinx
#

what does VARCHAR(50) do i mean the 50 and where do i change it

brazen charm
#

The max length of a string

proven arrow
#

Yeah 50 was just example, you can change it as you like. But I don't think someone will have prefix that long or do they?

velvet coyote
#
    await conn.execute('''
    CREATE TABLE IF NOT EXISTS people (
        id BIGSERIAL PRIMARY KEY NOT NULL,
        user_id INT NOT NULL,
        user_name VARCHAR(50) NOT NULL,
        user_score VARCHAR(20) NOT NULL
    )
    
    ALTER TABLE ADD CONSTRAINT unique_id UNIQUE(user_id)

    ''')

this will execute both of the statements right?

proven arrow
#

No

velvet coyote
#

why not?

proven arrow
#

Afaik asyncpg only allows one command per execute

velvet coyote
#

ohh

proven arrow
#

Try it and you'll see if it works, but I'm sure you will get that error

velvet coyote
#

yep syntax error

#

why does this give me syntax error?

    await conn.execute('''
    ALTER TABLE people ADD CONSTRAINT unique_user_id UNIQUE(user_id))
    '''
    )

error
:

    return await self._protocol.query(query, timeout)
  File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near ")"
proven arrow
#

You have an extra ) bracket

torn sphinx
#
query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix VARCHAR(10),
                PRIMARY KEY (guild_id)
                )"""
        await self.bot.db.execute(query)

        prefix = "/"
        await self.db.execute("INSERT INTO prefix VALUES($1, $2)", guild.id, ['/'])

this is when bot joins a server ^

then to add prefix it works like this

prefixes = await self.bot.db.fetchrow("SELECT * FROM prefix WHERE guild_id = $1", ctx.guild.id)

        total_prefix = prefixes["guild_prefix"]

        if prefix in total_prefix:
            return await ctx.send("Prefix already exists!", delete_after=15)
        
        await self.bot.db.update("UPDATE prefix SET guild_prefix = array_append(guild_prefix, $1) WHERE guild_id=$2", f"{prefix}", ctx.guild.id)
#

how i do the prefix remove thing?

#

how to remove the whole json

#

like all the things that it has

#

and leave that
{}

velvet coyote
#

DO I have to do values seperately or can I do them in the same execute statements

proven arrow
#

They can be in the execute statement

#

@torn sphinx to remove is the same as when you add to it, except for remove you can use the function array_remove

torn sphinx
#

yh i used it

#

now i am trying if it is working or not

proven arrow
#

@torn sphinx You just overwrite it with an empty dict

#

my_json = {}

velvet coyote
#

What is this error?

  File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.NumericValueOutOfRangeError: integer out of range
torn sphinx
#

yeah

#

for yo in users:
users.pop(yo)

#

it did this

#

it did this
@torn sphinx Why pay linear complexity instead of doing what @proven arrow suggests?

proven arrow
#

@velvet coyote are you trying to store a number larger than the column type?

torn sphinx
#

um okk

languid shale
#

what is pyx?

velvet coyote
#
    await conn.execute('''
    CREATE TABLE IF NOT EXISTS people (
        id BIGSERIAL PRIMARY KEY NOT NULL,
        user_id INT NOT NULL,
        user_name VARCHAR(50) NOT NULL,
        user_score VARCHAR(20) NOT NULL
    )

    ''')

    await conn.execute('''
    ALTER TABLE people ADD CONSTRAINT unique_user_id UNIQUE(user_id)
    '''
    )

    await conn.execute('''
    INSERT INTO people(user_id, user_name, user_score)
    VALUES(707472976454483988, '♦ Inheritanc-e#1090', 12)
    '''
    )

hmmm???

languid shale
#

Cython?

torn sphinx
#

well its not that hard to just make for loop

#

but fine

proven arrow
#

Use BIGINT and not INT for the user id column. @velvet coyote

torn sphinx
#

well its not that hard to just make for loop
@torn sphinx What if you have a billion keys in that dictionary?

velvet coyote
#

ohh ok

torn sphinx
#

@torn sphinx What if you have a billion keys in that dictionary?
@torn sphinx thats not a point to argue actually

velvet coyote
#

can u use IF NOT EXISTS IN ALTER TABLE AS WELL?

proven arrow
#

No

velvet coyote
#

oh

torn sphinx
#

@torn sphinx thats not a point to argue actually
@torn sphinx Assigning a new dictionary is a constant time operation, which means, a constant number of cycles of your CPU will be used for doing that operation.
If you have one single key in the dictionary, it can be fine.
Do it with 10 keys, it will take more time i.e. more CPU cycles.
Do it with with infinitely keys, and you will be wasting your time removing references from that dictionary instead of just reassigning it.

velvet coyote
#

then I will just do this istead

    try:
        await conn.execute('''
        ALTER TABLE people ADD CONSTRAINT unique_user_id UNIQUE(user_id)
        '''
        )
    except asyncpg.exceptions.DuplicateTableError:
        pass
proven arrow
#

There is IF EXISTS when you want to drop columns or constraints

torn sphinx
#

@torn sphinx Assigning a new dictionary is a constant time operation, which means, a constant number of cycles of your CPU will be used for doing that operation.
If you have one single key in the dictionary, it can be fine.
Do it with 10 keys, it will take more time i.e. more CPU cycles.
Do it with with infinitely keys, and you will be wasting your time removing references from that dictionary instead of just reassigning it.
@torn sphinx Don't do the garbage collector job 😄

#

oki

#
        query = """CREATE TABLE IF NOT EXISTS prefix (
                guild_id bigint,
                guild_prefix VARCHAR(10),
                PRIMARY KEY (guild_id)
                )"""
        await self.bot.db.execute(query)

        await self.bot.db.execute("INSERT INTO prefix VALUES($1, $2)", guild.id, ['/'])
asyncpg.exceptions.DataError: invalid input for query argument $2: ['/'] (expected str, got list)
#

@proven arrow

#

check now

hoary sonnet
#

Do you need to do this in Django or are these queries abstracted?

proven arrow
#

@torn sphinx because your column type is no longer an array.

#

See how you create the table, you never specify it to be an array

torn sphinx
#

oh yeh i used [] now

torn sphinx
#

Can someone help me with this issue? I'm using asyncpg.

await conn.execute("INSERT INTO test_table(test1, test2, test3) VALUES($1, $2, $3) ON CONFLICT DO NOTHING", 123, 123, str({"title": "yes"}))

The first 2 values are both bigint's, & the last one is json. Running this query gives me this error:

Invalid input syntax for type json
DETAIL:  Token "'" is invalid.
#

Figured it out. I used json.dumps({"title": "yes"}) in place of str({"title": "yes"})

shrewd geyser
#

When you use sqlite3 how do I get the value instead of where it is

deep hill
#

Hello, could anybody in the know help me with this WHERE statement?

                           "WHERE strftime('%d/%m/%Y', 'LastLoginDate') >= strftime('%d/%m/%Y', 'now', '-10 days') "
                           "GROUP BY PlayerIP HAVING COUNT(DISTINCT SteamID) > 1 ")```

LastLoginDate is a date value stored as 03/11/2020 for example. What I am trying to achieve is data to be retrieved where the LastLoginDate is within the last 10 days.

There is retrievable data in the database, but the where statement seems to be incorrect. Googling suggested the strftime function can be used inside a where statement in SQLite3?
proven arrow
#

@deep hill Why not just WHERE LastLoginDate > datetime('now', '-10 days') ?

#

When you use sqlite3 how do I get the value instead of where it is
@shrewd geyser What do you mean? I didnt understand?

shrewd geyser
#

@proven arrow Like if I want name from people where id=something. It just returns to me where the name is. I want what the name is

deep hill
#

@deep hill Why not just WHERE LastLoginDate > datetime('now', '-10 days') ?
@proven arrow I don't know but that works... thank you! does this method default to day first date format?

proven arrow
#

@shrewd geyser Huh? No it doesnt. It should return you the value. Can you show your query?

shrewd geyser
#

Sure, it returns a <name object at >

proven arrow
#

Oh ok well thats because you must be outputting the object, instead of its values

#

@deep hill What do you mean first date format?

shrewd geyser
#

Here's my query ```sql
SELECT bank FROM economy WHERE citizen_num=?

In python
```py
bank = c.execute('''SELECT bank FROM economy WHERE citizen_num=?''', value)
proven arrow
#

datetime('now', '-10 days') - now is the current datetime, and the second value is the modifier, so its applied to now to get the date of past 10 days

deep hill
#

datetime('now', '-10 days') - now is the current datetime, and the second value is the modifier, so its applied to now to get the date of past 10 days
@proven arrow it doesn't appear to be working correctly. I think I need to define that LastLoginDate uses the day first format (1/11/2020 instead of 11/1/2020) which is why I was playing with the strftime command. I think the datetime('now' '-10 days') bit is probably ok

proven arrow
#

@shrewd geyser You need to fetch the result after executing the query, so bank.fetchone()

#

Also are you sure that query you provided works? Because value should be provided as a tuple.

shrewd geyser
#

Yea there tuples

proven arrow
#

Oh i see, maybe where you declared it. Then thats fine

shrewd geyser
#
value = (ctx.author.id,)
```Thats how to do it right?
proven arrow
#

yes

sullen hinge
#

Hi, I'm trying to insert a json object into SQL server but keep getting error saying that the JSON text is not properly formatted.
json.dumps({"197512025":"https://www.finn.no/realestate/homes/ad.html?finnkode=197512025"}) Is the data I'm trying to insert. Anyone have an idea as to what I'm doing wrong?

#

the object itself works fine through Management studio when marked as nvarchar and passed to the openjson function

#

Never mind, I made a silly mistake.

fast whale
#

pls ping me if anybody replies

proven arrow
#

@fast whale That's not a database error

#

Read the error, it says `ValueError: invalid literal for int() with base 10 'testing'

#

Which means you passed a string when the function was expecting a integer

fast whale
proven arrow
#

You should have your column link to another table with a Foreign Key, and in that table have all those columns

velvet coyote
#

you can do this right?

            ALTER TABLE IF NOT EXISTS infractions ADD CONSTRAINT unique_user_ud UNIQUE(user_id))
torn sphinx
#

so I am trying to store assignable roles that are available in each server, what is the best way to do this is postgres?

This is how I would store them in json

Database
  guilds
    guild_id
      assignable_roles
        role1
        role2
        role3
#

because I dont think storing each role is works in rows

#

wait no

#

i figured it out

#

it works

proven arrow
#

@velvet coyote You can use IF EXISTS instead. This make sense as well because you would only want to alter a table if it exists, and not alter it if it does not exist. You can see the syntax for it in the documentation https://www.postgresql.org/docs/current/sql-altertable.html

#

And you have an extra closing bracket in your statement at the end.

velvet coyote
#

right

#

thx

#

@proven arrow ohh, u misunderstood what i was trying to do, it seems, I wanted to add the constraint if it doesn't exist to avoid relation already exists error

proven arrow
#

There is no syntax for that unless you manually check or handle the exception. Also I don't see why you need to do this?

#

You create the constraint once? Why do you need to do it multiple times?

torn sphinx
#

using discord.py, should i store everyithin in the database a char data type?

#

or should I limit everything

#

postgres sorry

#

as the database

wintry stream
#

@torn sphinx it depends on what you want to store

#

for instance i store user IDs as a bigint

torn sphinx
#

so bigint up to 19 digits, but discord id are at 18 rn, could this be an issue in the future tho?

#

I guess if i'm worried about it then I should store as text?

#

or is it bad to store everything as text char-type?

#

I would be storing guild_id user_id tags tag_contents, etc

#

btw @wintry stream what is "warnid"?

wintry stream
#

just an auto increasing serial

#

bigint just means it can store up to 19 digits

#

you can store 18 digits just fine

torn sphinx
#

u think it's fine if I store them all as text types tho?

#

like everything, every single entry in the database

wintry stream
#

i mean...that will mean you do need to do some conversion

#

also it takes more storage afaik

#

but my warnid is just an auto incrementing PK

torn sphinx
#

what does pk stand for?

#

oh nvm

#

primary key i assume ?

wintry stream
#

yes

torn sphinx
#

yo big serial can go higher than bigint :v

wintry stream
#

but serial is auto incrementing and afaik cannot be manually inserted

torn sphinx
#

what does afaik mean?

wintry stream
#

it can probably go higher because int has 1 bit for negative numbers

#

as far as i know > afaik

torn sphinx
#

ah,

wintry stream
#

int has either the first or last bit as one that can go negative, and since serial cannot go negative it uses that bit for extra numbers

torn sphinx
#

oh i see

#

aight, thnx for the help

wintry stream
#

for a user ID you can just use bigint

#

it will be fine

#

before discord needs to switch to length 20 id's it will be a very long time

torn sphinx
#

ye I just did the math (was not thinking before) and their number far exceeds how many humans are going to be alive when the bot, or i die.

#

going to use the bigint

#
@bot.event
async def on_ready():
    global conn
    conn = await asyncpg.connect(host = 'localhost', database = 'DiscordBotDatabase', user = 'postgres', password = os.getenv('DB_PASSWORD'))
    await conn.execute('''INSERT INTO config
        (guild_id bigint, prefix varchar(16))
    VALUES
        (123, '456')
    ''')
asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "bigint"

why is thi erroring

#

note: the table does not exist, but from what I understand this should crreate it?

wintry stream
#

you don't add the type when you use insert into

#

let me verify it would create it

torn sphinx
#

but how does the database know what datatype then? does it auto?

wintry stream
#

maybe use IF NOT EXISTS

torn sphinx
#

asyncpg.exceptions.UndefinedTableError: relation "config" does not exist

#

when i removed the datatype opt

wintry stream
#

you first create the table using CREATE TABLE

#

there you specify data types

#

then INSERT INTO will check if it's correct

torn sphinx
#

oh i see

wintry stream
#

you can use CREATE TABLE IF NOT EXISTS if you're not sure if it already exists

torn sphinx
#

cool

torn sphinx
#

is it good practice to asyncpg close(), after every connect, send data to database/receive?

wintry stream
#

i don't close the connection

#

at most i would use transactions

torn sphinx
#

so a query is requesting data right? and a transaction is when you interact with the database, like change data?

#

so .close() when changing database data?

#

sorry what do u mean I dont think I follow

#

so keeping the database connection up for as long as the bot is up is okay? or can this cause any corruption or something bad?

wintry stream
#

@torn sphinx i've yet to had any faulty data and i never closed the connection

#

a transaction is just something in SQL that allows you to just ctrl z

#

so like you can create a transaction, do a lot of stuff to the database

#

if you're happy, you commit and it gets pushed

#

if you're not happy, you just rollback and it just continues like nothing ever happened

torn sphinx
#

what does ctrl z do?

#

oh i understand

wintry stream
#

i mean transactions and closing a connection isn't exactly related

#

it's more of a general tip you could use

torn sphinx
#

I think I kind of follow, I'll look into the transaction docs

#

thnx

#

hi, I would like to save this as json to use it later as a mongodb document. How can I translate this grammar files to json? <symbol> = a mix of constants and <other_symbol>s

#

help is appreciated

fast whale
#

How to get reason value ?

results = await self.bot.db.fetchval("SELECT guild_id FROM blacklisted_guilds WHERE guild_id = $1",guild.id)

reason = results["reason"]

it says
TypeError: 'int' object is not subscriptable

proven arrow
#

fetchval gives you a single value

#

In this case it's given you an integer stored in the guild column

fast whale
#

ah i should do fetchrow , i guess?

proven arrow
#

Yeah

#

But for that you would have to alter your query

#

Currently you've only asked to retrieve the guild column, so you need to specify other columns as well. Or if you want all columns use * as the column value

fast whale
#

ah got it thanks

torn sphinx
#

any help please?

#

how do i get data from a sqlite db

#

thanks.

wintry stream
#

@finite chasm assuming this is for a discord bot, you have multiple options

#

you can take a very simple 'database' with either sqlite3 and json, which aren't actual databases really but just local files

#

or make a database server, like postgres or mysql

#

i personally really enjoy postgres, but servers are harder to setup

#

at least for a starter

brazen charm
#

they use SQL

torn sphinx
#

pg uses sql too

brazen charm
#

postgre technically also has an extension of SQL that makes at a fully function programming language should you want

wintry stream
#

SQL is seen as the general language for databases

#

some databases slightly alter a few keywords, but it all boils down to the same core

brazen charm
#

you write SQL to execute on the DB to save the data

#

e.g.

wintry stream
#

pretty much all drivers use a string where you write the query

queen saffron
#

has anyone here displayed data from a mongo database to a datatable before using flask? having trouble with pagnating, I wish I knew of an easier way to display a table of the data stored in mongo

wintry stream
#

well with SQL it's quite different from JSON

#

SQL is a whole new language

torn sphinx
#

use a nosql db

wintry stream
#

i personally hate noSQL databases, but that's most likely because i know SQL

#

a noSQL database is a database that doesn't use SQL

#

like mongo doesn't use SQL

#

so SQL is seen as the standard language for using databases

#

and a lot of databases use it, because well it's a standard

#

now some databases know people don't like learning a new language, so they made noSQL databases

#

a database that doesn't use SQL

#

like mongoDB doesn't use SQL, thus it's a noSQL database

#

SQL is just a language

#

something like mySQL or postgreSQL is an actual database

proven arrow
#

something like mySQL or postgreSQL is an actual database
Even NoSQL are also databases.