使用python将CSV文件写入SQL表

时间:2019-07-10 10:19:31

标签: django python-3.x pyodbc

嗨,我正在尝试使用python将csv文件写入SQL Server数据库中的表中。我面临错误。这是我正在执行的代码。

CSV文件包含500多行

def LoadFile(self, hierarchySetId, fileName):
        try:
            with open('csvfilePath', 'r') as f:
                reader = csv.reader(f)
                columns = next(reader)
                query = (
                    'INSERT INTO DummyTprFileType (RBNode1,RBNode2,RBNode3,RBNode4,RBNode5,RBNode6,RBNode7,BusinessSegment,DividendPartner,Region,TPRRBNode1,TPRRBNode2,TPRRBNode3,TPRRBNode4,TPRRBNode5,TPRRBNode6,TPRRBNode7,TPRRBNode8,TPRRBNode9,TPRRBNode10,TPRRBNode11,TPRRBNode12) values ({1})'
                )
                cursor = connection.cursor()
                for data in reader:
                    cursor.execute(query, data)
                cursor.commit()

        except pyodbc.Error as err:
            print('Error')
            print(err.args[0])

        return True

错误:

TypeError at /api/tprMappings/loadMappingsFile/
not all arguments converted during string formatting
Request Method: GET

PyCharm终端显示如下:

(0.000) QUERY = 'INSERT INTO DummyTprFileType (RBNode1,RBNode2,RBNode3,RBNode4,RBNode5,RBNode6,RBNode7,BusinessSegment,DividendPartner,Region,TPRRBNode1,TPRRBNode2,TPRRBNode3,TPRRBNode4
,TPRRBNode5,TPRRBNode6,TPRRBNode7,TPRRBNode8,TPRRBNode9,TPRRBNode10,TPRRBNode11,TPRRBNode12) values ({2})' - PARAMS = (); args=['IST', 'Global Gas', 'Canada', '', '', '', '', '', 'Post
Dividend', 'Americas', 'IST', 'Global Oil', 'Global Crude', 'Canada', 'Canada Crude Trading', '1C', '', '', '', '', '', '']

0 个答案:

没有答案