2

I am trying to compare a list of links to the liks stored in an sqlite database.

assuming the links in the database are:

link.com\page1

link.com\page2

link.com\page3

I have written the following code to chick if a given link exists in the database and adds it if it did not exist.

links = ['link.com\page2', 'link.com\page4']
c.execute('SELECT link FROM ads')
previouslinks = c.fetchall()

for l in links:
    if l not in previouslinks:
        c.execute('''INSERT INTO ads(link) VALUES(?)''', (l))
        conn.commit()
    else:
        pass

the problem is even though the link is in the database, the script does not recognise it!

when I try to print previouslinks variable, results look something like this:

[('link.com\page1',), ('link.com\page2',), ('link.com\page3',)]

I think the problem is with the extra parentheses and commas, but I am not exactly sure.

1 Answer 1

1

fetchall() returns a list of rows, where each row is a tuple containing all column values. A tuple containing a string is not the same as the string.

You have to extract the values from the rows (and you don't need fetchall() when iterating over a cursor):

previouslinks = [row[0] for row in c]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.