I have this code, it reads a csv file for the 2 first columns and appends a list from each row.
with open(self.selected_file[0], 'rb') as csv_file:
itemids = []
csv_reader = csv.reader(csv_file, delimiter=',', quotechar="\"")
for row in csv_reader:
itemids.append([row[0], row[1]])
I have a database that already contains 2 tables, each for every item respectively. I wish to check every row of the csv file (which are pairs of strings). If both strings are unique to their respective table (meaning row[0] is unique to the first item table in my db, and row[1] unique to my second item table), then add those values to their respective tables. I tried the following:
for item in itemids:
first_itemids = db_cursor.execute('''SELECT itemid FROM items_one''').fetchall()
second_itemds = db_cursor.execute('''SELECT itemid from items_two''').fetchall()
try:
if not item[0] in first_itemids and not item[1] in second_itemids:
db_cursor.execute('''INSERT INTO items_one(itemid) VALUES (?)''', (item[0], ))
db_cursor.execute('''INSERT INTO items_two(itemid) VALUES (?)''', (item[1], ))
db_conn.commit()
However this check if not item[0] in first_itemids and not item[1] in second_itemids
always evaluates to true, so duplicate non unique items are being added. I also tried the other way around
if item[0] in first_itemids or item[1] in second_itemids:
pass
but that also failed
Note: These aren't my actual variable names, I don't know if same column names in different tables under the same DB can cause problems, but mines don't anyway - I just changed it for the sake of readability.
Edit:
I also tried checking each csv row before appending it to my item list like so:
with open(self.selected_file[0], 'rb') as csv_file:
itemids = []
csv_reader = csv.reader(csv_file, delimiter=',', quotechar="\"")
first_itemids = db_cursor.execute('''SELECT itemid FROM items_one''').fetchall()
second_itemids = db_cursor.execute('''SELECT itemid from items_two''').fetchall()
for row in csv_reader:
if row[0] not in first_itemds and row[1] not in second_itemids:
itemids.append([row[0], row[1]])
And then just insert the lists' value to the DB. No good as well