#π Adding Data to a Database
23 messages Β· Page 1 of 1 (latest)
@tranquil steeple
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.
What have you tried?
Because it should be quite straightforward with basic sql - you load the db (it gets created if it doesn't exist) using the sqlite3 lib in python, then run basic sql to create tables and add data to them...
So the question is: how your sql looks like?
I created the database and a Table in it. I tried adding data with cursor.execute("INSERT INTO daten("column names") VALUES (1, 2, 3, 4, 5)")
This is not valid sql you posted. "column names" is supposed to be replaced by actual names of the columns in your table. daten would be the table name?
yeah i know that. daten is the name of my table and I replaced my column names with "column names" so you wouldnt get confused by my columns :/. In my code I have my columns there
Could you show how you created the table and the actual full command?
Also the nested " would cause syntax error
Oh, I just noticed... If you used the "INSERT then "column will close it.
Because it's the same type of quote.
Use single quotes ' as the outside quotes
connection = sqlite3.connect("Trainingsdaten.db")
cursor = connection.cursor()
cursor.execute("""CREATE TABLE daten(datum DATE, dauer INTEGER, art, partner, intensitaet INTEGER, points INTEGER)""")
cursor.execute('INSERT INTO daten(datum, dauer, art, partner, intensitaet, points) VALUES (03-10-2025, 50, Training, Rehrl, 7, 50)')
connection.commit()
Whats the error that results?
sqlite3.OperationalError: no such column: Training
the values shud be quotes i think
for datetime and trainsing, and Rehrl
You could use cursor data tuple to insert stuff
data = ('2025-10-03', 42, 'Sample')
sql_insert_query = 'INSERT INTO table(col1, col2, col3) VALUES (?, ?, ?)'
cursor.execute(sql_insert_query, data)
alright I used quotes for the values u told me
it works now but what if I have a input gui where the values are always different? I obviously cant use quotes for that...
the input GUI would make the value already a string so quotes wont be needed
for exmaple using input()
col3 = input('Input for column three:')
data = ('2025-10-03', 42, col3)
sql_insert_query = 'INSERT INTO table(col1, col2, col3) VALUES (?, ?, ?)'
cursor.execute(sql_insert_query, data)
This help channel has been closed. 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.