How to insert only one value into sqlite3 table as an variable while inserting null to every else column

Viewed 984

I want to modify the code beneath so that I only insert input variable to the table. All other columns should store NULL value. I think the most obvious solution would be to declare that column2 = NULL and column3 = NULL, but I was thinking is there another way?

import sqlite3

column1 = input()

sqliteConnection = sqlite3.connect('database.db')
cursor = sqliteConnection.cursor()

sqlite_insert_with_param = "INSERT INTO ideas (column1, column2, column3) VALUES (?, ?, ?);" #column1 is primary key

data_tuple = (column1, column2, column3)
cursor.execute(sqlite_insert_with_param, data_tuple)
sqliteConnection.commit()

cursor.close()

Eventually I want this to work in Tkinter app that prompts user input for column 1 with Entry-widget and it would create the database entry without any other information on it other than the primary key.

1 Answers

If you remove the other columns from the list, like:

sqlite_insert_with_param = "INSERT INTO ideas (column1) VALUES (?);"
data_tuple = (column1,)
cursor.execute(sqlite_insert_with_param, data_tuple)
sqliteConnection.commit()

then column2 and column3 will be null in the new inserted row, unless you have defined a default value for them in the definition of the table.

Related