0

I am fairly new to Python and trying to figure out way to use variables from file as described below.

I have a file query.txt

query1="select count(*) from table1;" 
query2="select count(*) from table2;" 

My main program:

conn=connect_db()
print >>log,"connection successful"
c=conn.cursor()
with open('query.txt') as fp:
     for line in fp:
         print line
         i=1
         query="query"+str(i)    #If I print query I get query1

#I am looking to pass query1 as argument, to execute first query
         c.execute(query); 
         r=c.fetchone()
         print r
         i+=1

In shell I would use c.execute($query) and it would replace it with it's assigned value. How do I achieve it with Python?

Appreciate your help and guidance.

Sam S
  • 39
  • 4
  • You mean like _"If `i == 1` execute query1, if `i == 2` execute query2, else fail with a strange error message"_ ? – moooeeeep Jun 20 '18 at 08:14
  • 1
    I misunderstood the question. You want to dynamically create a variable name? Don't do that, store your queries in a dictionary with `"query1"` etc as keys and the queries as values. `query_dict = {"query1: "select count(*) from table1;", "query2": "select count(*) from table2;"}` – roganjosh Jun 20 '18 at 08:18
  • You should review the documentation of the module that provides the database API. Also have a look at this: https://stackoverflow.com/q/902408/1025391 and this: https://stackoverflow.com/a/42947632/1025391 – moooeeeep Jun 20 '18 at 08:21
  • Uh, with the different answers and my own misunderstanding, all going in different directions, I'd say this question could do with some clarification. – roganjosh Jun 20 '18 at 08:22
  • Side note: *enumerate(foo)* will return item and it’s number, no need to have *i* counter. – 0andriy Jun 20 '18 at 08:22
  • Please provide [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Braca Jun 20 '18 at 08:24
  • roganjosh, I believe this is my best bet. queries are huge and hence I wasn't inclined towards using dictionary, but looks like that is what I have to use. Thank you! – Sam S Jun 20 '18 at 20:45
  • 0andriy, I tried it with exec but will give a try with enumerate. Thank you! – Sam S Jun 20 '18 at 20:46

2 Answers2

1

Change query.txt to:

select count(*) from table1;
select count(*) from table2;

Then in Python:

for query in fp:
    c.execute(query)
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • 1
    But this doesn't assign any query to any variable name. It's just going to indiscriminately execute every query in the file. Probably the OP wants to parse each line into a dictionary so that they can choose which to execute. – roganjosh Jun 20 '18 at 08:26
  • roganjosh is right, based on result of each query, I want to put a success or failure message in email for the given validation. – Sam S Jun 20 '18 at 20:44
0

You can use the sys library for that. https://docs.python.org/2/library/sys.html?highlight=argv#sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

To loop over the standard input, or the list of files given on the command line, see the fileinput module.

Argv allows you to pass parameters from the commandline when running your script. A tutorial can be found here: https://www.tutorialspoint.com/python/python_command_line_arguments.htm

[EDIT] 2 Assumptions:

  1. You are working with python 2.7
  2. Your script runs commandline in Linux.

Not saying it won't work otherwise, but this is what I know works.

[EDIT2] Alex Hall's answer is actually what the OP needed, so focus on that one instead.

Community
  • 1
  • 1
JustLudo
  • 1,690
  • 12
  • 29