Python Forum
How to save specific variable in for loop in to the database?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save specific variable in for loop in to the database?
#1
have commands like:

SELECT UserName FROM Win32_ComputerSystem
which gives me the username of computer. or SELECT Manufacturer, Model FROM Win32_ComputerSystem it gives me the manufacturer.

In this part of my code




query = f"select command_line from wmic_commands where id = '{wmic_id}'"
    cursor = connection.cursor(dictionary=True)
    cursor.execute(query)
    command_line = cursor.fetchall()
[b]    for data in command_line:
      cmd_line = data['command_line']
      output = wmic.query(cmd_line)[/b]
      result = json.dumps(output)
      mySql_insert_query = """INSERT INTO wmi_inventory (wmi_data, date) 
                                VALUES (%s, %s) """
      now = datetime.now()
      dt_string = now.strftime("%Y-%m-%d-%H:%M:%S")
      record = (result, dt_string)
      cursor.execute(mySql_insert_query, record)
      connection.commit()
I execute all these queries in my for loop. And save the output as a json data in my database. But i want to save outputs in to the database like when i execute the username commands, save it in to username column.
How can i do it? Im so confused because in my for loop I run all the commands in order.
Reply
#2
Is that your full query?
If you are able to do your select against the same table, in your example = FROM Win32_ComputerSystem

Then you should be able to insert all those values into the DB per loop.

It would be better if you are able to provide a little more code that you are using or trying to better assist..

example i found online..
mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)

mydb.commit()
I think providing more of your code would help.. im sure its just something that needs to be reorganized so that each loop inserts that set of data values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Pymodbus read and save to database stewietopg 3 1,765 Mar-02-2023, 09:32 AM
Last Post: stewietopg
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,101 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,129 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,410 Mar-09-2022, 07:20 PM
Last Post: deanhystad
  Get latest version off website and save it as variable [SOLVED] AlphaInc 5 1,888 Nov-14-2021, 09:00 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020