import mysql.connector
from mysql.connector import Error
def my_function(id):
a = 0
try:
connection = mysql.connector.connect(host='222.22.22.22',
database='l2222',
user='2222',
password='22', charset='utf8')
# sql = "select sum(time) from idletimehistory AS vsum where
vehicleid= %s and idle=0"
sql = "SELECT SUM(gas*.84*(TIME/3600)) FROM idlexxxx WHERE
vehicleid=%s AND idle =0"
adr = (id,)
cursor = connection.cursor()
cursor.execute(sql, adr)
records = cursor.fetchall()
# print("Total number of rows in Laptop is: ", cursor.rowcount)
print("\nPrinting each record")
for row in records:
a = a + 1
vid = row[0]
print("sum = ", id, row[0], )
sql2 = "update vehicles set savings = %s where id = %s "
adr2 = (vid, id,)
cursor.execute(sql2, adr2)
connection.commit()
except Error as e:
print("Error reading data from MySQL table", e)
a = 0
try:
connection = mysql.connector.connect(host='12222226',
database='2222',
user='2222',
password='22', charset='utf8')
sql_select_Query = "select id from vehicles"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print("Total number of rows in vehicles is: ", cursor.rowcount)
print("\nPrinting each laptop record")
for row in records:
a = a + 1
vid = row[0]
my_function(vid) # call the function with the id number
except Error as e:
print("Error reading data from MySQL table", e)
finally:
if (connection.is_connected()):
connection.close()
cursor.close()
print("MySQL connection is closed.", a)
print(a)
|