0

I'm having an issue with my script. The error I'm getting below is

File "./filter.py", line 12
with open('test.txt') as f:
        ^
SyntaxError: invalid syntax

The code that I'm using below is this.

with open('test.txt') as f:
    for row in f:                                
        cur.execute("DELETE FROM filterstagetbl where filtersetidn IN (select filtersetidn from filtersettbl where name = '"+row.strip()+"'")
        cur.execute("DELETE FROM filtersetaccesstbl where filtersetidn IN (select filtersetidn from filtersettbl where name = '"+row.strip()+"'")
        cur.execute("DELETE FROM filtersetmembertbl where filtersetidn IN (select filtersetidn from filtersettbl where name = '"+row.strip()+"'")
        cur.execute("UPDATE filtersettbl set status = 4 where name = '"+row.strip()+"'")
    conn.commit()

The script basically connects to DB, looks for a file and delete based on the input of the file.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Shifty
  • 3
  • 2
  • 3
    Several problems here: Your indentation is shot, so we cannot distinguish posting problems from actual indentation issues. You failed to include the actual error message. You shouldn't use string interpolation when you really should be using SQL parameters instead. – Martijn Pieters Apr 29 '14 at 16:17
  • Sorry about that. I gave the original code that I had before I started mucking around with it. – Shifty Apr 29 '14 at 16:29
  • You are using an old version of Python that doesn't yet *have* the `with` statement.. – Martijn Pieters Apr 29 '14 at 16:32

1 Answers1

0

The with statement was added in Python 2.5; you must be using a version older than that.

Either upgrade Python, or not use the with statement; you could use try/finally here to ensure the file object is closed:

f = open('test.txt')
try:
    for row in f:
        # ...
finally:
    f.close()

You should really use SQL parameters to interpolate the row data instead of using string manipulation:

for row in f:
    params = (row.strip(),)
    cur.execute("DELETE FROM filterstagetbl where filtersetidn IN (select filtersetidn from filtersettbl where name = ?",
                params)
    cur.execute("DELETE FROM filtersetaccesstbl where filtersetidn IN (select filtersetidn from filtersettbl where name = ?",
                params)
    cur.execute("DELETE FROM filtersetmembertbl where filtersetidn IN (select filtersetidn from filtersettbl where name = ?",
                params)
    cur.execute("UPDATE filtersettbl set status = 4 where name = ?",
                params)

where you'll need to adjust the parameter placeholder syntax for your database adapter. sqlite3 uses ?, MySQLdb uses %s, as does psycopg2.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • the db that i'm coding against is postgres so I'm told to use these statements. – Shifty Apr 29 '14 at 16:43
  • 1
    @Shifty: All Python database adapters conforming to the DB-API2 spec (all the ones you'd ever use) support SQL parameters. `psycopg2` is the most common adapter used for PostgreSQL and it [supports SQL parameters](http://initd.org/psycopg/docs/usage.html#query-parameters) using the `%s` syntax. – Martijn Pieters Apr 29 '14 at 16:46
  • @Shifty: Whomever told you to use these statements has a thing or two to learn about database parameters still. It is rarely a good idea to *not* use SQL parameters. – Martijn Pieters Apr 29 '14 at 16:47
  • understood. I appreciate the advice and will work towards improving this. – Shifty Apr 29 '14 at 16:49