Python/Mysql - inserting variable values into table

30 views Asked by At

error message when trying to insert variable values into table.

Error: {} Failed executing the operation; Could not process parameters: int(10), it must be of type list, tuple or dict

I have a table called FOODS which contains nutritional values of various foods. I want to extract foods consumed during the day, adjust values by quantity eaten and place result into table called CONSUMED.

int(10) in the error statement is the value of fieldvar0. When leaving this variable out the error statement stays the same but now refers to "cheese" - the value of fieldvar1.

Herewith code. I'm new to this so probably not very elegant.

# procedure to store food consumed to consumed table
sql_select_query = """select id, category, type, \
brand, seller, baseunit, baseqty, kcal, fat, sat_fat, \
carbs, sugar, free_sugar, fibre, protein, salt \
from foods where id = %s"""
cursor.execute (sql_select_query, (id,))
data = cursor.fetchone()
fieldvar0, fieldvar1, fieldvar2 = data[0], data[1], data[2],
fieldvar3, fieldvar4, fieldvar5 = data[3], data[4], data[5],
fieldvar6, fieldvar7, fieldvar8 = data[6], data[7], data[8],
fieldvar9, fieldvar10, fieldvar11 = data[9], data[10], data[11],
fieldvar12, fieldvar13, fieldvar14 = data[12], data[13], data[14],
fieldvar15 = data[15],
# sample check variables obtained from foods table
print (fieldvar0, fieldvar14, fieldvar15)
user_percentage = int(input("Percentage of base quantity consumed? "))
fieldvar7 = fieldvar7*user_percentage/100
#check calculated ok
print (fieldvar7)
# ok to here -  I didn't calculate other fields at this stage while testing
cursor.executemany("insert into consumed(id, category, type, brand,\
seller, baseunit, baseqty, consumed, kcal, fat, sat_fat, \
carbs, sugar, free_sugar, fibre, protein, salt) \
values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s, \
%s, %s, %s)", (fieldvar0, fieldvar1, fieldvar2, fieldvar3, \
fieldvar4, fieldvar5, fieldvar6, str(user_percentage), \
fieldvar7, fieldvar8, fieldvar9, fieldvar10, \
fieldvar11, fieldvar12, fieldvar13, \
fieldvar14, fieldvar15))
connection.commit()
0

There are 0 answers