幫助大家!我在將數據插入 MySQL 數據庫時遇到這個問題。我不斷收到此錯誤連接到 MySQL 1264 (22003) 時出錯:第 1 行的“Encours”列的值超出范圍 MySQL 連接已關閉我正在執行這段 python 代碼。我正在做的是抓取網頁而不是格式化它以將其插入數據庫 rows = []# loop over resultsfor result in results:# find all columns per result data = result.find_all('td')# check that columns have data if len(data) == 0: continue # if len(data)!=0 execute the rest ISIN = data[1].getText() Libelle = data[2].getText() Nominal = normalize('NFKD',data[4].getText()).replace(' ','') Encours = normalize('NFKD',data[5].getText()).replace(' ','') DerniereEcheance = data[6].getText() InterestRate = data[7].getText().replace('%','').replace(',','.') nom = int(Nominal) enc=Encours.strip() date_time_obj = datetime.datetime.strptime(str(DerniereEcheance).strip(), '%d/%m/%Y').strftime('%Y-%m-%d') intRate = float(InterestRate) rows.append([ISIN,Libelle,nom,enc,date_time_obj,intRate])print(rows)driver.quit()"""connection"""try: connection = mysql.connector.connect(host='localhost', database='biatfinancialdata', user='root', password='root') if connection.is_connected(): cursor = connection.cursor() for row in rows: sql = "INSERT INTO t_bta (EffectiveDate, ISIN, Libelle, Nominal, Encours,DerniereEcheance,InterestRate) VALUES (%s, %s, %s, %s, %s, %s, %s)" val =(datetime.date.today(),row[0],row[1],row[2],row[3],row[4],row[5]) cursor.execute(sql,val) print('im here') connection.commit()except Error as e: print("Error while connecting to MySQL", e)finally: if (connection.is_connected()): cursor.close() connection.close() print("MySQL connection is closed")
1 回答

智慧大石
TA貢獻1946條經驗 獲得超3個贊
當嘗試放置的值大于列的變量類型規范所允許的值時,通常會發生此錯誤。您沒有提到女巫列是錯誤的根源,但我們可以假設它是 varchar 字段類型,并且顯然,您超出了分配的長度。檢查輸入值和表列規格。每個字段類型都會發生此錯誤。
添加回答
舉報
0/150
提交
取消