前面完全初學者寫的有點長的代碼,請幫忙
我有一個具有以下值的資料庫:
| 號碼 | 旅行 | 銷售量 | 價錢 |
|---|---|---|---|
| 1 | 5 | 20 | 220 |
| 2 | 8 | 30 | 330 |
| 3 | 9 | 45 | 440 |
| 4 | 3 | 38 | 880 |
我正在嘗試使用 mysql-connector 和 python 來獲取列的總和作為變數,并使用它在 python 腳本中進行一些計算。這是我到目前為止:
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()
我想將這三個總和作為三個變數接收sum_trips,sum_sales并sum_price為它們分配一個積分系統,以便:
trip_points=20*sum_trips
sales_points=30*sum_sales
price_points=40*sum_price
然后將這三個變數trip_points, sales_points, prices_points插入到同一個資料庫中的另一個表中Points,列名與變數名相同。
這么久以來,我一直在努力尋找這個問題的答案。任何幫助或指導將不勝感激。我是大多數這些東西的初學者,所以如果有更好的方法來實作我正在尋找的東西,請告訴我。謝謝
uj5u.com熱心網友回復:
我可能遺漏了一些東西,但是對同一個表運行 3 個不同的查詢(在非柱狀的 MySQL 中也是如此)效率不高,我可能只執行以下操作:
CREATE new_table as
(SELECT
SUM(trips) * 20 as trip_points,
SUM(sales) * 20 as sales_points,
SUM(price) * 20 as price_points
FROM table_name)
在 MySQL 提示符或 Python 中(如果您正在嘗試某種自動化)
如果這有助于您繼續前進,請告訴我。
uj5u.com熱心網友回復:
這是您的代碼,稍作更改,回傳一個包含三個值的元組,例如,(sumTrips, sumSales, sumPrice)但我故意省略了關閉連接,(請參閱在連接自動關閉的地方截取的第二個代碼):
def sum_fun():
def sumTrips(cursor): # receives the cursor object, for queries
cursor.execute("SELECT SUM(trips) FROM table_name")
sum1=cursor.fetchall()[0][0]
return int(sum1)
def sumSales(cursor):
cursor.execute("SELECT SUM(sales) FROM table_name")
sum2=cursor.fetchall()[0][0]
return int(sum2)
def sumPrice(cursor):
cursor.execute("SELECT SUM(price) FROM table_name")
sum3=cursor.fetchall()[0][0]
return int(sum3)
try:
con = mysql.connector.connect(host='localhost', password='mypasword',
database='twitterdb', user='root', charset='utf8')
cursor = con.cursor(buffered=True)
return (sumTrips(cursor), sumSales(cursor), sumPrice(cursor))
except Exception as e:
print(e)
raise Exception("The connection fail because of this error: {}".format(e))
您可能希望將其呼叫為:
sumTrips, sumSales, sumPrice = sum_fun()
這段代碼給出了相同的結果,但使用了with:
def sum_fun():
def sum_column(cursor, column):
cursor.execute(f"SELECT SUM({column}) FROM table_name")
sum1=cursor.fetchall()[0][0]
return int(sum1)
try:
with mysql.connector.connect(host='localhost', password='mypasword',
database='twitterdb', user='root', charset='utf8') as con:
with con.cursor(buffered=True) as cursor:
return (sum_column(cursor, "trips"), sum_column(cursor, "sales"), sum_column(cursor, "price"))
except Exception as e:
print(e)
raise Exception("The connection fail because of this error: {}".format(e))
上面的代碼使用了with陳述句,當blockwith結束時它會關閉連接,因為呼叫了__exit__()呼叫close()方法的方法來關閉連接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371015.html
標籤:Python mysql 蟒蛇-3.x mysql-连接器 mysql-connector-python
