There is a code which inserted record In a DB

def insertMult(connect,array): connect.execute("INSERT INTO Goods (Gtin,Length,Width,Height,Weight,Multuplicator,Gtin2) \ VALUES ("+array[0]+","+array[1]+","+array[2]+","+array[3]+","+ array[4]+","+array[6]+","+array[7]+")"); connect.commit() 

Everything was ok, now there is a need for Id auto - increment

Added field to DB with Id. I run the code and get the exit ..

 NOT NULL constraint failed: Goods.Id 

Changed the code to

 def insertMult(connect,array): connect.execute("INSERT INTO Goods (Id,Gtin,Length,Width,Height,Weight,Multuplicator,Gtin2) \ VALUES ("+array[0]+","+array[1]+","+array[2]+","+array[3]+","+ array[4]+","+array[6]+","+array[7]+")"); connect.commit() 

Now insert 7 values ​​in 8 columns of the table, since Id auto increment do not pass anything and the same error .. I also transmitted 8 values ​​.. the same error .. Help plz

    1 answer 1

    I would do it a little differently:

     def insertMult(connect,array): connect.execute("INSERT INTO Goods (Gtin,Length,Width,Height,Weight,Multuplicator,Gtin2) \ VALUES (?,?,?,?,?,?,?);", array) 

    array - must contain exactly 7 variables with the correct data types - the data types must correspond to those in the table

    1. if id is auto_increment, then this field should not be explicitly specified
    2. Always try to use prepared statements / bind variables / parameters when working with literals - this will save you from SQL injections and will most likely work faster, because The RDBMS will use the already prepared execution plan and can save on parsing the request, checking the syntax and privileges and building the optimal execution plan for the request.