Home > database >  How to insert data from an Autoincremented table in sqlite3
How to insert data from an Autoincremented table in sqlite3

Time:07-11

I've been creating a database and started to edit a table whereas I made the ID auto increment.
While configuring it I came up with this code:

import sqlite3
payrollConnect = sqlite3.connect("payrollDatabase.db")
pC = payrollConnect.cursor()
pC.execute("""CREATE TABLE IF NOT EXISTS LoanDB (
    'EID' INTEGER PRIMARY KEY AUTOINCREMENT,
    'GPL' REAL,
    'SLMV' REAL,
    'MPL GSIS' REAL,
    'CALAMITY' REAL,
    'OPTL INST' REAL,
    'S/L CONSOLOAN' REAL,
    'UOLI LOAN' REAL,
    'PL REG REAL',
    'EDUC ASST' REAL,
    'MPL PAGIBIG' REAL,
    'CPL' REAL,
    'GSIS EMERG' REAL)
""")
loantest = [(0.00,0.00,0.00,0.00,0.00,3613.68,0.00,0.00,0.00,0.00,0.00,0.00),(0.00,3000,0.00,5000.00,0.00,3613.68,0.00,0.00,0.00,0.00,0.00,0.00)]
pC.executemany("""INSERT INTO LoanDB VALUES (?,?,?,?,?,?,?,?,?,?,?,?)""",loantest)

When I run this I got this error:

    pC.executemany("""INSERT INTO LoanDB VALUES (?,?,?,?,?,?,?,?,?,?,?,?)""",loantest)
sqlite3.OperationalError: table LoanDB has 13 columns but 12 values were supplied

Why is this error occurring? From my understanding, this should not occur, since I made the 'EID' column in an auto incremented column.

Thanks in advance.

CodePudding user response:

For columns with Auto Increment property you don't need to supply the values.

Try this.

pC.executemany("""INSERT INTO LoanDB('GPL', 'SLMV', 'MPL GSIS', 'CALAMITY', 'OPTL INST', 'S/L CONSOLOAN', 'UOLI LOAN', 'PL REG REAL', 'EDUC ASST', 'MPL PAGIBIG', 'CPL', 'GSIS EMERG')) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)""",loantest)

Or if you don't want to provide column names, you can provide null value for AUTO_INCREMENT column

pC.executemany("""INSERT INTO LoanDB VALUES (null, ?,?,?,?,?,?,?,?,?,?,?,?)""",loantest)
  • Related