Kinda long code by complete beginner ahead, please help out
I have a database with the following values:
| Sl.No | trips | sales | price |
|---|---|---|---|
| 1 | 5 | 20 | 220 |
| 2 | 8 | 30 | 330 |
| 3 | 9 | 45 | 440 |
| 4 | 3 | 38 | 880 |
I am trying to use mysql-connector and python to get the sum of the columns trips, sales and price as variables and use it to do some calculations in the python script. This is what I have so far:
def sum_fun():
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='mypasword', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
def sumTrips():
cursor.execute("SELECT SUM(trips) FROM table_name")
sum1=cursor.fetchall()[0][0]
return int(sum1)
def sumSales():
cursor.execute("SELECT SUM(sales) FROM table_name")
sum2=cursor.fetchall()[0][0]
return int(sum2)
def sumPrice():
cursor.execute("SELECT SUM(price) FROM table_name")
sum3=cursor.fetchall()[0][0]
return int(sum3)
except Error as e:
print(e)
cursor.close()
con.close()
I would like to receive the the three sums as three variables sum_trips, sum_sales and sum_price and assign a point system for them such that:
trip_points=20*sum_trips
sales_points=30*sum_sales
price_points=40*sum_price
And then take the three variables trip_points, sales_points, prices_points and insert it into another table in the same database named Points with the column names same as variable names.
I have been trying so hard to find an answer to this for so long. Any help or guidance would be much appreciated. I am a total beginner to most of this stuff so if there's a better way to achieve what I am looking for please do let me know. Thanks
SELECT SUM(trips), SUM(sales), SUM(price) FROM table_name...?