Search code examples
pythonsqlitesql-insertauto-increment

How to tell python "hey, that one column you ask me to fill is already filled with autoincrement"


Python keep telling me that I need to insert something in my sql folders,

CREATE TABLE voting_page (
    voter_id integer primary key autoincrement,
    voter_name varchar(255) NOT NULL,
    occupation varchar(255) NOT NULL,
    voter_age int(3),
    candidate_name varchar(255) NOT NULL
    );

This is my sql file, which is already set to have an auto increment

My python file need to insert four which is,

    c.execute('''INSERT INTO voting_page VALUES (DEFAULT, :voter_name, :voter_occupation, :voter_age, :candidate_name)''',
            {
                'candidate_name': candidate_saved,
                'voter_name': voter_name_saved,
                'voter_occupation': voter_occupation_saved,
                'voter_age,': voter_age_saved,


            })

sqlite3.OperationalError: table voting_page has 5 columns but 4 values were supplied

I tried using DEFAULT and sqlite3.OperationalError: near "DEFAULT": syntax error


Solution

  • Either list the column names without the column voter_id:

    INSERT INTO voting_page (voter_name, occupation, voter_age, candidate_name) VALUES (:voter_name, :voter_occupation, :voter_age, :candidate_name)
    

    or use NULL for voter_id:

    INSERT INTO voting_page VALUES (NULL, :voter_name, :voter_occupation, :voter_age, :candidate_name)
    

    In both cases voter_id will be auto incremented.