使用python 3和mysql,我正在尝试更改数据库:

尝试计算这些记录的总数:

数据中的平均年龄,最小LIMIT_BAL,最大LIMIT_BAL

我对为什么我做不到这一点感到完全困惑。到目前为止,这就是我要做的,但是我不知道下一步该怎么做。

import csv

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="hw6"
)

mycursor = mydb.cursor()


f = open("UCI_Credit_Card.csv")
for row in csv.reader(f):
    a= row[0]
    b= row[1]
    c= row[2]
    d= row[3]
    e= row[4]
    f= row[5]
    g= row[6]
    h= row[7]
    i= row[8]
    j= row[9]
    k= row[10]
    l= row[11]
    m= row[12]
    n= row[13]
    o= row[14]
    p= row[15]
    q= row[16]
    r= row[17]
    s= row[18]
    t= row[19]
    u= row[20]
    v= row[21]
    w= row[22]
    x= row[23]
    y= row[24]
    sql = "INSERT INTO customers (ID, LIMIT_BAL, SEX, EDUCATION, MARRIAGE, AGE, PAY_0, PAY_2, PAY_3, PAY_4, PAY_5, PAY_6, BILL_AMT1, BILL_AMT2, BILL_AMT3, BILL_AMT4, BILL_AMT5, BILL_AMT6, PAY_AMT1, PAY_AMT2, PAY_AMT3, PAY_AMT4, PAY_AMT5, PAY_AMT6, default_payment_next_month) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    val = (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
    mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")


mycursor = mydb.cursor(buffered=True)


#Average Age

sql_select_query = """SELECT AVG(AGE) AS AverageAge FROM customers"""
mycursor.execute(sql_select_query)
myresult = mycursor.fetchall()
print("Average Age")
for x in myresult:
    print (x)

我不确定如何保存这些值以及如何在shell中显示出来。

评论