帮助大家!我在将数据插入 MySQL 数据库时遇到这个问题。我不断收到此错误
连接到 MySQL 1264 (22003) 时出错:第 1 行的“Encours”列的值超出范围 MySQL 连接已关闭
我正在执行这段 python 代码。我正在做的是抓取网页而不是格式化它以将其插入数据库
rows = []
# loop over results
for 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")
智慧大石
相关分类