0

Let say I have a MySQL table XYZ and in that, I have two columns i.e. "filename" and "status", and I want to access only those files whose status is NotVisited.

SO, basically, when i am iterating a folder consist of files that I want to access, first I want to check if that file is present in the table and if not present I will add it into the table .

ID = "Visited"
files_ = glob.glob("test/**/*.dcm", recursive=True)
for i in files_:
    qur = cur.execute("SELECT filename FROM dicom2 WHERE filename == '{}' AND flag = '{}'".format(i, ID))
    cur.execute(qur)
    data = cur.fetchall()
    if data:
        print("EXIST")
    else:
        cur.execute("insert into dicom2(filename, flag) values('{}','{}')".format(i, ID))
        print("Added")
print("Done")

Basically, i am not able to get the logic or this below line

qur = cur.execute("SELECT filename FROM dicom2 WHERE filename == '{}' AND flag = '{}'".format(i, ID))

can anyone please help me with this?

Thanks.

  • That query is going to `SELECT` all the `filename`s from the `dicom2` table applying specific filtering conditions: if `filename` == `your .dcm files` and `flag` == `Visited`. `execute` is going to execute the query against your DB and `fetchall` is going to return you the records that match the above conditions – Grajdeanu Alex Oct 08 '21 at 11:32
  • In SQL, the comparison operator is `=`, not `==`. Voting to close this as a typo. Also, consider using [parameter substitution](https://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python?r=SearchResults&s=1|31.9547) rather than string formatting to bind values to your queries. – snakecharmerb Oct 08 '21 at 11:45
  • Thanks for the reply @GrajdeanuAlex. i am confused about the place of "your .dcm files" I want for loop's "i" and that i don't know how to do it – Nimesh Kumar Oct 08 '21 at 11:55
  • @GrajdeanuAlex like this SELECT filename FROM dicom2 WHERE filename == i AND flag = "Visited". – Nimesh Kumar Oct 08 '21 at 11:59
  • Thanks for reply @snakecharmerb if you can also look into this – Nimesh Kumar Oct 08 '21 at 12:09

0 Answers0