#๐ Python MySQL
72 messages ยท Page 1 of 1 (latest)
@tired vessel
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
this is the code im using for this option
print("Add performance selected")
AthleteID = input("What is the athlete's ID? ")
CategoryID = input("What is the category ID? ")
EventID = input("What is the event ID? ")
Performance = input("What is the performance? Example: 10.5 sec or 60.2 meters ")
PerformanceDate = input("On what date was this achieved? Example: 2000-01-31 ")
sql = "INSERT INTO performances (AthleteID, CategoryID, EventID, Performance, PerformanceDate) VALUES (%s, %s, %s, %s, %s)"
values = (AthleteID, CategoryID, EventID, Performance, PerformanceDate)
mycursor.execute(sql, values)
mydb.commit()
this is the same code but in english
what dpes the table look like? when you created it
youre inserting everythin as strings, and it looks like its expecting something as something not a string
that would be your issue, convert the relevant strings to ints
okay so i need to specify the all the inputs?
is the correct placeholder character being used?
values = (int(AthleteID), CategoryID, EventID, Performance, PerformanceDate)
they all got white instead of brown
i normally use ?, but i have seen people use %s, but i dont know much about it other than google says its something about a str replacement
when you hover what does it say the error is
oh wait my mistake
add int to everything that should be an int
all right i will try
according to the docs, %s is indeed the correct placeholder
getting the same error
i think something else is broken because my code is only 80 lines and this one goes all the way to 1199
all that means is one of the functions to make sure you are using it right is catching the error
i use pymysql
can you copy paste that error at the bottom, i dont feel like typing it out
yeah of course one moment
pymysql.err.OperationalError: (1136, "Column count doesn't match value count at row 1")
@tired vessel could you try enclosing the args tuple in a list?
how do i do that? sorry im very bad at python
i found the issue
ohhh
dont worry about that
okay
mycursor.execute(sql, [values])
count how many columns you are selecting in the sql variable.
then count how many you are trying to insert in values
its throwing the error bc you are telling it 4 values and then giving 5
oh my god no way
ah, it's only 4 named columns
didnt catch that bc im used to seeing the error worded differently since i use sqlite3 lol
OndereelID is missing
can you try using ? instead of %s?
pymysql docs say to use %s
im really mostly curious, if it works it fixes his problem, if not at least we tried something lol
If args is a list or tuple, %s can be used as a placeholder in the query. If args is a dict, %(name)s can be used as a placeholder in the query.
same error
did you convert your datetime properly?
On what date was this achieved? Example: 2000-01-31 2024-11-09
@tired vessel so we're clear, you're running mycursor.execute(sql, values), yes? my advice from before of wrapping it in a list was incorrect
if thats the format, i would actually change the entry to be a varchar bc date is something else entirely
yes
okay let me do that one moment
date is basically an object that can give you any kind of date related data you can think of about its stored date
(if using datetime module lol)
it seems to be a formatting error from what im reading on google
but i dont see that format error in your code
that's weird
can you send what your code looks like now?
elif option == 4:
print("Prestatie toevoegen geselecteerd")
AtleetID = input("Wat is het ID van de atleet? ")
CategorieID = input("Wat is het ID van de categorie? ")
OnderdeelID = input("Wat is het ID van het onderdeel? ")
Prestatie = input("Wat is de prestatie? Voorbeeld: 10.5 sec of 60.2 meter ")
DatumPrestatie = input("Op welke datum is dit behaald? voorbeeld 2000-01-31 ")
sql = "INSERT INTO prestaties (AtleetID, CategorieID, Prestatie, DatumPrestatie) VALUES (%s, %s, %s, %s)"
values = (int(AtleetID), int(CategorieID), int(OnderdeelID), Prestatie, DatumPrestatie)
mycursor.execute(sql, values)
mydb.commit()
your sql and values still dont line up
4 names, 4 %s, and 5 values
it needs to all be the same
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.