Python Forum

Full Version: Compare variable with values in a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a file:
cat file1.txt
cel01
cel02
cel03
cel03
I have a Oracle DB cursor that brings some values from to variable VAL_CEL1.
cursor_fgnmaes.execute("select distinct failgroup from v$asm_disk")
I want to exclude all entries found in file1.txt from cursor query , something like:
cursor_fgnmaes.execute("select distinct failgroup from v$asm_disk where failgroup not in [ALL_LINES_FROM_FILE]")
How can I do that? I tried opening file and read line per line but did not work.
Wouldn't you rather ask this question in a MySQL forum??

In a MySQL query, the WHERE part needs to refer to something in a table I believe. So those file lines need to be in the table somewhere.

I never use Oracle DB, but in MySQL, you couldn't do: "select distinct failgroup from ... " You will get an error.
Either use distinct_failgroup as a column name, or distinct, failgroup as 2 column names.

Quote:SELECT distinct_failgroup FROM mytable WHERE mytable.distinct_failgroup NOT IN('cel01', 'cel02', 'cel03')

You could use Python to open the file, read each line and insert the lines as variables before running the SQL query.

with open('myfile.txt') as mf:
    mylist = mf.readlines()

my_sql = "SELECT distinct_failgroup FROM mytable WHERE mytable.distinct_failgroup NOT IN("
for i in range(mylist):
    var = mylist[i].strip() # get rid of newline
    if not i == len(mylist) -1:
        my_sql = my_sql + var + ', '
    else: 
        my_sql = my_sql + var + ')'
Then use the SQL query my_sql