Home > Software engineering >  Use database defaults while inserting new row from code
Use database defaults while inserting new row from code

Time:10-18

I am creating a login/register program with SQLite database and I wonder how can I insert a row to a database while passing only 4 out of 7 columns and leave first three (id, status and type) blank so they are filled with database default values. How can I do something like that? I tried passing just 4 arguments but it wont work in any way I try.

username = input("username: ")
name = input("name: ")
password = input("password: ")
email = input("email: ")

salt = 'xxxxx'
hexdex = hashlib.sha512((password   salt).encode()).hexdigest()


con = sqlite3.connect('database/database.db')
cur = con.cursor()

cur.execute("INSERT INTO users VALUES (DEFAULT, DEFAULT, DEFAULT,  (?), (?), (?), (?))", (username, name, hexdex, email))

con.commit()
con.close()

CodePudding user response:

You must list all the columns that will get the values that you supply and skip the columns that will get their default values:

sql = "INSERT INTO users(username, name, hexdex, email) VALUES (?, ?, ?, ?)"
cur.execute(sql, (username, name, hexdex, email))

I use username, name, hexdex, email as the names of the columns.
Change them if they are different than the actual names.

  • Related