#databases
1 messages · Page 86 of 1
lol
Look into SQLite, and perhaps use an ORM to go with it to make managing the database a little more elegant
So, I know that collation means... how things are indexed and order, similarly to how a library organized itself or how letters are in order so that we have a concept of "alphabetical"
shellmancer_dev-# \d user
Table "public.user"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+----------------------------------
id | integer | | not null | nextval('user_id_seq'::regclass)
user_name | character varying(120) | | |
email | character varying(120) | | not null |
image_file | character varying(40) | | not null |
password | character varying(60) | | not null |
date_created | timestamp without time zone | | not null |
date_confirmed | timestamp without time zone | | |
is_over_18 | boolean | | |
is_admin | boolean | | not null |
is_verified | boolean | | not null |
Indexes:
"user_pkey" PRIMARY KEY, btree (id)
"user_email_key" UNIQUE CONSTRAINT, btree (email)
Referenced by:
TABLE "gamemaster" CONSTRAINT "gamemaster_gamemaster_id_fkey" FOREIGN KEY (gamemaster_id) REFERENCES "user"(id)
TABLE "player" CONSTRAINT "player_player_id_fkey" FOREIGN KEY (player_id) REFERENCES "user"(id)
But what is meant here by the column "collation"
@torn sphinx Which database is this, just so I know?
@torn sphinx OK, I'm not as familiar with Postgres, but I think what I'm seeing here is a cousin to the way other DBs work
The "collation" column here is a property of each column type that indicates how that column's data is to be sorted
Database collations are usually for the sake of alphabetizing or sorting according to the way different regions or locales handle such things
Nothing in that column probably means "use the database's default collation"
hello, what is best SQL ide for storing data on discord servers?
when you make web applications, do you use an ORM to create the table and relationships? or do you create the tables in the database first?
Normally you use an ORM to define the tables
and a migration tool (Alembic for SQLAlchemy) to make changes to your schema without losing data
why a migration tool if you're not migrating anything?
At some point during development, you're schema is gonna change - if you already have data, you don't want to lose that data in the process. A migration tool is a thing that lets you change your schema safely
so let's say you have a web application similar to Meetup and it's in place for like a full year. then you want to rename a column in a table, and drop a column in another table. how do you do that?
(if not through a SQL IDE)
Alembic?
You'd use the migration tool. Its been too long, and I never learned them well enough to give you the exact code. But roughly what you'd do is make the changes to your schemas, then you'd run something like makemigrations with Alembic or the Django migrations tool
hmm ok cool thanks i'll check it out
What would be the best method to develop a search system / feature for an application (or in this case, a bot) that searches data in a database? Right now what I do is kind of building dynamic SQL but I'm not sure if there's a better way, especially when you deal with more filters and such.
Hmm should I believe this linter, as I saw I should do connection.cursor() inside aiopg
This is the connection
This is the method which returns the connection

I am getting the error: No such column: context_set
The problem is just that I definetly have that column in my db. I have verified it with DB browser.
This is my code for creating the db:
def intent_db_make():
if isfile(intent_db_path):
intent_db_backup()
# opening database
conn = _sqlite3.connect(intent_db_path)
c = conn.cursor()
c.execute("""CREATE TABLE intents (
row_num integer,
tag text,
patterns text,
responses text,
intent_type text,
response_type text,
context_set text,
context_filter text,
mood text
)""")
# apply changes and close database
conn.commit()
conn.close()
and this is the code for adding data to the db (this is where the error orcurs)
for row_number, i in enumerate(intents['intents'], row_num):
print("-----------------------")
print("Current row number is {}".format(row_number))
# extracting data from file and adding to variable
tag = i['tag']
patterns = '&/&'.join(i['patterns'])
responses = '&/&'.join(i['responses'])
intent_type = i['intent_type']
response_type = i['response_type']
if 'context_set' in i:
context_set = i['context_set']
else:
context_set = 'N/A'
if 'context_filter' in i:
context_filter = i['context_filter']
else:
context_filter = 'N/A'
if 'mood' in i:
mood = i['mood']
else:
mood = 'N/A'
if tag in tag_list:
print("Tag: '{}' already exists in database".format(tag))
pass
else:
# adding the data to the database
c.execute("INSERT INTO intents VALUES (:row_num, :tag, :patterns, :responses, \
:intent_type, :response_type, context_set, :context_filter, :mood)", {
'row_num': row_number,
'tag': tag, 'patterns': patterns, 'responses': responses,
'intent_type': intent_type, 'response_type': response_type,
'context_set': context_set, 'context_filter': context_filter,
'mood': mood})
conn.commit()
print("Data added to row")
# apply changes and close database
conn.commit()
conn.close()
The error gets thrown at the buttom where it inserts it into the db
Heya. I’m trying to use WITH ROLLUP on a MySQL query made through SQLAlchemy.
Unfortunately, the rollup implementation of sqlalchemy.func doesn’t produce the right syntax for the MySQL dialect.
Any idea how to fix that, or find a workaround to issue the right query at the very least?
Hey. I am trying to write a simple scraper, which requires saving data in a database and I couldn't find any design patterns. What would be the correct way to interact with a sqlite db in the scraper? Would I make some sort of a wrapper with methods like add, edit, delete or would I create connections and execute the commands directly? Also where should I store the sqlite commands for creating a database, altering tables and so on?
Please tag me in your reply.
Hello! I'm looking for a database similar to enmap for JS where the interface is as if it were a python object, but it is actually persistant
does anyone know a database system like that?
existing_user = User.query.filter_by(name = form.username.data , email = form.email.data).first()
print(existing_user)
if existing_user:
print("This is bad news")
return make_response(f"The name or the gmail you enter is already created")```
Im trying to check if the user who want to sign up used an account or name thats already exists
but this is not working!
any help pls?
Why the User.query is not working ?
so when i use sql in python i want to get a whole row where the data is equal to something, i get the whole row but i want to have the values separate so its like 20, 30 etc not (20,), (30,) how can i do that?
@odd scaffold SQLite comes with Python and might serve the same job
@violet mountain Can you explain a little better? I'm not sure what you're striving for or how your current querying works
@lament heath "not working" could mean anything. Is it crashing? erroring? doing nothing?
if result:
curs.execute('''SELECT ID FROM Faults WHERE Number_Plate=? AND Fixed=?''',(reg.get(),fixed))
hmm = curs.fetchall()
print(hmm[1])
``` it will print out "(30,)" but i just want it to print out "30"
@violet mountain When you retrieve records they're as a tuple, so you can just take each row and get element 0 from it if you only have one element
k
@celest zodiac I don't know if the problem is coming from it because I don't know how to check if db.sesson.add is working anyway
def signup():
form= Signup()
print("hi")
if request.method=="POST":
existing_user = User.query.filter_by(name = form.username.data , email = form.email.data).first()
print(existing_user)
if existing_user:
print("This is bad news")
return make_response(f"The name or the gmail you enter is already created")
else:
user= User(form.username.data,form.email.data,form.password.data)
print(user)
db.create_all()
db.session.add(user)
db.session.commit()
print("Saved")
flash('Thanks for registering')
return redirect(url_for("home"))```
Here's the code for the sign up
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(100), unique=True)
email = db.Column(db.String(100), unique=True)
password=PasswordField(db.String(50))
def __init__(self,name,email,password):
self.username=name
self.username=email
self.username=password
```
that worked @celest zodiac why does it work like that?
@lament heath Can you inspect the database directly'
@violet mountain when you return a record, even if it's only one column, it's expressed as a tuple or some other iterable, because there could be any number of columns
oh kk so i just need to always have [0] ty#
I don't know how acutally
@lament heath what DB are you using?
sqlalchemy
No, the underlying DB, not the ORM
Do you mean this flask_sqlalchemy sorry im new to databases and these stuff
@lament heath I'm talking about the underlying database itself -- is it SQLite? MySQL?
oh sqlite
OK
there's actually a standalone GUI you can use to inspect SQLite databases
I do a lot of SQLite stuff and it's incredibly handy
so this way you can look directly in tables, etc. and make sure things are being recorded as intended
oh thanks a lot i will check this out
probably asked a lot here but what would be the best integrated/supported in python, postgresql or mysql/mariadb
hey guys, i'm haivng a little problem with SQLAlchemy (flask), the code is the following:
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
import sqlite3
db_file = 'spacegame.db'
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Create Table Users
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_file
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = "User"
id = db.Column('id', db.Integer, primary_key = True)
nickname = db.Column('nickname', db.String(100))
email = db.Column('email', db.String(100))
password = db.Column('password', db.String(20))
ERROR:
id = db.Column('id', db.Integer, primary_key = True)
TypeError: Column() got an unexpected keyword argument 'primary_key'
but primary key show be "expected", right?
id = db.Column("id",db.Integer, primary_key=True)
same error
i'll try something
try to change it to name.sqlite3
@celest zodiac I download it but the code is actually not saving any values to databases
def signup():
form= Signup()
print("hi")
if request.method=="POST":
existing_user = User.query.filter_by(name = form.username.data , email = form.email.data).first()
print(existing_user)
if existing_user:
print("This is bad news")
return make_response(f"The name or the gmail you enter is already created")
else:
name=form.username.data
email=form.email.data
password=form.password.data
db.session.add(User(name,email,password))
db.session.commit()
print("Saved")
flash('Thanks for registering')
return redirect(url_for("home"))```
And its not showing any errors
Is the save action running?
its printing saved
and name,email,password have a value also
id = db.Column("id",db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
email = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100), unique=True)
def __init__(self,name,email,password):
self.username=name
self.username=email
self.username=password
```
solved by:
Reinstalling flask-sqlalchemy
oh
yes, but thanks guys!
i must check
but i guess it is
i changed the name of the file to spacegame.sqlite3
it was spacegame.db
it did not solved the issue
but it's not giving me any exception
i'll try to create a table
yup
i'll come back, if it works i'll tell you
ok nice
how to use sqlite3 via shell?
i'm checking videos, they go to the cmd first, create the db
and then they go coding
i'll download itr
Nice good luck😀
i never did anything with databases + programming
my dbs were made on excel
hahahah
you commiting?
the connection
commit + close?
i guess it's not used for web applications
idk
i wish i knew
really
dbs are fucking complex
np... Thanks
I didn't go to deep with them
But I don't know why this code is not showing at least one error
Its just not saving
@lament heath what db are you using?
Anyone know if you can get hacked through 3rd party packages? lol
of course
@celest blaze Are you familiar with Djoser?
What does that even mean?
And I want to just put it out there that my troll alarm is kind ringing
kind of*
I meant it more or less literally: the code that you yourself write can have bugs that could be exploited. The famous example is https://xkcd.com/327/
my programming iq isn't high enough to understand this meme bro
but i get the memo
But what I was asking is if someone could make an open source package
and install shit on their computer when they install that package
I have tried the a command which requires such
with gives me an AttributeError
coroutine does not have attribute cursor
hey guys, i'm really newbie at databases, could someone give me a light
about relations
i'm using sqlite3
i have a game "ready" but it's not using databases
i have my data stored on files
and i want to put it all in databases
so i can fix balancing
i have 4 races at my game
the races share the same properties, like "minerals, population"
things like that
they share all the data
how can it work properly?
Databases are pretty complicated and represent a steep learning curve imo
As I just learned it fairly recently myself, I'm going to offer you some advice on what to start reading first.
You need to understand "The Normal Forms" or "Database Normalization"
https://www.lifewire.com/database-normalization-basics-1019735
You can read about it here or you can watch a video about it, but its pretty important.
The thing you will have to realize is that at first, you will be designing your database only to discover it was missing something you have to add.
But a database is locked in stone. Editing it is very difficult without destroying the data you have in it. So in the beginning, you're going to want to just use a lot of test data to try to get it right.
I think you are right to start with sqlite. You might eventually want a real, online database server but it only makes it harder to understand if you start there imo.
Good luck
@dusty helm
Also, unrelated, I just found the obscure solution to my database problem haha http://blog.code4hire.com/2017/06/setting-up-alembic-to-detect-the-column-length-change/
I love alembic, I love the way it simplifies our lives by generating
bite size deltas to tables in our projects for simple and controlled migrations.
And today, I encountered an interesting problem with alembic on a project I am working on.
I'm really excited about the possibilities of a JSON field in pg.
I'm pretty sure that means that you can store data in the database that isn't necessarily structured
Is that right?
https://www.2ndquadrant.com/en/blog/postgresql-anti-patterns-unnecessary-jsonhstore-dynamic-columns/
PostgreSQL has handy json support. That doesn't mean you should use it for everything.
That's right
I hate Alembic.
Basically what alembic does is assemble a migration file that invariably never works so that I can search stackoverflow for how to edit to make the specific change im trying to make.
@little raven Im using sqlite 3
I have never used sqlite3 before, but by my experience with modern database python apis, in general there should be a connection.commit() method which you have to do
You should refer to sqlite3 docs 😄
Yup I put db.session.commit() but it still not working
And the thing is its not giving any errors to let me know why this is happening
Im pretty new to databases and flask
I have a huge database problem right now that I don't feel like writing up the problem yet. It's such a big problem that I have to do good job of writing up the problem before I bother asking it. And Ive been up all night. So Im just gonna get some rest first.
@lament heath are you perhaps forgetting db.session.add(thing_to_add) first?
You don't need that if you are updating something already in the db, but if you are adding something that is not there, you need to use that.
@torn sphinx I wrote the code here serval times before but I will put it again
Actually nothing is saved
In the database
have you done db.create_all() to initialize the tables in your database?
You dont need that in your code because it only needs to happen one time ever.
Oh
Usually you do it in flask shell
I run it on the main code
I dont think that would drop the fields in your database but it might be messing things up. You only run it once to give the database the outline set up in your models. Once its done you shouldnt need to do it again
Oh
Im trying to open discord from pc
1s
@torn sphinx Im a bit late sry: ```@app.route("/signup", methods=["POST","GET"])
def signup():
form= Signup()
print("hi")
if request.method=="POST":
existing_user = User.query.filter_by(name = form.username.data , email = form.email.data).first()
print(existing_user)
if existing_user:
print("This is bad news")
return make_response(f"The name or the gmail you enter is already created")
else:
name=form.username.data
email=form.email.data
password=form.password.data
print(name,email,password)
usr= User(name,email,password)
db.session.add(usr)
db.session.commit()
print("Saved")
flash('Thanks for registering')
return redirect(url_for("home"))
return render_template("signup.html",form=form)```
_id = db.Column("id",db.Integer, primary_key=True)
name = db.Column(db.String(100))
email= db.Column(db.String(100))
password = db.Column(db.String(100))
def __init__(self,name,email,password):
self.username=name
self.username=email
self.username=password```
I don't know why
I cant see any problem
its none
have you tried printing usr to see if there is a usr object there? Also, you are saving passwords in plaintext. I know that is not your current problem but you cant do that.
I need to hash them
Yeah worry about that later.
Um so from your db viewer I can see that you have your tables created.
print something at the beginning of the else statement where it starts saving stuff to the user
To see if its even getting there
mm, ive seen transient before. I forget what that means.
is it an object?
yeah I think it means that its an object that isnt added yet.
Try to print it after the add but before the commit
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked
[SQL: INSERT INTO user (name, email, password) VALUES (?, ?, ?)]
[parameters: (None, None, None)]
(Background on this error at: http://sqlalche.me/e/e3q8)```
its giving me this error now
SQLite is meant to be a lightweight database, and thus can't support a high level of concurrency. OperationalError: database is locked errors indicate that your application is experiencing more concurrency than sqlite can handle in default configuration. This error means that one thread or process has an exclusive lock on the database connection and another thread timed out waiting for the lock the be released.
Do you have mutliple instance of the database open?
Perhaps in a viewer, in a terminal, in your app
oh
honestly, Im not sure. I dont use sqlite but it should be very simple.
Does it get to thanks for registering and redirecting home?
It redirect the user to home yes
def __repr__(self):
return f"User({self.id}, {self.name}, {self.email})"
Add this to your user model as a method.
Look at the print again
ok
Not sure lets see
like the name,email, and password
I created have values
print("hi")
name=form.username.data
email=form.email.data
password=form.password.data
print(name,email,password)
usr= user(name=name,email=email,password=password)
db.session.add(usr)
print(usr)
db.session.commit()
print("Saved")
flash('Thanks for registering')
return redirect(url_for("home"))
I got it
User(name=name,email=email,password=passowrd)
Now hash those passwords.
That will work
lol
is it ? lol
its not working?
which one?
Damn so your original one said usr= User(name,email,password)
And I over looked that
They have to be keyword args
so I have to do it like this usr= User(name,email,password)
no
Thios is mine for example
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('home'))
form = RegisterForm()
if form.validate_on_submit():
hashed_pw = ph.hash(form.password.data)
user = UserAccount(email=form.email.data, password=hashed_pw)
db.session.add(user)
db.session.commit()
send_verify_email(user)
flash(f"Account created for {form.email.data}"
+ "Check your email to verify account for full functionality. You may login now.", 'success')
return redirect(url_for('login'))
return render_template("register.html",
title="Register",
form=form)
You mean the model?
name=form.username.data
Oh, I dont ask users for a name. I verify by email and password.
I let them add an alias after they make an account but its not connected to auth.
In my view, the less data collected that I dont need, the more protected my users are
oh
But there is nothing wrong with using a username if its important to the app
i just dont need it
ya I understand
what should I import for the hash password?
pip install argon2-cffi
?
that is what I use yeah
ok
I have no idea why your thing wont save.
why its returning None maybe thats the point?
So look
Is there anywhere in your script where you are saying User(name, email, password) Because if you are, you are not saving anything.
You have to say User(name=name, email=email, password=password)
That says, "Take this name variable and put it to the name field in my database"
I would recomend at this point pushing your entire app to a github and sharing the github. Because I dont see whats wrong
yes
k
Oh you know what I just realized, You have a constructor so you can use positional arguments.
This is how I do mine
class UserAccount(db.Model, UserMixin):
"""
UserAccount 1-1 PlayerProfile
UserAccount 1-1 GameMasterProfile
"""
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
user_name = db.Column(db.String(120), nullable=True)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(40), nullable=False, default="default.jpg")
password = db.Column(db.String(77), nullable=False)
date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
date_confirmed = db.Column(db.DateTime, nullable=True)
is_over_18 = db.Column(db.Boolean, default=False)
is_admin = db.Column(db.Boolean, nullable=False, default=False)
is_verified = db.Column(db.Boolean, nullable=False, default=False)
player = db.relationship('PlayerProfile', uselist=False, back_populates="user")
gamemaster = db.relationship('GameMasterProfile', uselist=False, back_populates="user")
def get_token(self, expires_sec):
s = Serializer(app.config['SECRET_KEY'], expires_sec)
return s.dumps({'user_id': self.id}).decode('utf-8')
@staticmethod
def verify_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
user_id = s.loads(token)['user_id']
except:
return None
return UserAccount.query.get(user_id)
def __repr__(self):
return f"User('{self.id}', '{self.email}')"
def __init__(self, **kwargs):
super(UserAccount, self).__init__(**kwargs)
# Create Row in DB When Created
db.session.add(self)
db.session.commit()
# Apply a player/gamemaster profile to users on creation
new_player = PlayerProfile(id=self.id)
new_gm = GameMasterProfile(id=self.id)
# Commit to DB
db.session.add(new_player)
db.session.add(new_gm)
db.session.commit()
I know thats ridiculously long but lol
ye lol
I dont use a constructor. Thats the __init__ where you create an object. I just use the super() thing and accept keyword arguments.
And thats why i thought you needed keyword arguments
np
Im just thinking why its returning None... I have another flask file and its working well there
And nothing is different about either?
_id = db.Column("id",db.Integer, primary_key=True)
name = db.Column(db.String(100))
email=db.Column(db.String(100))
def __init__(self, name, email):
self.name=name
self.email=email
@app.route("/home/")
def home():
return render_template("index.html")
@app.route("/view")
def view():
return render_template("view.html", values=users.query.all())
@app.route("/login/", methods=["POST","GET"])
def login():
if request.method=="POST":
session.permanent = True
user=request.form["nm"]
session["user"]=user
found_user= users.query.filter_by(name=user).first()
if found_user:#If user have logged in before
session["email"] = found_user.email
else:#If theres no user
usr=users(user,"")
db.session.add(usr)
db.session.commit()
return redirect(url_for('user'))
else:
if "user" in session:
flash("Already Logged in")
return redirect(url_for("user"))
return render_template("loginPage.html")```
Im not seeing any difference
Are you familiar with how you use flask shell?
I don't think... I don't what is it tbh
you know you can type python at command line and it will open up a thing for you to try out code and see what works? Well, flask shell is a command that you can use from your envrionment that will load up that interactive thing for you to test out commands but it will load up your database so that you can play with it.
(venv) virufac@box:~/Projects/shell_hacker_game$ flask shell
Python-dotenv could not parse statement starting at line 1
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
App: shellmancer [development]
Instance: /home/virufac/Projects/shell_hacker_game/instance
>>> from shellmancer.models import UserAccount
>>> user = UserAccount(email="dsafadsf@dsafasf.com", password="sdfdsfsD")
>>> user
User('1', 'dsafadsf@dsafasf.com')
Thats how you do it in linux im sure its the same in windows
you'll do from main import user based on your code
Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
And then try to input a user manually
okay then do
set FLASK_APP=main.py
Im assuming you're on windows?
yes
whatever your file is called where you initialize your app.
then do that
where i should put this FLASK_APP=main.py
oh directly into the command line.
cmd or powershell or whever you are in your environment
oh
Error: Got unexpected extra argument (FLASK_APP=main.py)
and you have to use the keyword set FLASK_APP=main.py
from main import user
now try to get a user in there with something other than None lol
if you want to save it you do.
You can do that, btw and see if it works if you cut out all the other stuff
Im hoping that you will just find that particular syntax required to get a correct user object.
you know what you should also consider
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(77), nullable=False)
See how ive set nullable and unique keywords?
db.session.add(admin)
That makes it to where it cant be none
but then you have to drop all your tables and create it again.
because is still returning None
yeah, I think its that constructor.
or some name
All three things are none?
the name, the email, and the password
hey
def __init__(self,name,email,password):
self.username= name
self.username=email
self.username=password
Did you notice that they are all self.username?
class user(db.Model):
_id = db.Column("id",db.Integer, primary_key=True)
name = db.Column(db.String(100))
email= db.Column(db.String(100))
password = db.Column(db.String(100))
also you dont have any field called username
it happens
I hope that it the fix
Im surprised i didnt see that.
I hope too thx a lot if it work or not because you really spent time helping me
Its cool, people spend a lot of time helping me too and I like this stuff. At least now you know how to use flask shell because its helpful when stuff doesnt work
I have to give back at some point.
yes
Always give back and I think it will return to you
yaaaaaaaaaaaaaaaaaa
it worked
For sure
I just thinks its nuts I had to have you open flask shell to find that.
I should have seen it too
No problem. Im glad I get to help someone finally, I usually am the one getting help.
yes, you should only save the hash in the database. The plaintext you should discard. You only use what they enter in the form to turn into a hash and then when they login, you use if ph.verify(user.password, form.password.data): to see if its that right one. If it is it will be True
oh
that way not even you know what their password is.
name=form.username.data
email=form.email.data
hashed_pw = ph.hash(form.password.data)```
hash it like this?
fix your indents but yeah
$argon2id$v=19$m=102400,t=2,p=8$k+I8FmCFZnGCvo4otSSChA$AxnuEZMNS6G+CZr+F54Xiw
this one is hashed?
yep that is what you save.
Oh i thought hashed is something like only points .....
I think if he was a good hacker he will find a way
nah, a hash is just a string that is generated from another string based on an alogirithm so that one always can be changed back to the other but that you can't use a hash table to figure out one from the other. Its hard to explain.
Securing Password by Hashing with Salt
with p=8$k+I8FmCFZnGCvo4otSSChA$AxnuEZMNS6G+CZr+F54Xiw
Keep in mind that all the stuff that is explained in that article is done for you by argon2
oh yes
Honestly, its a matter of a lot of time and comprehensive word list that makes cracking that hash virtually impossible
Do they use something like this in big companies?
yes like every word in it mean something maybe
I mean, in big companies hashing passwords is the first line of defense.
They do a lot more than that.
But yeah, thats why its important that users use passwords that are not guessable
Because if they dont, its almost impossible to crack them because of how much tiem it would take.
But yeah, i gotta get going. good luck man.
Hello. Any sqlalchemy user? I have the next code.
And I want get all Players by match_id, so first idea was kind of this.
So the question is how to do it in the most appropriate way - optimized as in query way as in code clarity.
P.S.
don't mind lazy settings
def get_by_match_id(match_id):
return session.query(Player).filter(match_id in Player.matches).all()
class Player(Base.Model):
__tablename__ = 'players'
id = Column(Integer, primary_key=True)
matches = relationship('Match', secondary='players_matches',
backref=backref('players', lazy='joined'), lazy='joined')
class Match(Base.Model):
__tablename__ = 'matches'
id = Column(Integer, primary_key=True)
class PlayersMatches(Base.Model):
__tablename__ = 'players_matches'
id = Column(Integer(), primary_key=True)
match_id = Column('user_id', Integer(), ForeignKey('matches.id'))
player_id = Column('role_id', Integer(), ForeignKey('players.id'))
for now I'm using session.query(Player).join(PlayersMatches).join(Match).filter(Match.id == match_id).all() (though have to properly test it)
I've never really used join before. I'm not sure hot it works yet.
don't know if this is the right place to ask, but i'm trying to deploy a postgres database to heroku, under their free hobby plan.
tried following their docs, which tells you to upload a pg_dump to an s3, sign the url, and then run a command to import it into heroku
I've done all that, uploading, signing, and when attempting to import i get
expected success, got code 500. I've tried changing the url which I sign, modifying the database and making a new dump, and changing the privacy settings on my s3, but nothing seems to work.
here's the docs for what I'm trying to do, I've followed it all to the t and gave it a few tries, not sure what I'm doing wrong.
https://devcenter.heroku.com/articles/heroku-postgres-import-export
Hi, is this the place for questions related to matplotlib?
@latent berry what happens when you pass in the -v flag for verbose output?
Hey <@&267629731250176001>, all of my help requests get archived before I can receive an answer. Any help? Apologies in advance for the tag.
that's most likely because the people looking through the help channels don't know how to help with your problem. you can try in one of these topical channels, which moves slower and you'll be more likely get an answer. however, you probably want #data-science-and-ml instead of #databases . also, please don't ping moderators next time unless you need moderation.
@runic pilot at which stage? attempting the final stage of importing with the flag makes no difference
i'm going to try troubleshoot some aws permissions as well
if you curl that s3 object can you get it?
i get an access denied, which i presume is because it's set to private
not really used aws before, but setting to public didn't make much difference
think i might have mistyped my aws credentials or something
setting it to public should definitely make a difference
Hello, i got a task been days on it, but my solution is poor looping and need advice please.
Got 2 databases A & B .
they need to update each other, (add , delete , update) both databases table row's have same ID.
A is MySql and B is Access database.
my solution was looping between records with nested looping comparing their ID's and it will be slow with many records,
Any other solution please that i can work on ?
There is no solution here with Microsoft Access
At least not automated
you can import pure sql into Access, but you cannot automate it to my knowledge
Maybe if you write an addin
I also highly question the premise of the solution itself
@untold hull
i turned on every permission I can, now i'm able to curl it but heroku is now complaining in a different way hahahaha
the idea, need php web app and vb6 desktop app.
they work together and the desktop app updates mysql when got internet connection, otherwise data is saved on access,
different databases, server and local one, internet connection not stable but they need to be connected with each other
this task will make me crazy lol, not possible ya ?
yeah really they want me build that also with vb6
forget vb6 xD, but that connection between database on mentioned situation is possible ?
Well, if you got an application as a middle man, then you can translate between the two
But you need to sync yourself
and that will be hard
hmm
Maybe if you use a local Microsoft SQL Server instead and an online MSSQL Server?
I can't give you pointers there, but at least they'd speak the same language
then it can be solved?
Maybe there is some easy sync possible then
I mean, it is pretty simple between 2 MySQL Servers with replication
I have never done it with an unstable connection though
i got your point
thanks alot man, well the task i got must be solved with given databases and language , i think i will drop it lol
Sorry I couldn't help more
i appreciated all :)
Does anyone know of the internal sqlite3 functions that would allow me to open a database from memory/a bytesio
conn = sqlite3.connect(":memory:")
This is opening not creating
I'm trying to open a locked database for reading and short of just copying the file there doesn't seem to be way that sqlite3 would allow
That creates a database in memory
Creating not opening*
opening and creating is the same in sqlite
If it exists, it is opened, if not, it is created
Opening an existing database on disk
So I'm just going to have to copy the file
Oh well
Seems rather limiting on SQLite3's part
I mean, you can read the master table, pull all data and commit it into a new one
idk what exactly you are trying to achieve with BytesIO here
Read from a locked database
Reading from a database is done with sql
that isn't limiting, that is the whole point
Sure but it is filed based
The file reading being kept from you is fine for a high level API
But there is no lower level option of passing the bytes to the parser
Again, simply not the point of a database
And if you simply copy it, it will retain the lock
The easiest method to rescue it is using the cli util, dumping it and importing it into a new file
What do you mean not the point of a database
to have access to the underlying bytes via it's api
I do not want to access the bytes
I just want to give it bytes instead of a filepath
Are there any Python & sqlite helpers online? Need some help getting started, any help would be appreciated!
Asking good questions will yield a much higher chance of a quick response:
• Don't ask to ask your question, just go ahead and tell us your problem.
• Don't ask if anyone is knowledgeable in some area, filtering serves no purpose.
• Try to solve the problem on your own first, we're not going to write code for you.
• Show us the code you've tried and any errors or unexpected results it's giving.
• Be patient while we're helping you.
You can find a much more detailed explanation on our website.
I'm writing a task app in Flask + SQLAlchemy and I have a DB design question: If I have a Task model with foreign keys to Steps, what should I do when I want to cancel a Task? Should I delete it and cascade the delete to include the Steps? Or should I use a status flag on everything (completed, canceled, pending, etc) and not delete anything, just change the status?
both patterns are fine, it depends if you want to keep data around for bookkeeping or "undeleting" something in the case that a user deleted something by accident
quick question about database
so i store something into a database using sqlite3
when it stores it gives it self its own ID
how can i print out the ID without knowing?
@violet mountain I don't know what you mean by "print out the ID without knowing"
okay so when i save to the database, i have an auto increment ID which i don't change
i want to print out the ID of the row when its saved
Generally, when you save a database object, the object will have an .id property which will be updated with the newly saved ID
this is how my database is set up
the invoice is the auto increment
when i add to the database i don't add to the invoice it auto increment it self
so, once you save the new record object, the Invoice field for that object should be auto-populated
yes
it adds one
and so on
i want to print out the invoice when the row is added
right, I'm saying the object you've created to interact with the database to save that record should have that value automatically updated
so you just print the field from that object once you've saved
That would depend on the ORM - can you show me the code where you save a newly created object?
give me 5 mins let me quickly write the code for it
def adding(self,first,last,repair,paid,phone,address,reg,hidden):
#checks if the car doesn't exists
db = sqlite3.connect("Pro.db")
curs = db.cursor()
curs.execute('''SELECT Number_Plate FROM Vehicles WHERE Number_Plate=?''',(reg,))
result = curs.fetchone()
if result:
left = str(repair.get()) - str(paid.get())
curs.execute('''INSERT INTO Faults(Number_Plate,First_Name,Last_Name,Repair_Cost,Amount_Left,Phone_Number) VALUES(?,?,?,?,?,?)''',(reg.get(),first.get(),last.get(),repair.get(),paid.get,left,phone.get(),address.get(),reg))
db.commit()
db.close()
else:
hidden.config(text="Nah")
@celest zodiac thats the program
OK, you're using basic SQLite3, not an ORM
I think it works this way - after curs.execute you'd say row_id = curs.lastrowid
and that would be whatever row ID was generated for the last insert
(It's specific to the cursor in use)
(Note that if you use .executemany you'll get None for that row ID)
oooo ty
@celest zodiac erm why am i getting this error with the database?
curs.execute('''INSERT INTO Payments(Number_Plate,First_Name,Last_Name,Repair_Cost,Amount_Paid,Amount_Left,Phone_Number) VALUES(?,?,?,?,?,?,?)''',(reg.get(),first.get(),last.get(),repair.get(),paid.get(),left,phone.get(),address.get()))
db.commit()
should left be left.get()?
no its a variable
also, for the phone_number column you're using address
OHHHHHHH
@celest zodiac sorry if i'm bothering you but last thing, can you have a look at my database and tell me if its in third normal form?
maybe just pass pre? I dunno
hi can somoene help me with an sql query question
Im using postgres SQL and was wondering the "proper" way to store expanding amounts of informations, aka is there a way to store tables within tables?
I don't think it is necessarily called storing tables within tables.
I found it it’s table relationships
hi
how would I get the second rank based on this
SELECT firstname, department, startdate,
RANK() OVER ( PARTITION BY department ORDER BY startdate DESC) AS rank
FROM Employees;
WITH ranked_data AS (
SELECT firstname,
department,
startdate,
RANK() OVER ( PARTITION BY department ORDER BY startdate DESC) AS rank
FROM Employees
)
SELECT *
FROM ranked_data
WHERE rank = 2;
one option
window functions create what you can think of as an adhoc subtable per row based on the criteria supplied to the function and return a value
depending on your flavor of sql you may have an NTH_VALUE window function available which would clean the above up
i think of the 'subtable' as a 'window' on the data based on the row, function, and params, hence the name i suppose
can anyone help me out with normalizing my database?
https://docs.sqlalchemy.org/en/13/orm/extensions/mutable.html#module-sqlalchemy.ext.mutable
What does this mean? I assume that it means that when I change the column, it's not going to be able to tell in migration. But it did and I migrated a column of sqlalchemy.dialects.postgresql.JSONB successfully into the database. I' wondering if without what is set up in the above documentation, what does that mean? Am I unable to change the data in the json without that?
Im pretty confused, how do u make a remote db?
Like can u host one on remote postgres host or something?
Would sqlite be a good database to use (I'm not planning on bringing the bot to many servers, probably 3 at max)
hey so i understand databases and queries and data pretty well. and connection strings. i am now learning python. if i just want to pull data into my .py file, what should i use?
sqlalchemy?
psycopg2 - got it.
thanks
with open(file, mode=mode, encoding=encoding) as f:
reader = csv.reader(f)
next(reader)
for row in reader:
cur.execute(f"INSERT INTO {table_name} ({','.join(cols)}) VALUES (f{','.join(row)}) ")
conn.commit()
table_name = name of table
cols = columns of table
row = the row from csv being inserted
Traceback (most recent call last):
File "C:\Users\arjun\Codes\misc\Kaggle Datasets\zomato_analysis\add_to_db.py", line 70, in <module>
cur.execute(f"INSERT INTO {table_name} ({','.join(cols)}) VALUES (f{','.join(row)}) ")
psycopg2.errors.SyntaxError: syntax error at or near "Storyss"
LINE 1: ...urkish,vietnamese,wraps) VALUES (f19049583,Street Storyss,In...
This is the error I'm getting. Is there any way to iteratively add data to a db?
or is my syntax completely wrong?
There's many ways to iterably add data
but I would suggest, first, using something called parameterized queries
Here's a relevant example from the postgres wiki:
namedict = ({"first_name":"Joshua", "last_name":"Drake"},
{"first_name":"Steven", "last_name":"Foo"},
{"first_name":"David", "last_name":"Bar"})
You could easily insert all three rows within the dictionary by using:
cur = conn.cursor()
cur.executemany("""INSERT INTO bar(first_name,last_name) VALUES (%(first_name)s, %(last_name)s)""", namedict)
If you do not specify the (first_name) and just use %s, a tuple of elements ("Joshua", "Drake") or a list of them, functions the same
this will handle escaping for you, but perhaps not the table name. Best not to have dynamic table names, however
Ah thanks but I used pandas.to_sql() method, seemed easier cause I had over 125 columns
i wonder if this is the best way for me to access my values
WITH main AS (
SELECT * FROM users.info
WHERE users.info.id = 1
)
SELECT * FROM main
LEFT JOIN users.inventory as inv
ON main.id = inv.user_id
LEFT JOIN users.ratelimits as ratelimits
ON main.id = ratelimits.user_id;
or can i optimize it further
you can replace the * with the actual columns you want
tru smh, also i do wonder, how do i implement values for a function that i made
DO $$
DECLARE
ratelimit INTEGER := 42;
reset_timeout timestamptz := NOW() - '1 hour'::interval;
BEGIN
IF reset_timeout >= NOW() THEN
ratelimit := 45;
reset_timeout := NOW() + '1 hour'::interval;
RAISE NOTICE 'reset';
ELSE
IF ratelimit > 0 THEN
ratelimit := SUM(ratelimit - 1);
RAISE NOTICE 'decrement';
ELSE
RAISE NOTICE 'error';
END IF;
END IF;
END $$;
however, i would want the ratelimit and reset_timeout variables to be
SELECT image AS ratelimit,
image_refresh AS reset_timeout
FROM users.ratelimits
WHERE user_id = 1;
Using PostgreSQL 11
Ok I am totally stuck. So I want to create a database where I save a server_id and in that server_id I want to save data for every user
like this in json
{
guildid: {
userid: {
warns: 5
}
}
}
does anybody know how to do this
Hello guys, I know only little about sqlite and I need something like this:
insert into table (...) values (...) if there is no row with x=a and y=b in the table
What would be the correct statement for that, please?
please tag me in case you answer
Do you want to update every row that has missing data? @flint walrus
I am making a bot with a web app. You can change the prefix for the bot in the web app. When you change the prefix in the webapp, it sends a request to the API written in flask, which then updates the prefix in mongo. The bot (separate python app) then reads the prefix from the mongo database. The problem is, the bot caches the prefix as it would be insanely resource intensive to check the database for every single message the bot sees. Is there a way I can detect when the prefix has been updated in mongo so the bot can update its cache?
how resource intensive would it be?
have you tested to see under what amount of load it starts to buckle?
i have not tested it, but a query takes a short amount of time but when its being constantly bombarded with messages that adds up
is there a way to check how long a mongo query takes?
you can benchmark how long it takes in your code and log that
i'd like to avoid the problem altogether instead of working around it tho. Is there an event or something that fires when an item in mongo changes?
my point is that there are a lot of easy ways to handle high volume load and stay performant without using a cache (which will only add another layer of problems), but yes, I think there is something like that in mongo https://www.mongodb.com/presentations/event-based-subscription-mongodb
interesting, thanks!
hmm
is there an actual docs page on this?
for anyone just joining in im trying to figure out a way to detect when a document in a mongo database updates
they might have a way of sending some sort of message when that happens -- I know that AWS' dynamodb has a mechanism for that, e.g.
@mystic shore Not familiar with MongoDB, but this may help - https://stackoverflow.com/questions/9691316/how-to-listen-for-changes-to-a-mongodb-collection
@mystic shore off of that stack overflow i found a link to a new feature added that'll do just that.
https://emptysqua.re/blog/driver-features-for-mongodb-3-6/#change-streams
i need some refractoring
how do i make this look more beautiful
DO $$
DECLARE
ratelimit INTEGER := (SELECT image FROM users.ratelimits WHERE user_id = 2);
reset_timeout timestamptz := (SELECT image_refresh FROM users.ratelimits WHERE user_id = 2);
BEGIN
IF reset_timeout <= NOW() THEN
UPDATE users.ratelimits
SET image_refresh = NOW() + '1 hour'::interval,
image = DEFAULT
WHERE user_id = 2;
RAISE NOTICE 'reset';
ELSE
IF ratelimit > 0 THEN
UPDATE users.ratelimits
SET image = image - 1
WHERE user_id = 2;
RAISE NOTICE 'decrement';
ELSIF ratelimit <= 0 THEN
RAISE NOTICE 'out of credits';
ELSE
INSERT INTO users.ratelimits(user_id) VALUES (2)
ON CONFLICT DO UPDATE SET
image = excluded.image,
image_refresh = excluded.image_refresh;
RAISE NOTICE 'error';
END IF;
END IF;
END $$;
does it look good or can i make it look better
it runs flawlessly
also instead of RAISE NOTICE 'out of credits'; and such, how can i replace these to returning true or false
that works, but i just realized a much better way to do this, i can just store the ID of the item in mongo so it odesnt have to find it every time
it just has to look at it
anyone know of a widget that displays contents of an sqlite3 database in Tkinter?
or something close to that
no unfortunatley
i can see what the db has easily enough by printing it out
but i need to display the db contents on a GUI as a table
i can just suggest making a custom function then. i could help if you wanted but it might not be until tomorrow.
no worries m8, has to be done tonight lol
Can anybody here help me with a mutiple update function?
I'm just looking for some feedback to make it more elegant / better
How i can output the value BALANCE from table BANK
sorry for my delays smh
no problem 
psql -h localhost -p 5432 -U amigoscode test
Why doesn't this work on psql terminal?
I think I'm following a pretty old tutorial because half the things don't even work for me that works for the person doing the tutorial
https://www.youtube.com/watch?v=qw--VYLpxG4&t=137s
Learn how to use PostgreSQL in this full course. PostgreSQL is a general purpose and object-relational database management system. It is the most advanced open source database system widely used to build back-end systems.
⭐️ Contents ⭐️
⌨️ (0:03:16) What is a Database
⌨️ (0...
Its this one
what are the database init, migrate and update commands in fastApi if i am using SQLAlchemy as my ORM?
@umbral ridge No I don't, I just wanna get rows for which a certain condition evaluates as true.
okay..
select * from agents where commission = '' OR working_area ='';
like that?
@undone roost SQLAlchemy only gives you ORM capabilities, you're looking for a migration library, try alembic https://alembic.sqlalchemy.org/en/latest/ it's made to play well with SQLAlchemy because it's by the same author
@runic pilot Yes alembic i will be using....do i have to use docker in it ?
no, docker is used to make a single unified layer to the OS, nothing to do with app level code aside from installation
@runic pilot refer to the following article https://medium.com/@ahmed.nafies/fastapi-with-sqlalchemy-postgresql-and-alembic-and-of-course-docker-f2b7411ee396 thr guy has used docker ...but i don't necessarily have to ..right?
the docker stuff is entirely optional
I've been using flask+sqlalchemy+alembic+postgres for 5 years and only started using docker in the last year and a half
if you're using a mac, just use this to get a PGSQL server up and running https://postgresapp.com/
then make a setup_db.sh file:
createdb myapp
createdb myapp_test
createuser -s myapp_user
then use postgresql://myapp_user@localhost:5432 as your SQLALCHEMY_DATABASE_URI
remove the single quotes
from all of them?
yeah
@runic pilot i had a previous project ....from which i copied files and pasted into a new project directory.....so when i run the command alembic init . it says directory . already exist and is not empty
what to do?
i mean when to run alembic init .
if i leave the directory empty and run the command and then copy the files ..will that work?
I'd imagine it would
read through this for more info https://alembic.sqlalchemy.org/en/latest/tutorial.html
okay it did
now i can copy files ig
one thing...how to create tables from the models i have?
@autumn coral it's from 2019, and postgresql dev are not the kind to break things from one release to another, they usually add features, not remove them.
@runic pilot the article tells to create tables with alembic but i want to create tables with the models.py file i have created
anyone good at normalising databases?
from myapp.models import Base
target_metadata = Base.metadata
put that in the env.py file and it'll detect your models automatically
that's like one of the first lines in the link I sent, please read that carefully
about this....my structure is
app
alembic
versions
env
database
so what will be the import line for me
database is where the declarative base is
from database import Base
@violet mountain Just ask
no module named database
@rain wagon i need help with normalizing my database
Nobody will answer unless you finally ask a damn question
give me a minute
module not found error
and normally there's more than one table
hmm want the other tables
what's the business logic of this table? why does this table exist?
yup that looks pretty well normalized
it can not be normalized more than that right?
This is how it is normalized
1 customer can have many bills
you can break it down even more if you'd really want to
there are different forms of normalization, do you know which normal form you're supposed to use?
i was told to do 3rd normal form
I've taken out amountleft since that is kind of redundant
can do the math with a select
dam
scorcher24 ty
i was going to 3rd normal my who database but it would over complicate my program as much as it is rn
3rd normalform is something I'd consider the minimum
but even then, as I said, one customer can have many bills
so the previous form blows up in your face as soon as the customer wants a second car repaired
you'd have to make a whole new dataset with the same data you already have on file
you've got a redundant relation to the customerId in the bills table
it looks like your tables don't reference each other
ik
oh there is even more tables
i don't need it in my program
@rain wagon what do you suggest do i go and get the whole database normlised or just leave it as it is
I've added more tables
Now you got vehicles seperate and it gets referenced in the bill
It's also a not so bad idea to separate parts and colors, as there are a lot standard colors and parts etc pp
@violet mountain I would really stick with NF3 and continue this way of doing it, because it means your db can be extended, you can do proper joins etc pp
guys. using sqlite for medium projects, it's cool?
It has only advantages
@dusty helm If it's on the web, use a rmdbs
well, a server based rmdbs
like pgsql?
for example, or MySQL
ok, cool
i'm trying to create a sample saas
actually idk wtf i am doing right now
but thank you dude
kk @rain wagon
scorcher can you do one more thing to it? can you add the faults
if possible
we shouldn't be helping you on your homework
Rule 5
Do not provide or request help on projects that may break laws, breach terms of services, be considered malicious/inappropriate or be for graded coursework/exams.
not homework just a project for myself tbh
before i actually do my whole project
what was this referring to?
i might just lose like 5marks
for when i actually make the whole project
@violet mountain I leave that to you as an exercise 😉
ty
When I try to install MySql onto my computer it tells me that Python(64-bit) is not installed however I do have python installed
https://cdn.discordapp.com/attachments/470275631314239489/700789588083343431/unknown.png
https://cdn.discordapp.com/attachments/470275631314239489/700789794069807204/unknown.png
check if it's in PATH properly
@cinder sierra do all versions of python work for it?
I'd imagine?
@autumn coral it's from 2019, and postgresql dev are not the kind to break things from one release to another, they usually add features, not remove them.
@solid void Then this > https://www.youtube.com/watch?v=qw--VYLpxG4&t=137s < video tells me to do psql --help and he gets a help menu, if he typespsql -h localhost -p 5432 -U amigoscode testhe gets a message, if I do it, even wrong, it doesn't answer me
Learn how to use PostgreSQL in this full course. PostgreSQL is a general purpose and object-relational database management system. It is the most advanced open source database system widely used to build back-end systems.
⭐️ Contents ⭐️
⌨️ (0:03:16) What is a Database
⌨️ (0...
Now why is that?
https://hastebin.com/ducoyureko.py
The command says it works but nothing saves in the database
@fallen blaze Because you only commit if the result is not None
@autumn coral i guess you didn't install postgres properly, or you have a firewall bloking the request, connecting to postgres with psql is the most basic thing. i use similar commands to connect to my db very often, it works with any version.
@solid void Apparently it doesn't psql's bin folder on path
I added it to the path, and now it works!
Btw is there a way to get rid of all the users in psql?
I made one named 'test' to follow a tutorial
And now I can't get rid of it
And is there a good PostgreSQL tutorial you'd recommend me?
There's not many good ones out there
Or even the docs
I can't find the official ones
The postgresql doc is great, but it's not exactly a tutorial, i can't say i can recomment one, i was advise to try ”a curious moon”, a (not free) book that is a big tutorial, i bought it and did part of it, but didn't finish.
The youtube video you linked seems very good though (but i only watched/listened to the first 30mn or so)
Oh
Any sql tutorial should give you the basics anyway
There is a pdf version
Ooo..
Bonus points if you are into astronomy :)
I'm not
But anyways, I'll order it
And until that, I'll watch the tutorial
🙂
Btw how much knowledge does one need to learn PostgreSQL?
@solid void
I'm sorry if I'm bothering you a lot
This is the last one
Like till which level?
Variable? 😂
Oop?
Machine learning?
MySql?
I don't understand the question, i guess a lot of knowledge can be useful, but not definitely necessary
Sql definitely,but the tutorial will teach you at least the basics of that
oh
ML is unrelated
That was an example
MySql is a lot like Postgres, so it does help if you kno it, but you don't need to learn it first
What can help is an understanding of data normalisation, it'll just make you design better DBs
To learn sql, the concept of an array (rows/columns) and of a relationship, are necessary, i think that's about it, and data types like string, int, float, date, etc
Btw, I feel so bad for all the main people like mods, admins and owner
I saw ves getting irritated in tortoise server
He would probably mute him if it was this server lmfao 🤣
We are only humans, but the team is great, and we can take time off when needed, because there are many other people helping, but yeah, sometime users can be tiring.
I had never expected such a big/great/helpful community about python when I thought of learning it
Especially here in discord
Hm yeah, i don't go a lot in other servers (aside one where i'm admin), but you have to understand that rules and moderation can be different other there
mhm
But that's a bit offtopic for here :)
Yes, agree
I just hope ves doesn't go "How dare you be rude to me just because I'm in a different server 😠 " and bans me here 
hey I need some help
with prostreSGL
so i tried to connect my database from another pc and it didn't seem to work
this is my code
async def create_db_pool():
client.pg_con = await asyncpg.create_pool(database = 'levels', user = 'postgres', password = password, host = '127.0.0.1', port = port)```
is there anything I can do to fix it?
@torn sphinx Just for curiosity, is this a localhost?
How did you setup that host?
huh?
its a host that everyone uses
Did you pay for it though?
no
....
im confsed
I just started learning PostgreSQL
im newbie too
in the pgadmin4 website u can see in the url theres an ip and the port
Learn how to use PostgreSQL in this full course. PostgreSQL is a general purpose and object-relational database management system. It is the most advanced open source database system widely used to build back-end systems.
⭐️ Contents ⭐️
⌨️ (0:03:16) What is a Database
⌨️ (0...
What are you implying. Are you having trouble setting it up?
mhm?
well idk
I am trying to connet it from another oc
pc*
but it didn't seem to work
async def create_db_pool():
client.pg_con = await asyncpg.create_pool(database = 'levels', user = 'postgres', password = password, host = '127.0.0.1', port = port)```
this is my cmd
Yeah because your dbs is restricted to the first machine
you need to make it available online,
you got any idea how?
like r u sure this is correct what am doing
because I think I got this from my head
most people uses VPS or dbs from providers, If you are planning to host it locally you can ,but the system needs to be online 24/7
Its is online but there is still configurations to do to make it available online
how? though
ok
when you put "host='127.0.0.1" it tells the db is on your pc
if the db is on another pc, you must give the ip of that pc
can some one say me if they use php or node.js ? https://www.lieferando.de/
Einfach Essen bestellen in ganz Deutschland via Lieferando.de! Heute Lust auf Pizza, Sushi, Chinesisch oder Vegetarisch? Wähle aus über 15.000 Restaurants.
ive just installed postgre im trying to learn how to use it, what should i do now
good question, only you can answer that tho 
@torn sphinx 1/ that's off topic (not database related) 2/ no, you can't know that just from looking at the website, sometime there might be hints, but they are not proofs (and some programmers like to put false hints)
wooshwoosh i yoink in here with a question
reset_timeout := format('SELECT %I_refresh
FROM users.ratelimits
WHERE user_id = $2'
, $1);
this gives error in PostgreSQL 11, however i wonder what would be the correct way to write this kind of statement
another attempt 30 minutes later
EXECUTE format('reset_timeout := (SELECT %I_refresh
FROM users.ratelimits
WHERE user_id = $2)
', $1);
however, it also gives errors
been stuck at this error for nearly 1 hour now
:=? Is that the famous Walrus?
:= is actually an assignment operator in PostgreSQL
but when I think about it, yeh it kinda look like it 
it's confusing because python3.8 has that "walrus operator", and it wasn't clear if it was all sql or python+sql in your first snippet
been stuck for hours to no avail
reset_timeout := EXECUTE format('(
SELECT %I_refresh
FROM users.ratelimits
WHERE user_id = $2
)', $1);
@welcome.command()
async def channel(self, ctx, channel:discord.TextChannel):
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}")
result = cursor.fetchone()
if result is None:
sql = ("INSERT INTO main(guild_id, channel_id) VALUES(?,?)")
val = ("ctx.guild.id, channel.id")
await ctx.send(f'Welcome channel has been set to {channel.mention}')
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
elif result is not None:
sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
val = ("channel.id, ctx.guild.id")
await ctx.send(f'Welcome channel has been updated to {channel.mention}')
cursor.execute(sql, val)
db.commit()
cursor.close()
db.close()
Error im getting^
async def lettercheck(bot, message):
word = await bot.db.fetchrow("SELECT letter FROM adm WHERE guild_id = $1", message.guild.id)
if not word:
word = "k"
return True
else:
word = word['letter']
return True
#some command:
@commands.command()
async def check(self, ctx):
await ctx.send(f"{lettercheck}")```
why can't I insert my text into another function?
is returning this: `<function lettercheck at 0x040CF418>`
you probably want to write await ctx.send(f"{lettercheck()}")
or even
await ctx.send(lettercheck())
no point using an f-string with something that simple
ok
hi there, i have an issue with shelve dict, im trying to save my data by creating a dict in dict
topDict = shelve.open(config.topPath)
topDict['psn'] = {}
topDict['psn']['guy'] = (0, 0, 0)
print(topDict['psn'])
# output is {}
What would be the best way to use a db in python (on a flask app) without an ORM, as I need to be using SQL purely commands
@bot.command()
async def addword(ctx, text):
with open('curses.json', 'w') as p:
json.dump([str(text)], p)```
Instead of adding to the json it deletes anything old and replaces it. Also I beleive this fits under databases since Im using json as a database 😂
you should read the JSON file into a variable, add the word to that variable, then dump the variable back out
that's inefficient, but at least it'll preserve your data
so i have a mongodb document that looks like this:
{"_id": "1", "accounts": {"email@example.com": "Example user", "random-email@gmail.com": "Random user", "points": 5}}```
and i have an email/user and want to get the points
how would i get the document that has the email in the `accounts` dict
I've found the solution by the way.
Accessing the dict with dot notation i think it's called
basically db.find_one({"accounts.{}".format(email): user})
Hello is there anything wrong with having a design like this?
The plan is that when either a product/customer is deleted, so are the license keys associated with it
Honestly you shouldn't do it like that but you could.
You probably want to flip your paper so the customer is at the top then branches out, but idk
I get the following error whenever I try to execute query that has brackets
query ```db.execute("DELETE FROM grocery_item WHERE user_id= %s", (user["id"]), )
error
TypeError: 'int' object does not support indexing
I remember having an issue like this:
Try:
db.execute("DELETE FROM grocery_item WHERE user_id= %s", (user["id"], ))
First though, look at what your DB library suggests as its protection against injections
And use it instead of %s
But smye is right, it expects a tuple
@errant sable
placing the comma inside the parentheses fixed the issue
guys, i'm new to databases, so i'll ask a tip, i am making a game and i want to know the following:
my game is a space game (classic) where you have to mine asteroids and stuff, you have 4 races, and each race has its own balancing.
it's better to create a class User AND a class Player
or make them one
?
hello
whoever knows mongodb
do you know how to make an index required
so that if your try to insert a document without that index it wont allow it
please mention me if you know the answer to this
I usually query characters like:
SELECT * FROM MyTable WHERE name ~* "blabla";
However I need to query multiple ones, how can I achieve the same effect as ~* but with a IN (...) query? If it's possible. Related to this, sometimes ~* won't give me a result, is it possible to know which one of the item on the list gave no result from the IN query condition?
query = "INSERT INTO books(title,isbn) " \
"VALUES(%s,%s)"```
can someone explain what VALUES(%s, %s) means
%s of course means string, aint it?
Yes. Placeholders for string values
Check the DB library you're using for what you should be putting there instead of %s. That's liable to SQL injection
%s can be a fine placeholder, just don't use % after the string to replace it, but pass the params to the sql call
i prefer "named" and "pyformat" styles if they are available, as it's more legible and easier to update
not entirely sure but i can say, you'll want to swap to an async db library (not sure what there is for mysql) so that your queries don't block the bot's process
also @solid void i'm curious in case you know the answer, will all libraries support any of those or only some?
when i got into sqlite ages ago everything i saw said sqlite only supported qmark and that others only supported one and not others as well or something like that
i think libraries are free to implement any number of these, so you'll want to check the doc of the specific lib you want to use
i'm happily using named (:variable_name) with sqlite these days
ty
hey guys, is there any site or source of "ready to use" models for databases?
python's cookiecutter library has some good stuff, I used to use this template a lot https://github.com/cookiecutter-flask/cookiecutter-flask
https://gyazo.com/19ff817e1a325fa45c1d298992290e6a
im getting this error
announcement_type = request.form.get('xpmsgs')
lvlup_msg = request.form.get('lvlupmsg')
db.execute("UPDATE xpmsgs SET whereto = %s AND levelupmessage = %s WHERE guildid = %s", announcement_type, lvlup_msg, int(id))
this is how im running it
https://gyazo.com/920c220b5bef79eff601e869ea38ef5d
and this is my table
@torn sphinx placeholders expect a tuple
what place holder
all my other queries word fine
Give it a tuple and lmk if it works
ok
Just add parentheses around them
nope, same error
announcement_type = request.form.get('xpmsgs')
lvlup_msg = request.form.get('lvlupmsg')
xpmsgs = db.execute("SELECT * FROM xpmsgs WHERE guildid = %s", int(id)).fetchone()
if xpmsgs is None:
db.execute("INSERT INTO xpmsgs VALUES(%s, %s, %s)", (announcement_type, lvlup_msg, int(id)))
else:
db.execute("UPDATE xpmsgs SET whereto = %s AND levelupmessage = %s WHERE guildid = %s", (announcement_type, lvlup_msg, int(id)))
I doubt it's because its not in a tuple
That's been it for every other syntax error for me in every DB library I've used for python