所以我正在做這個作業(yè),從 CSV 文件中獲取數(shù)據(jù)行并將其插入數(shù)據(jù)庫。根據(jù)我教授的演示視頻,我所有的代碼都是正確的,但數(shù)據(jù)仍然沒有進(jìn)入我在 MySql 中創(chuàng)建的表。我還有一行 print(cur.rowcount, "records inserted") 應(yīng)該打印我的數(shù)據(jù)庫的行數(shù)但返回 -1。任何幫助將不勝感激,謝謝?。▽Σ黄穑绻y以閱讀)import mysql.connectorimport CSVdb = mysql.connector.connect( host = "localhost", user="root", passwd = "", database = "user_cards" )cur = db.cursor()f = open("UCI_Credit_Card.csv")index = 0for row in csv.reader(f): if index==0: index+1 else: ID = row[0] LIMITBAL = row[1] SEX = row[2] EDUCATION = row[3] MARRIAGE = row[4] AGE = row[5] PAY_0 = row[6] PAY_2 = row[7] PAY_3 = row[8] PAY_4 = row[9] PAY_5 = row[10] PAY_6 = row[11] BILL_AMT1 = row[12] BILL_AMT2 = row[13] BILL_AMT3 = row[14] BILL_AMT4 = row[15] BILL_AMT5 = row[16] BILL_AMT6 = row[17] PAY_AMT1 = row[18] PAY_AMT2 = row[19] PAY_AMT3 = row[20] PAY_AMT4 = row[21] PAY_AMT5 = row[22] PAY_AMT6 = row[23] payment_next_month = row[24] sql = "INSERT INTO customers (ID, LIMITBAL, 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, 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 = (ID, LimitBal, 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,payment_next_month) cur.execute(sql, val) db.commit()print(cur.rowcount, "records inserted")db.close
CSV 文件不會導(dǎo)入到 mysql 數(shù)據(jù)庫中
繁華開滿天機(jī)
2022-12-06 15:05:21