【发布时间】:2020-05-06 11:48:31
【问题描述】:
我正在开发一个 python 应用程序来通过 .csv 文件将数据插入到我的数据库中。但是在导入文件时会出现以下错误:
Warning: (1262, 'Row 1 was truncated; it contained more data than there were input columns')
result = self._query(query)
基本上适用于 csv 文件中的所有行。 文件里面的数据如下:
我可以将同样的方式直接导入 Workbench,但我想通过应用程序来完成。
按照下面的代码:
from tkinter import *
import pymysql
import os
import os.path
tess = Tk()
def import_file():
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='omnia')
print('connect successfull!')
if os.path.exists('C:/temp/teste.csv'):
statm = "LOAD DATA INFILE 'C:/temp/teste.csv' INTO TABLE testtable FIELDS TERMINATED BY ','"
cursor = conn.cursor()
cursor.execute(statm)
bt = Button(tess, text='browse file')
bt.place(x=10, y=10)
bt = Button(tess, text='import file', command=import_file)
bt.place(x=10, y=45)
tess.mainloop()
还有桌子:
create table testTable(
n_id int not null auto_increment,
c_nome varchar(255),
c_depto varchar(255),
n_salario float,
primary key (n_id));
【问题讨论】: