1

Hi I am trying to write a csv file into a table in SQL Server database using python. I am facing errors. Here is the code I am executing.

CSV file contains more than 500 rows

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

Error:

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

PyCharm Terminal shows below:

(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', '', '', '', '', '', '']
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Paras
  • 63
  • 1
  • 6
  • `... values ({1})` provides a place for you to format in the parameter placeholders but you never do it, so your `query` gets processed as-is. You need to update it so it looks like `... values (?,?,?, ...)` – Gord Thompson Jul 10 '19 at 14:07

0 Answers0