How to use double-byte characters (e.g., "()", "/", "@") in column names when inserting Python data frames into an SQL table

H.S 40 Reputation points
2023-12-26T10:31:37.85+00:00

I am considering using the pyodbc package in Python to insert a pandas data frame into an SQL database.

However, if the column names of the data to Insert contain double-byte symbols (specifically, double-byte parentheses "()", slashes "/", and at marks ”@”), I get the following error:

SyntaxError: invalid character

Can you please tell me how to work around the error?

Thank you in advance.

The following code is based on the "Insert Python dataframe into SQL table" documented by Microsoft.

An error occurs when some column names are changed to double-byte characters (the third column is changed to the double-byte character "(/)@").

Execution code (only the part that is the problematic part this time):

cursor = cnxn.cursor()
# Insert Dataframe into SQL Server:
for index, row in df.iterrows():
     cursor.execute("INSERT INTO [dbo].[inserttest] (DepartmentID,Name,(/)@) values(?,?,?)", row.DepartmentID, row.Name, row.(/)@)
cnxn.commit()
cursor.close()

Execution result (error at row.(/)@):

cursor.execute("INSERT INTO [dbo].[inserttest] (DepartmentID,Name,(/)@) values(?,?,?)", row.DepartmentID, row.Name, row.(/)@)
                                                                                                                            
SyntaxError: invalid character '(' (U+FF08)
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2023-12-26T11:51:26.93+00:00

    I can't see that it matters that you are using full-width characters. Even with plain half-width characters, this is a syntax error both on the Python side and the SQL Server side:

    cursor.execute("INSERT INTO [dbo].[inserttest] (DepartmentID,Name,(/)@) values(?,?,?)", row.DepartmentID, row.Name, row.(/)@)
    

    I'm not awfully good at Python, so I may be missing something on that part.

    If you actually have a column named (/)@, you need to do this:

    cursor.execute("INSERT INTO [dbo].[inserttest] (DepartmentID,Name,"(/)@") values(?,?,?)", row.DepartmentID, row.Name, row.["(/)@)"]
    

    That is, on the SQL Server side you need to enclose the identifier in double quotes or brackets. (I use double quotes, because they are easier to type on my keyboard), and in Python you need to use array notation.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.