#πŸ”’ Adding Data to a Database

23 messages Β· Page 1 of 1 (latest)

tranquil steeple
#

I am learning sqlite3 and cant get it to work. Can someone please tell me how to add data to my table

primal monolithBOT
#

@tranquil steeple

Python help channel opened

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.

sudden iron
#

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?

tranquil steeple
#

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)")

sudden iron
#

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?

tranquil steeple
#

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

sudden iron
#

Could you show how you created the table and the actual full command?

spark jackal
#

Also the nested " would cause syntax error

sudden iron
tranquil steeple
#
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()
spark jackal
#

Whats the error that results?

tranquil steeple
#

sqlite3.OperationalError: no such column: Training

spark jackal
#

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) 
tranquil steeple
#

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...

spark jackal
#

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) 
primal monolithBOT
#
Python help channel closed for inactivity

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.