I write script in Python and I have some problems, I need to run a command with a parameter that I get from the database, I did so (addition mysql.connector installed):
import mysql.connector
import os
mydb = mysql.connector.connect(
host="localhost",
user="name",
password="pass",
database="base"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM `employee` LIMIT 2")
myresult = mycursor.fetchall()
for row in myresult:
os.system('command ' + row[1])
I have 3 questions:
- is it correct that I use os and not subprocess?
- I get an answer in json format when I run this command, how do I get the value from there? Do I need to include "import json"?
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])