#databases
1 messages · Page 117 of 1
is pandas good for fetching stuff from the db and return it to discord?
No becaus3 pandas is a in memory dataframe library not a dB driver
You're best bet is postgre sql and asyncpg
A recommendations for self hosted database with ready-to-use REST API?
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?
@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.
right right
How would I delete every row in sqlite
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)
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
@proven arrow How do I store a Python List into a Sqlite3 database?
@late lake You can't, since sqlite doesn't support it
I'm making a seperate table with only the values I need to store
And for each row I will store a value?
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
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
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)
Probably because table is a keyword
What is the correct way to access my database models from another file?
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?
I'm just gonna go with my own solution lol
@proven arrow Yeah let me post an example of what I mean
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?
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)
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.
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
But that should be fine
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
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?
@nocturne basin SQLAlchemy makes life pretty simple
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
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.
https://www.dolthub.com/blog/2020-11-02-doltpy-dolt-in-python/
My team wrote about a Python API we built for a database we are building. It's a SQL database with Git-like version control features.
Dolt is a SQL database with Git-like version control features. It presents a familiar SQL interface while exposing Git-like primitives for…
@proven arrow Thank you for the help still!
@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!
Hi,
I have pymongo BulkWriteError.
I want someone with experience in this field to help me.
Ohhh
I used insert_many func in pymongo.
Hi,
I have pymongo BulkWriteError.
I want someone with experience in this field to help me.
please help me.😫
@late lake please help me
Which module to use to change time in the mongo database
or make a loop?
@late lake please help me
@torn sphinx What do you need help with?
Idk PyMongo
I barely know Sqlite3
@proven arrow How do I update Sqlite3 table column updating each row with new list values?
Someone pls help me with this ^
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'")
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 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?
is there a mongodb/pymongo server?
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
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"})```
You can use executemany with an update statement. The python docs give an example of this with an insert statement. https://docs.python.org/3/library/sqlite3.html
is there any golden rule when using transactions?
What kind of golden rule are you expecting?
When using transactions make them quick, make them count?
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
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
I actually was wondering because of the DISTINCT (and the need for a GROUP BY which I didnot put there for simplicity)
Ok, I found an env I could benchmark. Results are similar, it's early useless over-optimization XD
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
python 3.7
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
can I create a column to a table in the database using sqlalchemy ?
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?
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
Counting time in mongo through loop or module?
and which module
hi
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
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 ?
hi
@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()
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
see example
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
Does that table actually exist?
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.
This the find_by_id method
@proven arrow apparently not.
I did heroku pg:push database.dump DATABASE --app mybotapp btw.
And this is the output I have, it looks for the object but then when I print the object it does't appear
@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
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
Have you login to your database from heroku and verify if those tables are actually there? To see if the import was succesful?
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 cursor is a function, so you are missing the () after it
change it to my_db.cursor()
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/"
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
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)
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
@torn sphinx
oke i try that
@torn sphinx Is this the right one?
oke
wait
import wont show as purple in new file
how make it do that
nvm
#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
i mean idc where the data goes to it's just used for a user specifying a channel @torn sphinx
@torn sphinx dms
Is exploiting database a break of ToS ?
What does that even mean?
hello does anyone know about Matplotlib in python?
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?
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...
how to sort documents with a specific key in mongoDB?
@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.
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
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.
@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.
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")```
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
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"
@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.
@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'
@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 Your function is async so you need to await it, hence why you are getting just the coroutine object error.
I am awaiting it
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
So what do I do?
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
You can do the same with asyncio run_until_complete
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
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
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
For web dev is it necessary to learn PHP?
@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.
@swift fossil jesus no
What modules are suggested with async support that can access and modify MariaDB databases?
Please tag me upon reply.
Hi guys, in mysql, can we use LIKE statement in ON clause when joining tables?
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 ☝️
i am getting error
https://mystb.in/ExpertsTremblAssignment.sql
when i am running my main bot file
oh I see
?
@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).
sql_info = {"database": "FlanticDB",
"user": "postgres",
"password": "my_pass",
"host": "my_ip"}
@quaint yarrow its like this
@torn sphinx is your postgres listening on port 5432?
how do i check?
also is my_ip in this case local or remote? If it's remote, is it possible a firewall is preventing the connection?
local as of now will soon update it to vps ip
And you're on Windows?
yup
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.
it send an list of active connections
TCP 0.0.0.0:5432 0.0.0.0:0 LISTENING
its there
What happens if you run psql -h my_ip -p 5432
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)
the bot?
Yea
stuck for 10-15 sec then throw error
idk lol how do i check
what i mean is, is the value of "host" in your sql_info dict "localhost"
Try using "localhost" instead.
asyncpg.exceptions.InvalidCatalogNameError: database "FlanticDB" does not exist
there is a FlanticDB
Well, it's progress, it connected this time. lol
Depends. Is the database on the same VPS?
i mean currently im learning db so i am not doing on vps rn just asking
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.
hmm
also
asyncpg.exceptions.InvalidCatalogNameError: database "FlanticDB" does not exist
yea. so. makes me wonder if the user the bot is connecting as has permission for that db
oof there are permission to connect to db
but i am local hosting so i dont think so it would block it
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?
via pgAdmin
Did you log in to pgAdmin with the same credentials that are in your sql_info dict?
oh
weird.
You may need to lowercase the db name
on my on DB or in the config file
i changed both to flantic still not workin
sql_info = {"database": "flantic",
"user": "postgres",
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?
can anyone help me with sql querys
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...
It's import mysql.connector
Try importing it inside just a python interpreter
how do i do that in vs code if u can help...
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
Did you mean to do like this
No I meant in a python shell, so it'll look like the command prompt except you can run python code in it
So try installing it again from a Windows command prompt, like this
python3 -m pip install mysql-connector-python
It's possible you installed it to another location like an venv
I did it in inbuilt vscode terminal
I'll try in cmd as well
It also shows already installed
Show the output
wait
i tried uninstalling it and installing it also
but still
It's the same
It must be something silly i usually do that
#SQL Data
sql_info = {"database": "FlanticDB",
"user": "postgres",
"password": "my_pass",
"host": "localhost"}
Code: https://mystb.in/ObserveConnectivitySyndrome.python
Error: asyncpg.exceptions.InvalidCatalogNameError: database "FlanticDB" does not exist
SS: https://cdn.discordapp.com/attachments/696348850981699624/774221294098579466/unknown.png
can anyone help me why am i getting that error if i have a DB called FlanticDB
You sure you're not connected to the postgres 12 server
im so confused like what i do?
SELECT datname FROM pg_database;
@brazen charm try run that in the query tool in pg admin
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
@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
i mean the error comes when i start the bot
@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? 😭
new file?
I have no idea I don't use heroku
@torn sphinx make a new file, setup. A basic asyncpg system
ooo ok!
postgres
Do you know how to execute sql in asyncpg?
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;")
Yeah that should work
🤔
abc = await bot.db.fetchrows("SELECT datname FROM pg_database;")?
<Record datname='postgres'> its printing this
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
Hmm if you do execute can you do fetchall
Being on mobile I can't check any of this just btw
Basically just execute query and fetch all the results
I'm pretty sure it's gonna just return its own name which is annoying
what error will it raise if the table already exists?
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
docs of my driver whats' that?
the documentation of your database driver
so look it up in asyncpg docs?
yeah docs don't work like google
how do they work?
hmmm
It's returning its own name only
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?
@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
wait i am ok with what i have, nvm
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
@torn sphinx if you replace fetch with fetchrow it will only return the first row
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?
And then you can do abc = abc[0] to get the actual result
It looks like a node.js issue tho lol.
hi guys, how I can get list of my entire databases in postgresql
you want all tables in a database?
for example when I open my pgadmin I can see list of my databases
I will get name of them
oh you mean the actual databases
that's not possible afaik, that's database protected
@wintry stream not table name of databases as shown in pgadmin
like in pgadmin you log in so you can access it
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
actually I will get those name in python or node.js but python would be better
In this tutorial, we will show you how to show databases in PostgreSQL using psql and pg_database catalog.
i have a feeling it might only work from the admin account (postgres)
thx @wintry stream
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
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
very well I can run it as query in my python
yes, I was looking for something like this
@torn sphinx okay that probably means it's connecting to postgres 12 not 13
i have already deleted postgres 12
what i do?
im so stuck over here cant do anything
how can i get the count of rows of a column in sql
@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
@junior hound Try this,
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'
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
@torn sphinx you've just deleted it from pgadmin
The server itself will still be running
i deleted from pgadmin
what do i do create a new server and try?
i am reinstalling it
bruh im stuck idk what to do im done
How does CREATE TABLE IF NOT EXISTS work?
How does
CREATE TABLE IF NOT EXISTSwork?
If the table name you are giving after this is not present in your database it will be created or else nothing happens
It differs from CREATE TABLE as for that if the table already exists then error is thrown
i wish i knew whwt error it would throw
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
@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
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
T-SQL - How to check if table exists in a database using a select statement or sql query.
This will help I guess
There are two approaches, just need to write IF-ELSE construct for that
hmm
You insert values with INSERT INTO VALUES
and the values exist
but the main part was checking if the table exists
The link will help you
it indeed did.
Thankyou @violet axle
how to check your database data in postgresql? pgAdmin
You mean you want to check tables present in the database?
Or want to check records in a table of the database?
anyone can suggest me a really good dataset for uni coursework?
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>
Yeah
You can
pgAdmin - PostgreSQL Tools for Windows, Mac, Linux and the Web
anyone can suggest me a really good dataset for uni coursework?
On which field do you need dataset
On which field do you need dataset
@violet axle something that can get interest like health sector
Moreover ML stuff goes to #data-science-and-ml I think
or mental health as well could do too
Search in kaggle. You will surely find many good datasets
doing it
what is list called in postgresql data type
i guess array
let me verify
https://www.postgresql.org/docs/9.5/arrays.html @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
i'd say define gild_prefix as a char of like length 5
you don't want super long prefixes
but i want it a list so there can be multiple prefixes
guild_id VARCHAR[]
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
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
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.
@mental quiver Database ORMs exist
ah right, true
missing SET
i want it to overide old databse
read the link i sent you
query3 = """UPDATE mostwantedactivity WHERE (member_id, time) VALUES ($1, $2)"""
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?
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
thats not how it works
UPDATE mostwantedactivity
SET membe_id = $1, time = $2
WHERE condition;
where conditio?
You should be able to figure that out right?
See the link again, and the examples in it
query3 = """UPDATE mostwantedactivity SET member_id = $1, time = $2 WHERE member_id = $1"""
i get it now
thats it yeah?
Syntax looks fine, so now try it and see
query1 = """SELECT member_id FROM mostwantedactivity WHERE member_id = $1 RETURNING time"""
What's wrong with this now?
Why are you Returning time?
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?
Looks like a heroku error, with "events.js" and throw
Have you tried searching on the web?
What does it do the command you are entering?
Nvm I see
Well are both the databases same type?
@proven arrow i just want to return that db info
Well I don't think you can use Return for select statement.
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.
Documentation seems to differ then if that is working, https://www.postgresql.org/docs/13/dml-returning.html
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.
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
Remove the *
DELETE, deletes the entire row by default so you don't need to tell it to delete every column
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?
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
what about this? @proven arrow
This is the one https://pypi.org/project/humanize/
See the examples on that page, it's shown very clearly under the datetime section. You would just pass a datetime object to it
humanize.naturaltime(dt.datetime.now() - dt.timedelta(seconds=3600))
that would be it so it returns hours?
ABstraCti¤n{B¤x =>(bindObservable = n2sFiles{network, security, storage}) Unb¤x =>(bindObservable = i2rData{reliability, integrity, redundancy})}
@hoary sonnet whats that
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
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
You mean mathematically?
no
You apply an average formula to each row is what I would do.
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
Yeah
how would one apply an average formula to each row
I would like to learn that too. I'm new to postgress
o
what's a stat lib
well i'm storing data in a numpy array if that's what you mean
Or you can make the formula yourself
the data is for a financial market analyzer
I have heard of numpy
ok
that library doesn't have any postgres functions
it's totally unrelated to what i'm looking to do
You use it separately
why?
i can calculate averages fine outside of postgres
just trying to do it in the query because it seems it'll be quicker
yeah but the average of the values is used for filtering what data i want to be returned
You would probably want nosql for that
yeah can't do that, client said it has to be postgresql
Well, gl
would having a lot of data in a table, impact the ability for mysql to add a column?
Yes since it would copy the entire table to a new table
See the section under Performance and Space Requirements
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?
Yeah that shouldn't be the case for 2k rows
They are different algorithms used when using alter table
i see
"769732395309334568": [
**"696684329178366002"**
],
"773668635930525766": [
"682319844368056331"
]
}```How could I remove the one specified from my JSON file (696684329178366002)?
pls ping
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
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?
Hey. What is a good laptop to program on? I use python, java, and am trying to work with C++
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
@woeful anchor Any laptop that will support your favourites dev softwares 😄
@fleet jasper You're checking if 'Physics' is in [('Mathematics',), ('Physics',), ('Chemistry',), ('Computer Science',), ('English',)]. It isn't. ('Physics',) is.
sorry, forgot to mention earlier i wrote that only at first- but it didnt work either way
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.
unpack? how to do that?
def availablebooks():
cursor.execute("select BookName from BookInfo")
books = []
for book in cursor.fetchall():
books.append(book)
return books
oh adding it in the tuple.
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()]
ok- earlier i got no reply so now i m making modules at different directories and trying it other way 🙂
to be fair, you didn't ask a question. 😛
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?
to be fair, you didn't ask a question. 😛
@radiant elbow oh ya- network issues,, messages got deleted idk how
@torn sphinx do you want to update or insert?
so what i want to ask is
- how to append/remove/len and check if a element is there in a list
- samw with dict
@proven arrow
Postgres has many array operators and functions that you can use. It's best if you read more about them here, rather than me repeating what it says. https://www.postgresql.org/docs/current/functions-array.html
And then if you want examples you can always Google how to use that function, and you should see plenty.
What does fetchrow do?
@velvet coyote It fetches and returns the first row from the matching results.
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
@velvet coyote Dont use fstrings for the query, also your passing the datetime as a string when it should be datetime
ohh ok
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'))
what's $1, $2, $3?
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
So $1 will be replaced by k , $2 with v[0] and so on
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'
Its fetch, not fetchall
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?
hi guys who can talk DM?
no, ask here
i am a beginner and i need help to learn more and expand 🙂
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
Hello everybody, has anyone one of you experimented with multi paradigm DBs like ArangoDB?
how can I get the data as a dict?
@velvet coyote https://magicstack.github.io/asyncpg/current/api/index.html#record-objects
I don't get it
{"Sam": {"money": 100}, "Ava": {"money": 100}, "John": {"money": 1000}}
Now I want to get who has the most "money" (this is a json)
Like-
- John has 1000
- Ava has 100
3.Sam has 100
Something like a leader board
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, "/")
Morning
@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)
what does VARCHAR(50) do i mean the 50 and where do i change it
The max length of a string
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?
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?
No
why not?
Afaik asyncpg only allows one command per execute
ohh
Try it and you'll see if it works, but I'm sure you will get that error
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 ")"
You have an extra ) bracket
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
{}
DO I have to do values seperately or can I do them in the same execute statements
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
What is this error?
File "asyncpg/protocol/protocol.pyx", line 316, in query
asyncpg.exceptions.NumericValueOutOfRangeError: integer out of range
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?
@velvet coyote are you trying to store a number larger than the column type?
um okk
what is pyx?
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???
Cython?
Use BIGINT and not INT for the user id column. @velvet coyote
well its not that hard to just make for loop
@torn sphinx What if you have a billion keys in that dictionary?
ohh ok
@torn sphinx What if you have a billion keys in that dictionary?
@torn sphinx thats not a point to argue actually
can u use IF NOT EXISTS IN ALTER TABLE AS WELL?
No
oh
@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.
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
There is IF EXISTS when you want to drop columns or constraints
@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
Do you need to do this in Django or are these queries abstracted?
@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
oh yeh i used [] now
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"})
When you use sqlite3 how do I get the value instead of where it is
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?
@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?
@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 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?
@shrewd geyser Huh? No it doesnt. It should return you the value. Can you show your query?
Sure, it returns a <name object at >
Oh ok well thats because you must be outputting the object, instead of its values
@deep hill What do you mean first date format?
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)
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
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
@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.
Yea there tuples
Oh i see, maybe where you declared it. Then thats fine
value = (ctx.author.id,)
```Thats how to do it right?
yes
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.
pls ping me if anybody replies
@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
that is what idk where , lemme ask in discord.py
You should have your column link to another table with a Foreign Key, and in that table have all those columns
you can do this right?
ALTER TABLE IF NOT EXISTS infractions ADD CONSTRAINT unique_user_ud UNIQUE(user_id))
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
@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.
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
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?
using discord.py, should i store everyithin in the database a char data type?
or should I limit everything
postgres sorry
as the database
@torn sphinx it depends on what you want to store
for instance i store user IDs as a bigint
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"?
just an auto increasing serial
bigint just means it can store up to 19 digits
you can store 18 digits just fine
u think it's fine if I store them all as text types tho?
like everything, every single entry in the database
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
yes
in this tutorial, we will introduce you to the PostgreSQL SERIAL and show you how to use the serial to create an auto-increment column in a database table.
yo big serial can go higher than bigint :v
but serial is auto incrementing and afaik cannot be manually inserted
what does afaik mean?
it can probably go higher because int has 1 bit for negative numbers
as far as i know > afaik
ah,
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
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
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?
but how does the database know what datatype then? does it auto?
maybe use IF NOT EXISTS
asyncpg.exceptions.UndefinedTableError: relation "config" does not exist
when i removed the datatype opt
you first create the table using CREATE TABLE
there you specify data types
then INSERT INTO will check if it's correct
oh i see
you can use CREATE TABLE IF NOT EXISTS if you're not sure if it already exists
cool
is it good practice to asyncpg close(), after every connect, send data to database/receive?
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?
@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
i mean transactions and closing a connection isn't exactly related
it's more of a general tip you could use
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
this is the file https://github.com/googleprojectzero/domato/blob/master/common.txt
help is appreciated
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
fetchval gives you a single value
In this case it's given you an integer stored in the guild column
ah i should do fetchrow , i guess?
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
ah got it thanks
@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
they use SQL
pg uses sql too
postgre technically also has an extension of SQL that makes at a fully function programming language should you want
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
pretty much all drivers use a string where you write the query
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
use a nosql db
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
something like mySQL or postgreSQL is an actual database
Even NoSQL are also databases.