Python script that takes values from a file and passes them as variables as postgres commands

StackOverflow https://stackoverflow.com/questions/22158871

  •  19-10-2022
  •  | 
  •  

Question

I'm trying to create a script that would do the following;

Take a file (we'll call it input.txt) and it is suppose to pass them into postgres sql statements.

#! /usr/bin/python
import psycopg2
#Trying to connect to DB
try:
    conn= psycopg2.connect("dbname='test' host='test' user='test' password='test'")
except:
    print "I am unable to connect to the DB."
cur = conn.cursor()
with open('input.txt') as f:
    for row in f:
            cursor.execute ('''Delete from filterstagetbl
                               where filtersetidn IN
                               (select filtersetidn from filtersettbl where name = '$s');''',
                            row)

I am looking for a better way of doing this and I'm looking for advice. Thanks for the help in advance.

No correct solution

OTHER TIPS

Does each row contain one value, or many? If many then you will have to parse the line into its individual elements first. It looks like you just need one value, but remember that the lines come in with newlines on the end, and that's going to stop you matching the database contents.

If you can't connect to the database it's all very well to print an error message, but it would be wise to terminate the execution of the program. You can to both at the same time with sys.exit().

Parameter substituion in psycopg2 uses `%s as its parameter replacement markers. The second argument to execute must always be a tuple, even if there's only one data item. So your code should look more like this:

#! /usr/bin/python
import psycopg2
#Trying to connect to DB
try:
    conn= psycopg2.connect("dbname='test' host='test' user='test' password='test'")
except:
    import sys; sys.exit("I am unable to connect to the DB.")
cur = conn.cursor()
with open('input.txt') as f:
    for row in f:
            cursor.execute ('''Delete from filterstagetbl
                               where filtersetidn IN
                               (select filtersetidn from filtersettbl where name = %s);''',
                            (row[:-1])

Lastly, the changes to the database will not be made permanent if you don't commit them, so you should finish off with

conn.commit()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top