Forums

Django shell inserting multiple values in a rwo using custom sql

hello

i use the custome sql in django shell to insert values but it ask the primary key value

p.execute(‘INSERT INTO webapp_information VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)’,i)

i is the list of values

it shows error

OperationalError: table webapp_information has 32 columns but 31 values were supplied

the table has 31 column only. But giving the primary key, works fine. how to resolve this. thankyou

[edit by admin: formatting]

If you had just added an extra field, and you did not do a migrate then that could be the problem.

Hello..thanks for replying.. I did not add new field and also I migrate to database.. The thing is, django asking the primary key or ID for new row.. if I give primary key, then it worked.. but how could I give primary key for every row which I inserted..

Like in normal python shell, we did not give primary key or ID for new row.. Why this asking primary key.. Basically, the primary key will automatically generated when we create a new row..

What to do..

Hello..thanks for replying.. I did not add new field and also I migrate to database.. The thing is, django asking the primary key or ID for new row.. if I give primary key, then it worked.. but how could I give primary key for every row which I inserted..

Like in normal python shell, we did not give primary key or ID for new row.. Why this asking primary key.. Basically, the primary key will automatically generated when we create a new row..

What to do..

It actually asking the value for the ID column in the database..

It actually asking the value for the ID column in the database..

Maybe you could explicitly specify the column names in your insert statement? That is, instead of

p.execute('INSERT INTO webapp_information VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',i)

...you would do

p.execute('INSERT INTO webapp_information (column_name_1, column_name_2, column_name_3, column_name_4, column_name_5, column_name_6, column_name_7, column_name_8, column_name_9, column_name_10, column_name_11, column_name_12, column_name_13, column_name_14, column_name_15, column_name_16, column_name_17, column_name_18, column_name_19, column_name_20, column_name_21, column_name_22, column_name_23, column_name_24, column_name_25, column_name_26, column_name_27, column_name_28, column_name_29, column_name_30, column_name_31)  VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)', i)

...replacing the column_name_X stuff with the actual column names, of course.

thank you very much.. i did this same method..and of course your are also post this same method.. i am happy once again thank you

Excellent, we could both work it out :-)