#databases
1 messages ยท Page 54 of 1
Well does it require await anywhere?
nope
Then it doesn't have coroutines
you speak the truth
And is synchronous
Id try it
alright
i'll try this
updating the pipenv
How do I connect to a DB using a sslcert with asyncpg ?
How do I give the ssl keys and certs with this ?
@dull scarab
Having this error
pg_hba.conf rejects connection for host "94.126.119.20", user "koinbot", database "koinbot_data", SSL off
I have this now
halp me ๐ฆ
post your pg_hba.conf
cur.execute("SELECT sum(salary) from salaries where teamID is '{0}' and yearID is '{1}'".format(team, year))
so I'm running this query for a list of years -- how do I do that as a single query?
probably using group by
how would that work?
where are your years coming from?
is it in another table?
no--a provided list
well then you have to just loop the list in python and run the query on each I think
I thought you had it in a table
actually maybe you still can
damn yea just looping is slow
something like where yearID in(all the years) group by yearID?
Fair warning I am rusty with SQL. I tend to figure stuff out by testing so it's hard to help when I can't test the sql
what would all the years look like?
cur.execute("SELECT teamID, yearID, sum(salary) from salaries where yearID in (2012,2013,2014) group by yearID".format(team, year))
no dice...
give me a moment then
thanks so much
Well here is a basic example that works https://www.db-fiddle.com/f/9BpLHbYF5iMKTbsSspkqdc/0
An online SQL database playground for testing, debugging and sharing SQL snippets.
idk what your actual schema is like though
it doesnt quite make sense for me that there'd be multiple rows for the same teamid and year though
I'm guessing you have individual people and also store their team and year there?
in which case it sounds like it isnt normalised
doesn't seem to work ๐ฆ
what's normalized?
even this returns a single null: SELECT sum(salary) from salaries where teamID is '{0}' and yearID in(2004, 2005)
what is your schema like then?
honestly it was an import. the DB is super small--maybe I can just share it here?
yeah cool
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
def get_annual_salary(connection, team, year):
cur = connection.cursor()
cur.execute("SELECT sum(salary), teamID, yearID from salaries where teamID is '{0}' and yearID in(2004, 2005)".format(team, year))
# cur.execute("SELECT sum(salary) from salaries where teamID is '{0}' and yearID is '{1}'".format(team, year))
rows = cur.fetchall()
print(rows)
def main():
database = "path to db"
# create a database connection
conn = create_connection(database)
get_annual_salary(conn, 'WAS', 2005)
# get_teams_list(conn)
# get_team_record(conn, 'COL', 2008)
if __name__ == '__main__':
main()
here's what I'm working with so you can really emulate it lol
ah it's cause the year column is a string not an integer
this works sql SELECT teamID, yearID, sum(salary) from salaries where teamID in ('ATL', 'BAL') and yearID in ('2004', '2005') group by teamID, yearID;
@rancid bronze also try to avoid using string formatting for your queries. Its vulnerable to sql injections and could be a nightmare
Use prepared statements instead where you replace the values you need with placeholders, i think its ? In sql and pass your variables in order to execute
Sqlite*
You can use any placeholder despite ? in the documentation
Hey guys, kinda new here. I was wondering if someone could possibly mentor me or guide me on how to become a Data Engineer
The boot camp I've found in my city are a bit on the expensive side also I'm working full time so it's hard to invest that time into a boot camp.
There really isn't any program for mentoring or 1 on 1 tutoring offered here. You're welcome to ask questions and discuss the language though. You might be looking for #data-science-and-ml as well. Maybe a few there can guide you towards resources to learning what you need. @sick nacelle
@dull scarab Ahh okay. Well I'm in a bit of a predicament. I was just wondering if someone could give me some advice on what I should do in terms of career goals as well.
I'm not in the field myself so I can't help you out there. But for general data-science questions you can ask in #data-science-and-ml , and if you need career / education advice try #career-advice
Got it thanks!
Hey I donโt know where else this would go but can anyone how you find the api to a website? Where are they usually?
any suggestions for a key-value store that allows wildcard searches across tuple keys?
e.g.
query (1 * *), get
(1,1,1)
(1,2,3)
(1,0,213) etc
hey there,
using sqlite, is there any way to increase a value by one while inserting, when another row ould have the same value?
im trying to save querys i get from users, and how common they are.
so far i have a table like this CREATE TABLE querys(id INTEGER PRIMARY KEY, query TEXT, amount INTEGER)
and i would like to increase amount when the query value is the same.
how would i do this?
Hey, is it possible to make SQLAlchemy interpret None as 0 for One-to-Many relationship?
I'm trying to order a table of Products.
I want to be able to order by the amount of Offers a Product have. I can do it like:
session.query(Product).join(Product.offers).group_by(Product.id).order_by(desc(func.count()))
But this way the query only returns Products that have one or more offers. I want to be able to query by ascending order including Products that has None offers.
Any way to do this?
guys see i have this pdf
now i wanna convert this to excel sheets for ease of use
how to do in p
py
https://github.com/pdftables/python-pdftables-api Seems to be a good guess. I have not used it though
Python library to interact with https://pdftables.com API - pdftables/python-pdftables-api
Accompanied by a blog post about it https://pdftables.com/blog/pdf-to-excel-with-python
Hello guys,
I have run into an unicode problem with postgres and python3. Anyone has any experience with that?
UnicodeEncodeError: 'ascii' codec can't encode character '\xdc' in position 448: ordinal not in range(128), while every encoding setting seems to be alright
nevermind, after 2 hours I'm just realizing that my system has not all necessary locales installed
@plain oxide strange man
how do i set a password for a database using sqlite3
ive seen in other languages its been conn.SetPassword("password") but doesnt seem to be it in python
oof i have learnt
@obsidian leaf sqlcipher
what
Is it considered a criminal offense to use RethinkDB over Postgres?
I want to use RethinkDB
you should probably read about the pros and cons of these databases
@cerulean harbor There's a reason Postgres is industry standard
Also you were trying to use MySQL
But you say Postgres is bad?
no
good luck adding column on mysql table with millions of records
still learning the fundamentals i guess. could use a little guidance. http://pasteall.org/1363532/python
Guys. I wanna do an Update query
and update values in a db
using vars
is the syntax this way
Update table SET (column1, column2) = (var1, var2);
because Pycharm says there is an error
What db
UPDATE table
SET column1 = ?, columns2 = ?
WHERE some_condition``` and pass the variables in order to execute
is the general format, but the placeholder ? may vary based on db
So e.g
cursor.execute(
"""
UPDATE users
SET money = ?, transactions = ?
WHERE id = ?
""", new_money, len(transactions), user.id
)```
The where just specifies what to update
if you exclude where it will update the entire table
alrighty
What are you trying to update?
executemany ? what is this ?
it does many queries with a list of variables
i'm a good guy newb with DB stuff and python
so you'd setup a list of entries to update and pass the list with a query and it handles it for you
Lemme find the thing, I don't remember the exact syntax
sure ๐
postgres
uh, which lib?
psycopg2
I tried Asyncpg
but couldn't establish a connection
because i'm using ssl certs
and couldn't find a doc with an ssl example working
So it expects a query and a list of tuples for parameters for the query
cursor.executemany("QUERY", [(1,2), (3,2)] )```
This is how I do currently
So using the example query from earlier..
parameters = [
(55, 10, 245245), # money, transactions, id
(100, 23, 242342)
]
cursor.executemany(
"""
UPDATE users
SET money = ?, transactions = ?
WHERE id = ?
""", parameters
)```
You don't have to loop through your entries and execute, just format them as to somthing like thispy params = [ (params1), (params2), ]
then execute many
you can keep that format, but you gotta pack it into a list of tuples
then pass that list with a query at the end when the loop is done running
I see
I have this error with the first solution you provided @dull scarab
And with the executemany
this means I have to do 1500+ params tuples
It's that or 1500 queries, execute many is probably more optimized for that many
i dont think postgres has ? placeholders?
it uses $1, $2, $3 ... iirc
Yo, can anyone here help me out with some basic SQL?
Database theory I mean
Functional Dependencies like
A->BC
is this correct ?
I don't recognise the DBMS you're using but it looks like your missing the as ... part of the with
DBMS ?
I setup the connection and it's working
but the output of this part of code is
I just want the number of rows returned
Hello, what's the easiest way to connect a mySQL db to a HTML form?
php
Okay, I never used php, would you mind to tell me if what I'm coding is wrong or not?
@fringe plover you would use a framework, like Flask or Django with Python. In the framework you make things called models. The HTML form will make an instance of that model and then it will be stored in db.
Oh okay. Is it easier to use than php ?
PHP is a programming language specifically designed for web development while what import error said are frameworks for web development for the python programming language
So if you don't want to learn a new (horrible) language yes
Okay for now I just made smth that sends the data to my email and answer myself to all the emails. But I'll look into that, thanks
PHP is awful, as it a combination of lot of languages
u'll find things coming from C, perl and java
Most languages are combinations of other languages
Is anyone familar with mysql.connector?
I got many connections run with it. But know I have to work with SQLExpress by Microssoft. So Hostname looks something like SERVERNAME\SQLEXPRESS
I kn ow I have the right credential and the server is running because other clients have active connections. Butwith mysql.connector I always get the error:
mysql.connector.errors.DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'SERVERNAME\SQLEXPRESS' (10061)
using a mysql connector to connect to an mssql server is only going to cause you issues.
Hi! I've a question about how to approach translations of terms on a database. I read there are different options. I chose the one that consists of adding translation_id in every table with terms to be translated (product_id, product_name, translation_id), one "translation" table (translation_id, language_id, term), and a table for language specs (language_id, language_code, language_name). However, how can approach fields headers translation? (I'm returning them on API requests). Thx!
Hi ! In SQLITE, when inserting around 19000 rows, it takes quite some time to do it. are there any tips to make it faster ? Should I look for indexes ? Or it could come also from my latpop HD ? thanks.
HDD speed matters there
ok thank you
@primal idol Inserting that many rows should not take long, can you post the code of how you are inserting please
In fact, I was wrong. I have a file which is a list of 75000 dictionnaries. And so far, I was extracting each dictionnary and doing an INSERT in sqlite3/python3.7
I am thinking about replacing by executemany but it seems it is accepting only tuple as parameters (need to check more)
building up a list of parameters and using executemany is probably a bit faster
it just takes the parameters you give normaly, but in a list```py
normal
execute("query", (param1, param2))
execute many
executemany("query", [ (param1, param2), (param1, params2) ] )```
I will try. May I ask what is the difference in the background ?
opening just one connection instead of open/closing one
for each insert
?
Mostly that each insert has quite a bit of overhead which you have to go through for every execution when doing it one by one
while executemany setups this overhead for all of the queries
as far as i know, at least
ok thank you. I just need to replace my list by tuple.
@dull scarab @primal idol Its because in sqlite with python, you have transactions (transactions are a thing other than python just to be clear, just the way the standard sqlite3 lib handles them)
Each insert will have a new transaction, which will slow things down a lot
Also 75000 inserts shouldn't take long
Hi everyone! working on some views for a postgres db. Essentially, got this intermediate table with the columns patient_id, disease, mortality, patient_id is integer pk, disease is a string name of the disease, and mortality is a string that can either be ALIVE or EXPIRED. I am grouping by disease names. I want to get mortality percentages for the different diseases. I am able to get a count for all admissions, however I am not sure how to get a count of the rows where the mortality = EXPIRED.
my query looks like something along the lines of
select
disease,
count(patient_id) as n_adm,
<<count(patient_id where mortality = EXPIRED) >>
from blah
group by disease;
I know the second count statement in the pointy brackets is incorrect; I'm just trying to illustrate what I'm trying to do
Does this make sense? Am I being dumb? I would appreciate any advice! Thanks in advance!
sqlcmd = '''CREATE TABLE IF NOT EXUSTS {}(var REAL, var2 TEXT, var3 REAL, var4 REAL)'''.format(self.support_channel_count)
sqlisrtcmd = '''INSERT INTO {}({}, {}, {}, {})'''.format(self.random, authorid, name, age, score)
con = sqlite3.connect('botdata\\databases\\userinfo.db')
c = con.cursor()
c.execute(sqlcmd)
c.execute(sqlisrtcmd)
Ignoring exception in command tnew
Traceback (most recent call last):
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(args, **kwargs)
File "c:\Users\ahdba\Desktop\Availium Solutions Bot\Commands\TicketSystem\tnew.py", line 198, in tnew
c.execute(sqlcmd)
sqlite3.OperationalError: near "EXUSTS": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(ctx.args, **ctx.kwargs)
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "EXUSTS": syntax error
Help please ๐
can you print the queries before executing
@dry patio CREATE TABLE IF NOT EXUSTS
EXUSTS -> EXISTS
oh crap lol
Now im getting this error
Ignoring exception in command tnew
Traceback (most recent call last):
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "c:\Users\ahdba\Desktop\Availium Solutions Bot\Commands\TicketSystem\tnew.py", line 199, in tnew
c.execute(sqlisrtcmd)
sqlite3.OperationalError: near "235973302898655232": syntax error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(*ctx.args, **ctx.kwargs)
File "C:\Users\ahdba\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: near "235973302898655232": syntax error```
to start, you shouldnt be using format strings with sql.
do something like:
db.execute( 'SELECT * FROM user WHERE username = ?', (username,) ).fetchone()
thats how you get sql injections
next is self.random a random number? if so you are doing INSERT INTO 235973302898655232
which i didnt think would be a syntax error but it shouldnt be a table name
random is letters the number is for authorid
i think your insert syntax is wrong then:
'INSERT INTO user (username, password, locked) VALUES (?,?,?)'
also its super super important that you stop using format strings for your sql
you magically avoid a ton of security issues and bugs if you switch to statements like these instead
@dull scarab Thanks for the advices yesterday. In term of performance, it is day and night execute versus executemany
๐ Nice to hear
i explained why...
Is there a way to pass a string into something like this
crsr.execute(sql_command, firstbc)
When I have firstbc = box1, crsr.execute fails saying that sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.
Which makes me think it's taking each character in firstbc as a statement
When I pass it as a tuple it doesn't work
what about it "doesn't work"? do you get an error? does it launch a nuclear missile?
Oh, well it's working now...
Yesterday it just wasn't reporting anything back, which made me think it wasn't getting inputted properly
How can I search if an entry exists in the table based on a variable?
Right now I have sql_command = "SELECT itemName1 from inventory WHERE itemName1 =?"
But it could be in itemName1, itemName2, itemName3, or itemName4
you can use OR in your WHERE statement
eg WHERE itemName1 = ? OR itemName2 = ? etc
I have not figured out how to pass a single value to multiple ? 's though without passing it in multiple times yet
Yeah that's kinda what I have now
Right now I just run this for itemNameX
crsr.execute(sql_command,(firstbc,))
if crsr.fetchone():
sql_command = """SELECT itemLocation from inventory WHERE itemName1 = ?;"""
crsr.execute(sql_command,(firstbc,))
location = crsr.fetchone()[0]
print(location)
http://www.sqlitetutorial.net/sqlite-case/ might do what you want.
SELECT itemLocation
CASE
WHEN itemName1 = ? then itemName1
WHEN itemName2 = ? then itemName2
END
FROM inventory
WHERE
? in (itemName1, itemName2)
something similar to that
an standard SQL INSERT statement with dynamic values lik
INSERT OR REPLACE INTO table values (?, ?,?,?), data
How should I handle when I have around 75000 tuples in the field data and the length of my tuples is not always the same.
In the above case, it is 4. but could be 9 or 1.
@primal idol You can insert lots of data in a single insert.
INSERT INTO table (col1, col2, col3) VALUES (...,...,...), (...,...,...) ....
But ensure that every tuple is the same.
Either make them the same
Or run an insert for each tuple and specify the columns
But I'm not sure how you want to do an OR REPLACE
UPSERTs are notoriously tricky
That was my issue, length of tuples are different. And i cannot run a single insert for this amount of lines
Replace is not inportant for now. Just an insert with executemany.
Perhaps fix the tuples with missing columns by replacing them with tuples with NULL in those columns
That is what i was thinking but i was hoping for something different ๐
A trick i didn t know
If the tuples are the same it is still not an executemany, just a single statement. A single insert can create multiple rows
P.S I'm new to Discord - Did you delete my messge?
It turned red.
Then disappeared
Weird
What is your input data stored as?
List of Dict or List of Lists or List of Tuples?
List of dict with nested dict inside
Regardless... Something like
for i in range(len(data)):
row = data[i]
new_row = fill_in_the_blanks(row)
data[i] = new_row
I ll try ! Thanks
And then add an enhancement - for example let the fill_in_the_blanks() function return None if the row is correct.
So no update required if the row is already correct...
Using None as an indicator that the row does not need to be updated....
for i in range(len(data)):
row = data[i]
new_row = fill_in_the_blanks(row)
if new_row is not None:
data[i] = new_row
I did it a bit differentlt but takes for help @marsh ferry
@marsh ferry red means it failed to send, you get a limited time to pick retry from the โฎ menu, then it goes away
Could I get someone to test something I wrote?
@barren wave no lol
Is this chat for SQL Databases?
anyone here good with json?
Yes @torn sphinx
Json is probably web development
I would say it depends on the context, you can have JSON in SQL DBs
like postgres supports JSON and JSONB field types, and those two require additional knowledge of built-in functions in order to query for it.
or you can use nosql database which is a sort of a json most of the times.
It's also not like this channel is exclusive to sql... stuff like tinydb and mongodb are probably on topic for in here
Hey, I have a list stored as a string in my database, but how can I make it an actual list when using it in Python? Is there a way to transform the string into a list?
So, the list is basically like this now: "[513470169419481110, 513470171159986191]"
yeah you could use json module of python and do json.loads( "[513470169419481110, 513470171159986191]")
Thx!
'[513470171159986191] WHERE guild_id = 510853998337589259' at line 1")```
query = f"UPDATE moderation_config SET staff_roles = {staff_roles} WHERE guild_id = {ctx.guild.id}"```
Someone any idea why I'm getting this error? staff_roles is an list by the way. Don't know if it has something to do with that.
because SQL is not a friend of lists afaik?
at least not lists like you want them
and as SQL doesnt know what a [ means it throws a syntax error
@torn sphinx
@torn sphinx don't use f-strings for sql queries. ```py
query = 'UPDATE moderation_config SET staff_roles = ? WHERE guild_id = ?'
cursor.execute(query, (staff_roles, ctx.guild.id))
or possibly
cursor.execute(query, (json.dumps(staff_roles), ctx.guild.id))
๐ thx
any cool kids who can help me with mongodb by any chance?
or should i be going to a #help channel?
basically, i've got a collection called 'users'
and under every one of the documents, there's an array called 'bets'
and this is an array of documents
so, like, embedded documents
and say there's a user, let's call them 'linky'
i want to find a specific bet from the user 'linky' with a specific _id
how does one do that?
okay solved it by doing stuff, if anyone actually reads this and cares just ping me but you probably figured this all out already k bye
c.execute("SELECT * FROM admins,teachers,students WHERE username LIKE :username",{"username":(str(self.username)+"%")})```
gives me an error
```py
sqlite3.OperationalError: ambiguous column name: username```
any ideas on what it means by this?
whats wrong with it?
Try without it
Either the colon shouldn't be there, or it can't find someone like username
i believe dbapi requries you to have a : for named parameters
try a different param name maybe?
One way is By combining str.format() and %s .
So:
table_name = 'users'
values = [20]
sql = 'SELECT * FROM {table} WHERE age = %s;'
cur.exec(sql.format(table=table_name), values)
Note that in the above the cur.exec() AUGHT TO do the escaping on the values. This will not happen automatically with whatever is in table_name so that needs to be from a trusted source, otherwise you open yourself up to all kinds of sleepless nights.
I say AUGHT because you must check that your db package implements that correctly.
ping @dry patio
.format is horrible, dont do that
the issue sqlite is having that the username field could refer to any of the three tables
you will need to do something like
admins.username LIKE :username OR teachers.username LIKE ...
what is from admins,teachers,students
is it a join? a union? (EDIT: It's a join, but it looks like you want a union)
@obsidian leaf what exactly are you trying to do?
fixed it by doing this but thanks anyway ๐ py c.execute("""SELECT admins.username FROM admins,teachers,students WHERE admins.username = teachers.username AND admins.username = students.username LIKE :username""", {"username":(str(self.username)+"%")})
what im trying to do is generate a username ie. firstinitial + lastname and if thats already taken then add a 1 on the end of it - then a 2 if thats taken
making a school database
ok but
why three tables
why not one table for all users, and then a column to say if that user is an admin, a teacher or a student
cus student stores some other things like year group - behaviour points, achievement points
you should have a base user table and then a student additional data table
also that query you fixed it with doesn't look right
yea i thought so xd
made it late last night and i made a note to check it next time i go back to it
so how would you say i should add year group, points etc. on?
probably a students table with the same primary key as the users table
ok yea
and then any student will have a row in both tables and you can just select * from users inner join students on users.id = students.id
whether id is a numeric id or a username etc
that way users.username can be a primary key or a unique constraint
to stop you from accidentally creating two users with the same name
thank you ๐ ill make the changes soon
So i want to create a table of people and cars, i created a table that map the car to the owners but i also want to have a record of the purchase order (so i can sort it by obtain order)
i am using sql_alchemy btw
you would prob want three or four tables then. One for people, one for cars, one for ownership, since a car can have more than one owner such as primary driver and occassional driver or if someone cosigns a lease etc, then a purchase order table
ownership could be car_id, person_id, type_enum where type_enum in some way tells you how they own it like if they are the primary owner or cosigned etc.
in theory the purchase order table could also be the ownership table
looks like you have the first three but youll have to do something similar for this last table. I would expect a PO typically to be a po_id and a a 'list' of items on the po. so it may also need an intermediate table to relate it to cars and also one for people
if you want one owner per car and one po per car then you dont really need the intermediate tables
Hi there, I am having a problem with using sort_values() with a height-related feature. It is a dtypes == 'object' and I want to keep it a \d-\d{1,2} format. I can't seem to have it sort in an ascending order. What steps can I take to solve this issue? Thanks in advanced.
anyone here a bit smart with .MDF and .LDF files?
specifically the garbage fire that is MSSQL
Hello guys - Would anyone be available to help at all?
!t ask
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.
โข 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
โข Keep your patience while we're helping you.
You can find a much more detailed explanation on our website.
Thanks ๐
I have a discord bot I have created which is hosted on my dedicated server - the dedicated server does not have mysql on it (doesnt need it)
The bot is made in python and connects to a remote mysql database. I am randomly getting an error which is shown next
I have asked the hosting server providers the database is hosted on to check logs/firewalls etc.. and they are advising they cant see anything.
I am struggling to find the problem and where to fix it. Not sure if its my dedicated server thats dropping the connection or if its the remote mysql server kicking it out
Random bypasser here without a direct solution to your problem but wondering if it can be designed around -- if the connection is inconsistent can you just write a try/except and a loop until it gets the data you want?
what I normally have to do is kill the robot and start it back up again
but its such a tiny script - not sure if the script is maybe not keeping the connection alive
so how often does it happen?
That's not bad. Just write code to handle the exception when it happens? I mean, if it was some huge enterprise system that's not really a good answer, but if this is just a small app I wouldn't worry about making sure the connection never ever fails?
the host of the mysql server has just advised the wait_timeout is set to 120
is that possibly the issue
Seconds? As in two minutes? Hm
So if you don't make any interactions
within 2 minutes
it drops
yep seconds
yeah, that makes sense
So you need to send some keepalive query?
ill run the bot
and give it 2 mins
ive sent a query through it - just waiting for 2 mins then ill try another command to see if it kicks it out
seems solid
So, like
idk there's a lot of ways to keep a connection alive but
maybe async/await a 110 second sleep?
would the best way be sending a ping every 60 seconds
then run a dummy query as a keepalive?
hm idk if you can, like, forge a keepalive by pinging, think you need to actually interact with that somehow. There might be a keepalive setting in the actual connection object?
if you could just set that...
I'd google it -- python mysql keepalive
just close the connection after you're done with it and reconnect
use a connection pooling library, it should take care of that without you churning too many real connections
I wouldnt have a clue how to add that if I'm honest - a friend online built most of it lol
but I can take this feedback to him ๐
just mysql I think
there are about half a dozen regular mysql libraries for python, which one are you using
oh, mysql.connector
Thanks @patent glen
list_as_string = str("'1', '2', '3'")
cursor.execute("SELECT * FROM [dbtable] WHERE [id_] in (?)", list_as_string)
Anyone got any idea why this wont give any results?
using pyodbc on msql
nvm, turned the whole query into an fstring and made it work
You should let the DB connector format the arguments.
What DBMS uses [tablename] ???
@gloomy zephyr EDIT sorry wrong @, I don't even know why your name comes up when I type @merc
the correct way to do that is something like py thelist = ['1', '2', '3'] question_marks = ','.join('?'*len(list)) # ?,?,? query = f"SELECT * FROM [dbtable] WHERE [id_] in ({question_marks})" cursor.execute(query, thelist) # or (..., *thelist) if you have other arguments
@near anchor
I trongly disagree @patent glen. You should let the connector format the arguments.
See the section on parameters here:
?
@marsh ferry note that what I used an f-string for was not the arguments themselves, it was a list of question marks.
There's no other way to do that
most databases do not allow you to pass in a list or sequence object as a single parameter - i'm not aware of any that do
Really? with Psycopg2 formats lists as PG SQL "array"s I assumed they all do that.
sqlite3 doesn't even allow you to put a single parameter after in at all, it's a syntax error
Yes but I think = ANY(%s) is more ANSI-SQL compliant
and if you try to pass a list or tuple into a query that's not a syntax error, you get sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
I've only worked much with PG so I may be wrong.
sqlite doesn't support ANY, I don't know if it's ANSI or not, but it's still got the same problem - it expects a query or a list, and you can't bind an array-as-single-paramter for a list in most connectors
it's been my experience on oracle and ms sql as well, though that wasn't in python
This kind of thing is coming up more and more in my work now so time for me to go educate myself a bit more.
anyway, pyodbc doesn't, see https://github.com/mkleehammer/pyodbc/blob/d8e5537c04dae715b72c364fbffe2b2eb4810e2d/src/params.cpp#L37 for the supported types
It's cool that postgresql can, but that's a very uncommon feature in my experience
im trying to do something like that but i get this error
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OperationalError: no such column: user.id
nvm got it
what did you end up with?
# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print(c.fetchone())
reading the python docs on sqlite3, why is the first one insecure? it doesnt go into much detail as to why
I'm guessing that the second one protects against SQL injection.
lol ah sql injections, another thing i'll have to read about 
So the way I do embed rn, is create a new one when ever i want to send an embed, I want to create a function that i call when i want to send embeds but I can't think of a way of doing that.
is this a #databases question?
Hi guys, I am new to the server, I need help with a problem with sqlite
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.
โข 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
โข Keep your patience while we're helping you.
You can find a much more detailed explanation on our website.
code
I don't get any error in my code but it fails to update the table.
I can insert a row without any problem
maybe where isn't matching anything?
I use a print before and I am getting the title right. I am going to double check
@lunar cave are you just overwriting the same data?
the row is null.
It's sqlite.
It should be getting committed since the connection is being used as a context manager.
I am using the regular sqlite lib
import sqlite3
I've been able to create and work with the table, except the update
@pure cypress yeah im not too familiar w/ the dbapi ins and outs
ive been burned by that before
specifically in sqlite iirc
I found the problem. thank you.
since my I had overwritten the value of my title in the middle of my code.
the sqlite was not able to find an avaliable title because I stored a different title a few lines before. I need to be more careful assigning the variables.
thanks mark for mentioning the where in one of your solutions
You're welcome.
Hello.
I am rather new and have taken on a small project to help me understand what I am learning and to keep my motivation up for learning.
I have gotten a medium sized json response and used json.loads to turn it into a python list.
Could someone help me with how to insert the values from this rather large list into a DB? I dont think I am googling right because all my results take me to stack overflow where most of the questions I either dont understand yet or they arent what I am looking for.
@vital yarrow you can try https://docs.ponyorm.com/firststeps.html one off easiest ORM's
Thanks IbeeX, I will read up on that now.
@solid pine In short it means that a user could insert their own sql strings into yours if you concatenate the strings like py execute("SELECT * FROM users WHERE name=" + name_var +";")
or
execute("SELECT * FROM users WHERE name={};".format(name_var))```
So if `name_var` is an non sanitized variable that user could input something like
`x or 1 = 1` (I believe would work, without testing it)
This would make the query
```py
execute("SELECT * FROM users WHERE name=x or 1=1;")```
You can probably see why this is an issue.
the -- shown in that comic makes the rest of the line a comment @solid pine
in case you were wondering
Is anyone of you experienced with SQL-Express connections? I worked with mysql-connector and it realy works fine to connect to an mysql-server. But ms sqlexpress seemsto be a bit different to connect with.
ok I have it. FYI
If you use SQLEXPRESS, you have to use pyodbc. MySQL-Connector doesnt work with it.
You need to find the number of orders per customer. One order = one invoice. Invoices has a foreign key on CustomerId.
You can do a query on Customers with a join on Invoices using CustomerId
Since you need the total, you need to use the sum() aggregate function.
This is MySQL. Why does this work?
create table role (
roleCode varchar(255) not null primary key,
displayName varchar(255) not null
);
insert into role (roleCode, displayName) values ('admin', 'Administrator'); -- okay
insert into role (displayName) values ('ShouldNotWork'); -- but it does work
I am trying to do a python telegram bot connected to a sql database, what module should I use to connect with the sql database?
I am using this module https://github.com/python-telegram-bot/python-telegram-bot
I am using a MySQL database @broken linden
then use either MySQLdb or sqlalchemy if you want an ORM
i think pymysql is more active https://github.com/PyMySQL/PyMySQL
okay so I'm trying to figure out how to get an SQL query written using SQLAlchemy so that I don't have to rely on chunks of raw SQL
SELECT A.*, MAX(ledger.created_at)
FROM accounts A
JOIN accounts_ledger ledger
ON A.id = ledger.account_id
WHERE ledger.successful
GROUP BY A.id;```
@ionic pecan if you want an ORM pymysql isn't an ORM
and where did i imply that?
i think pymysql is more active
You are comparing
So that is where the implication came from
compared to mysqldb
I see, miscommunication then
Hello, i'm using sqlite and i'm trying to update a column (which is currently null) with the count of the number of duplicates found in another column.
hope the formatting below works
Update GrimWares
set
ItemQuantity = (Count(ItemName) from GrimWares);
i used Py formatting even though it's SQLlite ;o
i get the error: near "from": syntax error:
I wonder... What is the simpliest way to work with MySQL? I mean is there some sort of the most basic connector or something.
Hey folks, I have a table named groups with the following columns:
groupname, gpassword, gid
I would like to add a column called members
but it needs to contain multiple users
im using sqlite3
found an answer - just going to create additional columns member1, member2, member3
recommend me a high perf timeseries store
@glossy ermine do i need to add just the select amd the group by into the query? i have no where clause requirements.
@glossy ermine ya i agree was asking if its just the select or I'll need the group by in the query aswell right?
Hi guys ! I was asking myself
how would u handle Ctrl + Z & Ctrl + Y ?
storing all modifies in a database as a diff format ?
a size-limited database where u delete first index and add an index when it goes to limit ?
also is it possible to store an array / dictionnary in database ?
'cause i want to make the diff more efficient by putting lines as indexes
https://redux.js.org/recipes/implementingundohistory not database specific but covers the most common state management patterns to implement undo/redo
$ sudo service mongodb status
โ mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2018-11-26 13:05:02 UTC; 3s ago
Docs: man:mongod(1)
Process: 1556 ExecStart=/usr/bin/mongod --config /etc/mongodb.conf (code=exited, status=100)
Main PID: 1556 (code=exited, status=100)
systemd[1]: Started An object/document-oriented database.
systemd[1]: mongodb.service: Main process exited, code=exited, status=100/n/a
systemd[1]: mongodb.service: Unit entered failed state.
systemd[1]: mongodb.service: Failed with result 'exit-code'.```
I was trying to run mongodb but it isn't starting
It used to run and function properly btw
@me please ๐
can someone help me in #help-croissant
hmm
@faint pilot ive heard that SQLite is pretty good for beginners
np
sqlite is a standalone database engine
its great. its portable, high quality software, performant enough to use it at work, no need to mess around w/ setting up servers and authentication, nice extensions like jsonb
oh and python ships w/ a sqlite3 package
hmm just lookin for something that ik how to put together with digital ocean
sqlite is probably easiest
its only weak point is concurrency. if you are expecting more than a few users writing data at the same time you'll have to be careful
whereas mysql and postgres are much more robust in that regard
the error codes help with debugging
@hearty iron
Having trouble trying to figure out the best way to expose a list of posts to the client for pagination purposes
Is it a bad idea to send all posts to them as opposed to sending x at a time?
how much lighter is SQLite vs Postgres?
sqlite doesn't require a separate server process, it's managed from within the program connecting to the database
๐
i shall use it for my small project then
(got postgres installed already but sqllite would probably be easier to distribute to my server)
Hey following on from a question yesterday:
SQL Lite 3 question
Update GrimWares
set ItemQuantity =
(Select iq.ItemQuantity
from GrimWares as it
JOIN (Select ItemName, Count(ItemName) as 'ItemQuantity' from GrimWares Group By ItemName)
as iq ON it.ItemName = iq.ItemName)
if i do this sub-query on it's own, each row has a different number for the ItemQuantity, if i run this query as part of the update and set it returns '2' for each row for the ItemQuantity column?
I have a discord bot and I've got my rudimentary system working atm so that I can recall a specific case (kick, ban, etc) by a number using a massive dict. I would like to move this into a database of sorts. Wondering if I should use JSON or SQL?
If SQL would be better, does anyone have a good guide to follow because I don't fully understand how to use it?
Depends on the size and activity of your bot
If it's for personal use on a server or 2 a json would work.
You could use sqlite as well for a small amount of servers / activity, and postgres for larger bots as it's probably among the best alternatives for higher traffic
I haven't take this myself, but code academy usually have good courses https://www.codecademy.com/learn/learn-sql
Hi, I am using PyMySQL for my telegram bot, it works fine, but after some hours I am getting a pymysql.err.InterfaceError: (0, '') error in every sql query, I don't know what to do to fix that.
hell no dont use json for random access imo
theres no reason not to use sqlite
sql isnt hard to learn and sqlite is pretty much the simplest sql experience possible nowadays
I am using TinyDB to create a JSON Database holding basically an action log for my discord bot. It looks like, python {'_default': {}, GUILD_ID: {INDEX : {USER_ID: {CASE NUMBER : REPORT (string)}}}} I was wondering if I could remove the INDEX parameter so my dict would be formatted GUILD_ID > USER_ID > CASE NUMBER > REPORT but I am very new to JSONs and databases so I do not know how to accomplish this or if it is even possible because I have searched for ~10 mins and nada
And I am not using SQLite because the application for this bot will be very slim - currently just mastering d.py
from what I can tell its built in '1', '2', etc
Question, I have joined 3 tables, showing stores, the products they sell and their prices, how do I list all the products of stores that have any products between 10 and 50 dollars? Not the products that are between 10 and 50, but if a store has any between 10 and 50, list them all
@lofty quest you can have it like this
{
"_default": {},
"Guilds": {
GUILD_ID: {
"Users":{
USER_ID:{
"Cases":{
CASE_NUMBER: REPORT
}
}
}
}
}
}```
and then can access REPORT as `data['Guilds'][GUILD_ID]['Users'][USER_ID]['Cases'][CASE_NUMBER]`
It will make sense to you after you save a lot of data and have to get stuff from each thing and something similar.
Like getting all the reports from this will be easier!
You can get it easily by using nested for loops
check1 = c.execute("SELECT * FROM verifiedpeople WHERE id=:id", {'id': int(after.id)})
check2 = c.execute("SELECT * FROM verifiedpeople WHERE id=:id AND verified=:verified", {'id': int(after.id), 'verified': 'No'})
if int(after.id) in check2.fetchall():
if "No" in check2.fetchall():
c.execute("""UPDATE verifiedpeople SET verified = :verified
WHERE id = :id""",
{'id': int(after.id), 'verified': "Yes"})
print(c.fetchone())
print("Updated")
conn.commit()
conn.close()
print(c.fetchone())
return
else:
print("No updating")
return
if len(check1.fetchall()) == 0:
c.execute("INSERT INTO verifiedpeople VALUES (:name, :id, :verified)", {'name': str(after.display_name), 'id': int(after.id), 'verified': 'Yes'})
print(c.fetchone())
print("Added")
conn.commit()
conn.close()
return
else:
print("Already there")
return
it always prints "Added" when the thing is already there and it doesnt print "No updating"
using sqlite3
there something I'm doing wrong?
the first condition will never be true
.fetchall returns a list of tuples
if anything, you need to check if int(after.id) in check2.fetchone(), and then i'd also recommend just fetching the ID
how could I increase/decrease a value in SQLite?
decided to ask here since it is the databases channel
i have a database of files and folders:
['drop table if exists files'],
['drop table if exists folders'],
['create table files ('
'id integer primary key,'
'path text,'
'parent integer,'
'constraint unique_path unique (path),'
'foreign key (parent) references folders(id)'
')'],
['create table folders ('
'id integer primary key,'
'path text,'
'parent integer,'
'constraint unique_path unique (path),'
'foreign key (parent) references folders(id)'
')']
]
is there some way to set up some kind of rule that will automatically set the parent of a file/folder?
(parent being the folder that a file/folder is inside)
@pulsar timber https://stackoverflow.com/a/744290
thank you! @gilded narwhal
๐
what should I define UPDATE as?
@gilded narwhal i am not quite sure what you are asking, but if you dont want people to be able to put files in without setting the parent you can enforce NOT NULL, like parent INTEGER NOT NULL
@wind pelican rather than calculating and setting the parent of each file/folder manually i'd like the db to do it automatically for me when i insert them
hmmm
so basically i'd just add a path to the db and there'd be some rule thatd do like: 1. check if it startswith any of the paths in the folders table 2. set the index to the one that found, if it's found
this https://stackoverflow.com/questions/3727543/sql-query-to-get-full-hierarchy-path goes in to some useful sql commands you could use
i'm not trying to get the full path
it shows "unresolved reference "UPDATE"
i want the db to set the parent id for me
right but you will have to use a similar construct
just in reverse
its not a trivial query, i dont know how off the top of my head but hopefully that will give you some hints
ah, i see. thanks, i'll take a look
https://academy.vertabelo.com/blog/do-it-in-sql-recursive-tree-traversal/ this looks like its going the direction yoiu want.
but you might want to split the path yourself in to an array or something easier for the db to understand
i dont really need to split it since all the paths are in full anyway
including the folder paths
it just needs to be a startswitch check on the path
oh
i thought you had a recursive structure
like a row for each step
but it sounds like you store the whole path from the root in each node
nah, just wanna make it easier to step through folders as needed later on without doing a lot of text searching
so you just need to split the input somehow, like on the last '/'
then search the folder paths for the first half
and use the second half as the name
@pulsar timber that's quite strange. would you mind posting your whole query?
though personally id just make this two queries, get the right folder, insert the file row, commit
sure
import sqlite3
from inventory import Inventory
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
def insert_inv(inv):
with conn:
c.execute("INSERT INTO inventory VALUES (:credits, :hitpoints, :attack, :maxhealth)", {'credits': inv.credits, 'hitpoints': inv.hitpoints, 'attack': inv.attack, 'maxhealth': inv.maxhealth})
def update_credits(inv, credits):
pass
def remove_inv(inv):
pass
inv_1 = Inventory(50, 20, 25, 20)
inv_2 = Inventory(50, 20, 25, 20)
# c.excecute("INSERT INTO inventories VALUES (':credits', ':hitpoints', ':attack', ':maxhealth')", (inv_1.credits, inv_1.hitpoints, inv_1.attack, inv_1.maxhealth))
# conn.commit()
# c.excecute("INSERT INTO inventories VALUES (':credits', ':hitpoints', ':attack', ':maxhealth')", (inv_1.credits, inv_1.hitpoints, inv_1.attack, inv_1.maxhealth))
# conn.commit()
insert_inv(inv_1)
insert_inv(inv_2)
UPDATE Inventory SET credits = credits + 50
c.execute("SELECT * FROM inventory WHERE credits=?", (50,))
print(c.fetchall())
conn.commit()
im working with roughly ~100k files here so i thought maybe there was some way to let the db handle it automatically without extra calculation on my end
but yeah i should just time the effect of an extra query and see if it's significant
Pep, there's no UPDATE in this code
oh wow. completely glossed over that
You have to actually execute it as a query
tfw syntax highlighting is actually unhelpful ๐
no worries
wait no
it was @dull scarab who solved it. why am i taking credit ๐
never mind I misspelled execute lol
import sqlite3
from inventory import Inventory
import discord
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
def insert_inv(inv):
with conn:
c.execute("INSERT INTO inventory VALUES (:credits, :hitpoints, :attack, :maxhealth)", {'credits': inv.credits, 'hitpoints': inv.hitpoints, 'attack': inv.attack, 'maxhealth': inv.maxhealth})
def update_credits(inv, credits):
pass
def remove_inv(inv):
pass
inv_1 = Inventory(50, 20, 25, 20)
inv_2 = Inventory(50, 20, 25, 20)
# c.excecute("INSERT INTO inventories VALUES (':credits', ':hitpoints', ':attack', ':maxhealth')", (inv_1.credits, inv_1.hitpoints, inv_1.attack, inv_1.maxhealth))
# conn.commit()
# c.excecute("INSERT INTO inventories VALUES (':credits', ':hitpoints', ':attack', ':maxhealth')", (inv_1.credits, inv_1.hitpoints, inv_1.attack, inv_1.maxhealth))
# conn.commit()
insert_inv(inv_1)
insert_inv(inv_2)
c.execute("UPDATE Inventory SET credits = credits + 1")
conn.commit()
c.execute("SELECT * FROM inventory WHERE credits=?", (54,))
print(c.fetchall())
conn.commit()
right now if I enter in c.execute("SELECT * FROM inventory WHERE credits=?", (54,)), it shows the values correctly
but when I enter `c.execute("SELECT * FROM inventory WHERE credits=?", (56,)),' it also shows the values correctly
there aren't supposed to be more than 2 inventories
Does anyone know what is causing this? (Rethinkdb)
async def tuple_convert():
"""Converts all coords to tuples"""
all_docs = await r.table(USER_TABLE).run(bot_conn)
while (await all_docs.fetch_next()):
doc = await all_docs.next()
print(tuple(doc["cords"]))
doc["coords"] = tuple(doc["cords"])
await r.table(USER_TABLE).replace(doc).run(bot_conn)
sharpbot_1_242ea091cb4c | (523, 185)
sharpbot_1_242ea091cb4c | (660, 530)
sharpbot_1_242ea091cb4c | (676, 435)
And yet.......
Hi,
I want to use MongoDB in my application : you can set a search with parameters and then scrape tweets. I need to store the search parameters and the tweets. Then, I'll only iterate in these tweets. So, the read performance is the most important factor.
I'm new to MongoDB and I drew this scheme :
https://altab.fr/snaps/chrome_2018-12-01_23-20-12.png
The problem of this scheme is that a document can't exceed 16MB (hardcoded) (so ~1k8 tweets). I can store only usefull fields and work around this limit but I prefer not.
I would like to have your valuable advice before starting to implement MongoDB
well the relational way to do this would be referencing the "index" table on the individual "tweets" entries with a foreign key
not sure if mongodb, being a modern, webscale database, supports such thing.
@ionic pecan I think we can
if I understood well, you would merge the 2 collections?
I was thinking about having 1 document = 1 tweet in the tweets collection to bypass the document size limit
but I don't know if it's good for the performances
Heh, was that a reference to the "mongoDB is webscale" youtube video?
Hello, can someone help me understand why this attempt at an UPSERT only updates the first value (price) and does not update the other two (value and cost)?
import time
conn = sqlite3.connect("simple.db")
c = conn.cursor()
day = time.strftime("%j")
c.execute("CREATE TABLE IF NOT EXISTS prices(day INTEGER PRIMARY KEY, price int, value int, cost int)")
price, value, cost = input("write price value and cost separated by a space\n>").split()
print(price,value,cost)
c.execute("INSERT INTO prices(day,price) VALUES(?,?) ON CONFLICT(day) DO UPDATE SET price=excluded.price WHERE excluded.price < prices.price",(day,price))
c.execute("INSERT INTO prices(day,value) VALUES(?,?) ON CONFLICT(day) DO UPDATE SET value=excluded.value WHERE excluded.value < prices.value OR prices.value = 'None'",(day,value))
c.execute("INSERT INTO prices(day,cost) VALUES(?,?) ON CONFLICT(day) DO UPDATE SET cost=excluded.cost WHERE excluded.cost < prices.cost or prices.cost = 'None'",(day,cost))
for line in c.execute("SELECT * FROM prices"):
print(line)
conn.commit()
conn.close()```
The desired outcome would be a single row in the database for every day of input containing the lowest of each 3 of the values found (during the day)
apparently the problem with the other 2 values is that their value gets initiated to None and the WHERE clause does not validate
if anyone runs in the same problem it finally worked when I replaced the prices.value/cost = 'None' part of the statement with prices.value/cost IS NULL
```py
code here
```
ok I have a problem. I recently wanted to start developing my bot again so I copied it to my PC from my Raspberry Pi, copied the database itself from a MySQL database and installed MySQL on my PC
but now I have a problem...
MySQL 8.0 (my PC version) won't accept this type of queries
any idea why or how could I fix this?
SELECT rank FROM chat_leveling_system_backup WORKS
SELECT level FROM chat_leveling_system_backup DOESN'T
this is the table structure
Notice the double '' at the end
That's the ' the error added in. Notice it has one at the start of the error quote as well. Also the error is before that
I added a single one before and after the needed string
SOLVED
I uninstalled MySQL 8.0 and installed MariaDB and now it works
๐บ
thanks to @plain radish โค
Hi, I have some knowledge of relational database design but I'm having a hard time wrapping my head around NoSQL database design. Anyone know some useful tutorials for NoSQL design?
@tranquil trellis NoSQL can be imagined as a collection of JSON documents, these do not have to follow a certain Schema
Thats massively over generic
NoSQL can be more than json documents
Take redis for example
Or Cassandra
It's a general term for non-SQL databases, through often used to refer to ORMs like MongoDB
ORM?
I understand that but what I dont understand for example is when I should put stuff in different tables. Because from my limited experience, I have understood that NoSQL databases don't like combining stuff from multiple tables like relational databases.
Quick question regarding SQLite... The blob datatype doesn't really have that much documentation on it in the docs, but can I store a dict as a BLOB value?
blob is binary, you can store there whatever you want, but for dictionaries it's probably better to use text so you would be able to easily read it as a human being
okay
If I have a file called run.py and inside a function that looks like: ```python
from bot import database
def get_guild():
guild_id = input( "Enter Your Guild ID: " )
database.bootup(guild_id)
get_guild()Then in my dir `folder>bot folder> database.py` I have another function:python
def bootup(guild_id: int):
path = Path('my path')
connection = sqlite3.connect(path)
db = connection.cursor()
db.execute('''CREATE TABLE f'{guild_id}'(member TEXT PRIMARY KEY, cases BLOB)''')
db.commit()
its currently throwing: File "C:\Users\clapp\Desktop\My Bot\bot\database.py", line 10, in bootup db.execute('''CREATE TABLE f'{guild}'(member TEXT PRIMARY KEY, cases BLOB)''') sqlite3.OperationalError: near "'{guild}'": syntax error
All of my integer values are going to be converted into strings, hence the TEXT
Why do you want to have separate table per guild?
you could just have a guild name as a row of your table, or as a separate table and foreign id constraint to it
the second problem is that you should never use any user input unescaped in a query
becaue that makes it possible to use SQL injections
- The db will house info as such
guild --> user --> casesI figure the easiest way of accomplishing this is to create a table with a name equivalent to the Guilds ID.
- the user input is a string of numbers provided to them, so all they have to do is copy+paste.
The general question I was asking however, was if it is possible to make a table with a name that depends on an argument.
its throwing that error
f'{guild} -> f'{guild_id}
no end quotes?
because your function does not know what guild is
no
it's just guild -> guild_id
since you don't have a variable guild in the scope of your function
then the next question would be if you are running on python 3.6
3.6+
because f-strings is a python 3.6 feature
3.7.1
weird, try to use the good-old format instead
db.execute('CREATE TABLE {table_name} (member TEXT PRIMARY KEY, cases BLOB)'.format(table_name=guild_id)
but still I think it's worth to have data not in the separate tables, but in one table. In the end what is the gain of having it split into different tables? and how are you going to make migrations later on?
File "C:\Users\clapp\Desktop\My Bot\bot\database.py", line 10, in bootup
db.execute('CREATE TABLE {table_name} (member TEXT PRIMARY KEY, cases BLOB)'.format(table_name=guild))
sqlite3.OperationalError: near "3215467": syntax error
I will try to merge into one table and see how it goes, for now though, this syntax error is extremely annoying
you table names cannot start with digit
what you can have instead is something like guild_1231231 as a table name
but you definitely should think of redoing it, since using any user input unescaped in a query is really NOGO
i am in the process of redoing it. still really new to databases so trying to learn as much as possible and as fast as possible
i hope i can get some help here:
c.execute("SELECT rowid , * FROM sugs WHERE sugid = {}".format(reaction.message.id))
row = c.fetchone()```
it prints as if there were nothing in the row but there is!
`"240483083508252672" "-=The Promidius=-" "03/12/18" "test 6" "519232169260875787" "suggested"` thats the content of the row
i guess i'm missing something obvious anyone can help me?
Dont use format for queries, use prepared statements
Also why are you selecting a specific column, then everything.
so it also shows the row id
Is the id you use and store the same type?
Int/string
And are you fetching the id of the author, or the message
Theres a lot left out, what is reaction, what db model is it, what does the table look like
yeah theyr the same type
c.execute("""CREATE TABLE sugs (
userID integer,
userNam text,
sugPost text,
sugdesc text,
sugid integer,
sugStatus text
)""")```
thats the table
reaction.message.id is 519232169260875787
i chaged it, still same problem
And what database are you using?
sqlite3
Kind of?
yeah it does now select the info from the 1st row
problem must be then WHERE sugid = {}".format(int(reaction.message.id))
Again, don't use format
should i use ? ?
c.execute("SELECT rowid , * FROM sugs WHERE sugid = ?", (reaction.message.id,))```
yes
Don't forget to pass the id as a tuple
But as to why it's not working.. are you sure the entry is there?
and that the id is correct
Like if you got the id manually, and hardcoded it. Does that work
the id from the message is the same as the one gets stored.its also the same as reaction.message.id
and wont find that row
Is it querying the same db you're viewing?
yes
Did you recreate the table after changing the integer to bigint
yes
If you tried what I suggested, started with a clean slate, new table and all that and it still doesn't work then I have no idea
I'm afraid
hey there
i'm using the psycopg2 module and a postgresql database
and try to insert something with: python cursor.execute("INSERT INTO list (year, rating, imdbid, title, country, cast, genres, directors, writers, decade, bechdelid) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);" , (year, rating, imdbid, title, country, cast, genres, directors, writers, decade, bechdelid))
But I get bash Traceback (most recent call last): File "<stdin>", line 1, in <module> psycopg2.ProgrammingError: syntax error at or near "cast" LINE 1: ... INTO list (year, rating, imdbid, title, country, cast, genr...
all my variables are strings
cast is something like "tom, jerry, berna, isolde"
I thought with this format, execute would sanitize my strings.
@trim chasm Cast is a keyword
To fix you can either change the name, or wrap it in quotes
I'm surprised that when calling .execute() with parameters the driver can't identify that if it isn't a tuple that it's.. one parameter. It seems a bit ugly to have (parentId,)
uh i need help
so this is my code
def data():
for item in b:
c.execute("""INSERT INTO DATA (ITEMS) VALUES(?) """, item)
connection.commit()
data()
i get
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
b = ["Data1", "Data2", "Data3"]
so how do i fix this
c.execute("INSERT INTO DATA (ITEMS) VALUES(%s)", (item,)) or something
i tried that
or wait
let me try again
I think it's because it tries to use item as a tuple
@torn sphinx c.execute("INSERT INTO DATA (ITEMS) VALUES(%s)",(item,))
sqlite3.OperationalError: near "%": syntax error
where am i suppose to put the "%" at the end
uh
wait now i get
c.execute("INSERT INTO DATA (ITEMS) VALUES(%s)"%(item,))
sqlite3.OperationalError: no such column: Data1
It's unsafe to use string formatting in an execution, you should use prepared statements. This seems like an echo chamber because somebody else was doing the exact same thing if you scroll up in the channel a bit. ๐
I should legally change my last name to "'); DROP ALL DATABASES; COMMIT;" to find every online account system using string formatting on SQL queries.
Anyone here have any experience with inserting and selecting ' into a Sql database?
I've got it inserting fine, but can't seem to select it, either ways I've tried it syntax errors or it's looking for the escape character as part of the string
Which RDBMS are you using and how are you performing queries?
MariaDB and SqlAlchemy
What have you tried to escape the quote? \' and ''?
is the quote parameterised or part of the "raw SQL"?
Oh, well I suppose it's wrong to assume you're doing raw sql queries as it is an ORM after all
Sorry I'm not sure what you mean by that, still fairly new to using Sql with Python
Can you show the code for the query?
SELECT server FROM `tags` WHERE server={0} AND tag={1}
And then I'm passing the values into {0} and {1}
and those values have the quote?
uh, are you using .format...?
...sure...
check_sql = check_sql.format(ctx.message.server.id, self.escape(txt))
Escape() is what adds the required '' for the query
what are you using for the db, you should not be using .format for this, especially for a discord bot
Really? Why is that
If you use a query with parameter bindings or whatever they're called, sqlalchemy will likely take care of escaping the quote for you
Well when I was working on the part that was inserting it wasn't so I had to escape it myself, just to be clear escape() is used to add the single quotes around {1} like '{1}'
because you're opening yourself to sql injection
especially since this looks like a tag system
i'm curious why you're using sql queries since you said you were using sqlalchemy earlier
For ORMs, raw SQL queries are typically used only for more complex queries.
Oh okay, what's the alternative?
If you use SQL for everything, it kinda defeats the purpose of an ORM.
Well, do you even need an ORM?
If you do, then here is how you use it in SQLAlchemy https://docs.sqlalchemy.org/en/latest/orm/tutorial.html
If not, use something like aiomysql instead and keep performing queries using SQL
I'm just editing an existing bot, so I'm not sure what the original authors intentions were
I'll take a look at that expression link you posted once I get home though, thanks
If the existing queries were already being done with .format() then the authors of the bot were following bad practices
I kind of figured, it's a mix between %s and .format
If they are already using SQLAlchemy then fair enough to just keep using it since it may be a lot of work to migrate over to another library depending on the bot's scale.
Furthermore, it'd also be easier for you, as a beginner, to just convert the queries to use sqlalchemy.sql.expression.text since the SQL will be the same for the most part.
Where as the latter method would require relatively more learning of SQLAlchemy on your part
Regarding your original quote issue, like I said, I think SQLAlchemy will take care of escaping it for you if you use a parameterised query. Don't have a way to test that though.
I will play around with it some more tonight
I know you aren't using psycopg, @vernal ocean , but this section explains the problem with query parameters in SQL: http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters
"Never, never, NEVER use Python string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. Not even at gunpoint." I like the "Not even at gunpoint." part

Haha I'll give it a read
I have a table in my SQLite database initializaed with ```python
conn = sqlite3.connect(path)
db = conn.cursor()
db.execute( '''CREATE TABLE IF NOT EXISTS "Content Creators" (guildID INTEGER PRIMARY KEY, memberID INTEGER) ''' )
db.commit()
``` And I need to retrieve a list of all the member IDs for a certain guild. How do I do that? A lot of the docs and examples use data[key][key] but I don't know where they are getting the data from
Make a select query in SQL
that would look like ```python
db.execute('''SELECT memberID from "Content Creators" ''')
I am very new to SQL
okay, and this will output a list of memberIDs?
Yes
one
Hello, I am pulling data from the online game Eve using their swagger interface. I can get my code to pull the data and print it no worries, but trying to insert it into a database is proving to be an issue.
This is my code https://pastebin.com/RePWh
I have limiited the pull to just 2 pages at the moment until I know it is going to insert properly.
This is the error I am getting
File "test_script.py", line 48, in <module>
sql="insert into forge_orders (duration,is_buy_order,issued,location_id,min_volume,order_id,price,range,system_id,type_id,volume_remain,volume_total) VALUES (%d,%d,%d,%s,%d,%d,%s,%f,%s,%d,%d,%d,%d)" %json_response
TypeError: not enough arguments for format string```
Unfortunately I do not understand enough yet to know if what I have googled is giving me the right info.
I have tried `json_response = json.loads(list(raw_response.raw))` ` json_response = json.loads(raw_response.raw)` on line 41, as well as adjusting the insert statement as much as I know how, but cant seem to get anything to work.
Hmm, I sense a C background. In most SQL implementations I've used on Python %s is used for all value placeholders. Also, % is string interpolation which doesn't escape any fields since it's a String method unrelated to SQL at all. I suggest instead of "% json_response" to do ", (json_response["duration"], json_reponse["buyorder"], ... ) )
The INSERT expects the values to be in a tuple, not a dictionary. The values should be 1 dimensional, and dictionaries are not.
i'd also advise against using 'x' % y for your sql query
Ah ok, I have no background at all, just trying to cobble something together from bits and pieces I can find.
So are you suggesting something like ``` sql="insert into forge_orders (json_response['duration'],json_response['is_buy_order'],json_response['issued'],json_response['location_id'],json_response['min_volume'],json_response['order_id'],json_response['price'],json_response['range'],json_response['system_id'],json_response['type_id'],json_response['volume_remain'],json_response['volume_total'])"
or does the json_response variable need to be outside of the string, so use the double quotes " instead of singles ' ?
erm no
nothing like that
if you're gonna do it by tuple you'd need to do it one by one, alternatively you can just used named parameters and pass the whole json_response to it (assuming there's nothing else in the response)
im unsure of what you mean sorry.
This is the json response I am getting
[{"duration":90,"is_buy_order":false,"issued":"2018-11-07T03:55:30Z","location_id":60001801,"min_volume":1,"order_id":5233442817,"price":8000.0,"range":"region","system_id":30000162,"type_id":251,"volume_remain":4994,"volume_total":5000} There are 1000 of these returned per page.
Do I need to dreate a new insert query for each column is what you are saying? or a new insert for each set?
Right, that's a dictionary
Like a Map or a Hash
SQL values should be a list of values
That result is unordered
Usually you can use an "insertmany" sort of method to insert many rows, but you'll still need to convert that into a list. There are various ways to do this, maybe by mapping it.
Ok, thanks for the tips. Now I have some idea of what I should be trying to do. ๐
Spent 5 hours on finding this bug: "table X has 9 columns but 8 values were supplied"

A somewhat dumb question. What would one use in async environment to access a PostgreSQL database instead of Django ORM?
Is there even an ORM that is compatible with async environment?
Or the whole idea of ORM and async somewhat contradict each other?
?_Hi guys, for example primary key such as user_id, should it be in varchar or INT
It depends on what do you intend to put there and how do you intend to use it.
Generally having INT AUTOINCREMENT as a primary key is a good idea.
So SQLite
I know thereโs stuff like PYpi I think that do this sort of thing?
Was looking for mayb a better explanation in python on how it is done
do i just create a new db model defining the columns etc
?
You can import a CSV file into SQLite if thats what you want
yeah thats sounds bout right
then afterwards connecting to that db to do diff requests
Yea you can do that
Don't need python to import really, can just use the command line
and will that create the db with the corresponding column headers e.g. csv with car details
so brand, engine size, price etc
and whats the diff between using postrgres and mysql?
that changed quickly
lol
cause i know u can create a model
n connect to that model depending on ur db choice i.e. postgres and mysql
then do some shennagins there but im jus all over the place haha
what do you mean by model
in flask you can do something like db.model
n interact with that i.e opening a connection then doing some sql on it
sqlalchemy*
oh you mean sqlalchemy
yeah
so create a model give it corresponding table column names matching the csv
then open that csv n load that in using sqlalchemy?
then afterwards simply just opening a connection to it when i need to get some data from it
im new to all this so jus trying ro wrap my head around it
You don't need sqlalchemy to import the csv
If you want to use sqlalchemy afterwards to interact with the data, thats your choice
Yea
what is the cmd sorry
il try it now
is it something like load data 'filename' etc? :S
and what database should i choose to use?
then .import csv/file/location table
and will that put the table with the column headings matching csv headers?
i wanna upload all the data inside the csv to the table
yes
you don't need to create a table
o when i run that cmd it'll do it itself?
if it has the headers yes
o cool sweet
also when it comes to choosing what database to use what would be the best?
'optimal'
or does it even matter
it massively depends on what you plan to do
eg?
like between a relational and non relational db
sql vs nosql?
i feel like nosql would be better due to not being bound to structure limitations? :S
maybe ive gone off topic lol il jus google that kinda stuff
thanks @glossy ermine
how come?
because 9 times out of 10 your data is relational
relational as in a column matching a row value etC?
i have no exp with databases at all so relational n non relational i dont really know much
other then running sql
As in if your data is mumbled garbage and has no links then nosql is a perfect fit
But lets take a forum website for example, you have users, and those users have posts
This fits perfectly into a relational DB
oooooooo
nice example
so for a car database company
i guess relational DB would work
best
Yep :)
so why do people like to use nosql stuff
buzzwords
so i user postgres at my old work place