I have more than 100 rows of data and it would be really messy to have a line for insert table for every row. So I am trying to somehow quicken how I insert rows into my sqlite database. So I came up with the following:
def insert_item(obj):
with db:
c.execute("INSERT INTO Database VALUES (\
:First, :Last, :Item1, :Item2)",
{'First': obj.First,
'Last': obj.Last,
'Item1': obj.Item1,
'Item2': obj.Item2})
obj_1 = ('John', 'Doe', 4, 5)
obj_2 = ('Jane', 'Doe', 6, 8)
...
obj_106 = ('Johnathan', 'Doe', 9, 1)
d = {}
for x in range(1, 107):
row = (d["obj_{0}".format(x)])
insert_item(row)
However, it returned with this error:
row = (d["obj_{0}".format(x)])
KeyError: 'obj_1'
Not sure what went wrong, the string is returning the correct value that I want but the function is not working.
Thank you
dempty at the point in your code where you're trying to lookup 'obj_1'?