JackSparrow
10 years agoFrequent Contributor
adding a (python) list to project suite variable from database
Hi All,
Am trying to fetch the values from database which am able to do now after fetching, I want to add those values as a list in to single project suite variable but here its storing only the last loop variable.
- And if when there is space like in between South East ,from DB it is unable to fetch the particular value I tried concatenation every thing but its not working.
- area of i in the below code contains = North, South, East , South East , West.
def dbarea():
for i in range(0,5):
area = ProjectSuite.Variables.Location
#DBquery =ProjectSuite.Variables.Deal_DBQuery
area = area.split(",")
Log.Message(str(area[i]))
AConnection = ADO.CreateADOConnection()
# Specify the connection string
AConnection.ConnectionString = "Provider=MSDASQL.1;" + \
"Data Source=LMk2 Penelope";
#AConnection.ConnectionString = "Provider=MSDASQL.1;" + \
#"Data Source=itvuat";
AConnection.LoginPrompt = False
AConnection.Open()
#Log.Message((area[i]))
# Execute a simple query
RecSet = AConnection.Execute_('select sare_no from sare where short_name='+str(area[i]))
RecSet.MoveFirst();
data_container = []
r= []
while not RecSet.EOF:
# rows1 = ""
rows = RecSet.Fields.Item["Sare_no"].Value
data_container.append(rows)
# rows = rows.append(",")
#rows.split(",")
#Log.Message(rows)
RecSet.MoveNext()
AConnection.Close()
Log.Message(str(data_container))
dbcount = len(data_container)
Log.Message("The Database Count is :"+str(dbcount))
ProjectSuite.Variables.AddVariable("Var5","String")
ProjectSuite.Variables.Var5 = str(data_container)
Log.Message(ProjectSuite.Variables.Var5)Kindly let me know any suggestions
...but here its storing only the last loop variable.
This is because you have used a wrong indentation as well as have declared variables in the wrong places. Try this:
def dbarea(): area_string = ProjectSuite.Variables.Location all_areas = area_string.split(",") data_container = [] for area in all_areas: AConnection = ADO.CreateADOConnection() AConnection.ConnectionString = "Provider=MSDASQL.1;" + \ "Data Source=LMk2 Penelope" AConnection.LoginPrompt = False AConnection.Open() RecSet = AConnection.Execute_('select sare_no from sare where short_name={}'.format(area)) RecSet.MoveFirst() while not RecSet.EOF: rows = RecSet.Fields.Item["Sare_no"].Value data_container.append(rows) RecSet.MoveNext() AConnection.Close() dbcount = len(data_container) Log.Message("The Database Count is : {}".format(dbcount)) ProjectSuite.Variables.AddVariable("Var5","String") ProjectSuite.Variables.Var5 = str(data_container)Also note, that splitting of your string: "North, South, East , South East , West" will return:
["North", " South", " East ", " South East ", " West"]
With all spaces. So be careful.