RobD
RobD

Reputation: 455

What is causing this? Lexical error at line 39, column 85. Encountered: "\r" (13), after : ""

Hello trying to insert a panda dataframe into SQL Server 2017 and I'm getting this

Lexical error at line 39, column 85.  Encountered: "\r" (13), after : ""

at this point

cursor.execute("INSERT INTO dbo.someTable([records], [id], [userid], [originid],
[targetid],[targetname], [start_date], [end_date],[next_offset], [set_id])                
values (?,?,?,?,?, ?,?,?,?,?)",  
row['records'], row['id'],row['userid'], row['usernanme'], 
row['originid'], row['targetid'], row['targetname'],
row['start_date'], row['end_date'], row['next_offset'], row['set_id'])

The rest seems ok... As far as I can tell this is correct? What Am I doing wrong? Thnks

Upvotes: 0

Views: 1376

Answers (2)

ringadingding
ringadingding

Reputation: 475

Looks like there is a line break that your code is not recognizing, It is different between unix and windows. "\r\n" is for windows/DOS vs "\n" is on unix vs "\r" is on mac. Something created on windows/DOS is passed to unix will have problem.

Upvotes: 0

Mohammed Elmahgiubi
Mohammed Elmahgiubi

Reputation: 641

Reformat your string like so:

cursor.execute(
    """
    INSERT INTO dbo.someTable([records],
    [id], [userid], [originid], [targetid], [targetname],
    [start_date], [end_date], [next_offset], [set_id])
    values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    """,
    (row['records'], row['id'], row['userid'], row['usernanme'],
    row['originid'], row['targetid'], row['targetname'],
    row['start_date'], row['end_date'], row['next_offset'], row['set_id']))

Also, it seems you are missing parentheses around your parameters.

Upvotes: 2

Related Questions