-1

I have a query that reaches into a MySQL database and grabs row data that match the column "cab" which is a variable that is passed on from a previous html page. That variable is cabwrite.

SQL's response is working just fine, it queries and matches the column 'cab' with all data point in the rows that match id cab.

Once that happens I then remove the data I don't need line identifier and cab.

The output from that is result_set.

However when I print the data to verify its what I expect I'm met with the same data for every row I have.

Example data:

Query has 4 matching rows that is finds

This is currently what I'm getting:

> data =
> ["(g11,none,tech11)","(g2,none,tech13)","(g3,none,tech15)","(g4,none,tech31)"]
> ["(g11,none,tech11)","(g2,none,tech13)","(g3,none,tech15)","(g4,none,tech31)"]
> ["(g11,none,tech11)","(g2,none,tech13)","(g3,none,tech15)","(g4,none,tech31)"]
> ["(g11,none,tech11)","(g2,none,tech13)","(g3,none,tech15)","(g4,none,tech31)"]

Code:

cursor = connection1.cursor(MySQLdb.cursors.DictCursor)

cursor.execute("SELECT * FROM devices WHERE cab=%s " , [cabwrite])
result_set = cursor.fetchall()
data = []
for row in result_set:
    localint = "('%s','%s','%s')" % ( row["localint"], row["devicename"], row["hostname"])
    l = str(localint)
    data.append(l)
print (data)

This is what I want it too look like:

data = [(g11,none,tech11),(g2,none,tech13),(g3,none,tech15),(g4,none,tech31)]
tripleee
  • 175,061
  • 34
  • 275
  • 318
pcam
  • 95
  • 7
  • How u get 4 list – Smart Manoj Jun 11 '19 at 03:51
  • I'm pretty sure the code you're sharing and the output you're presenting don't match (for one, your print statement only prints `data`, but your example includes `data = `). If you provide the actual output, that may make it easier to see where you're going wrong. – Grismar Jun 11 '19 at 03:51
  • u want to remove quotes? – Smart Manoj Jun 11 '19 at 03:52
  • Possible duplicate of [Convert string to variable in python?](https://stackoverflow.com/questions/45396799/convert-string-to-variable-in-python) – Smart Manoj Jun 11 '19 at 04:03
  • @SmartManoj It's pretty clear that they want four different strings in the list instead of the same string four times. – tripleee Jun 11 '19 at 04:08
  • That's exactly what I'm looking for @tripleee I can't post the actual data it would be entirely too long. The example data is formatted identically to what I'm getting. – pcam Jun 11 '19 at 04:12
  • 2
    The code you posted does not seem to produce what you claim it does, though. Please try to refactor this into a [mre]. – tripleee Jun 11 '19 at 04:14
  • (Also, `localint` is already very much a string; copying it and coercing it to a string doesn't do anything useful.) – tripleee Jun 11 '19 at 04:15
  • @user1051850 I can see you've changed the question, but your "what I'm getting" is not really "what you're getting", since it's not something your code would produce. Can you please provide the actual output of your script (if that's what you're sharing) or the actual command you enter on the CLI and the actual output Python prints, instead of some 'nicer' version of it? – Grismar Jun 11 '19 at 04:31

1 Answers1

0
["('Gi3/0/13','None','TECH2_HELP')", "('Gi3/0/7','None','TECH2_1507')", "('Gi1/0/11','None','TECH2_1189')", "('Gi3/0/35','None','TECH2_4081')", "('Gi3/0/41','None','TECH2_5625')", "('Gi3/0/25','None','TECH2_4598')", "('Gi3/0/43','None','TECH2_1966')", "('Gi3/0/23','None','TECH2_2573')", "('Gi3/0/19','None','TECH2_1800')", "('Gi3/0/39','None','TECH2_1529')"]

Thanks Tripleee did what you recommended and found my issue... legacy FOR clause in my code upstream was causing the issue.

pcam
  • 95
  • 7