What is the python doing?
I read a few books for python and read the online manual, but trying to format string output was driving me crazy. To save you some rage, my observations are that using print("text",variable,"rest of query line 1")
will always force a space to be inserted. This will not fly in SQL queries and you don't want that hassle.
Trying to avoid vomit inducing code and searching around, I found that using stdout.write, lets you define the output spacing, followed by your variable values. It took me vastly longer than I would have liked to have figured this out, but considering generating database scripts was the main goal I had for learning python, I'm pretty excited this works.
The running python code is higher in the thread, but below I explain each line, with a # comment under each line.
Line for line breakdown to build the SQL Queries:
# import sys so we can use sys.stdout.write
pstartNum = int(input("Source Port number start: "))
# Asking to have the user (You) enter a start number.
q1 = "SELECT COUNT(*) AS Port_"
# Start of SQL Query, in this case we are getting a count, instead of a display of the values matching the query.
q2 = "FROM [dbo].[zy2016-09]"
# From portion of a SQL Query
qWat = "WHERE [Column 1] like '%:"
# Query for your search criteria.
qClose = "'"
# Close the SQL Query for the like string.
for x in range(100):
# Run this loop 100 times.
sys.stdout.write("%s%s %s \n" % (q1, pstartNum, q2));
# The left side with %s placement, says grab the 1st part of the Select variable, Current iteration of loop, with a space added for the variable for 'FROM (tablename)'.
sys.stdout.write("%s%s%s \n" % (qWat, pstartNum, qClose));
# Here the %s triple string placement is to have no spaces. We call the Query, current iteration of the start number from the loop, and close the line with a "'".
# Execute each query instead of waiting for all of them to complete.
# prints a blank line. I saw some crazy code to do this by other means, but since all I want is a blank line, this is way easier and fits the bill.
pstartNum = pstartNum - 1
# Once the 1st loop finishes, subtract 1 from the starting port number. Do this for each iteration, by sayin that variable is equal to -1 from it's current state. (Please note you will error if you start with a port number less than 100.)
# stops the 100 loop, so I can copy and paste this into my SQL query window and get results.
Remember this is a breakdown of the source code from the above 3rd post
If you want this to run for 1000 iterations, change the for x in range(100)
to for x in range(1000)
or whatever works for you.
If you are not already doing this on your own equipment, you may anger some server admin with resource utilization. Or in the case of using Amazon Web Services or other hosting, your bill might get nasty expensive.