Error while inserting dataframe into Sql server database using to_sql function in python

I am trying to insert pandas dataframe df into a SQL Server database using the dataframe.to_sql function. But I get below the error:

Source:

import pyodbc
import sqlalchemy
import urllib

df  #sample dataframe
params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=GIRSQL.GIRCAPITAL.com;DATABASE=Tableau;UID=SQL_User;PWD=pass")
engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
engine.connect()
df.to_sql(name='[Tableau].[dbo].[Test table]',con=engine, index=False, 
if_exists='append')

Error:

File "C: \ Users \ Arvinth \ sqlalchemy \ engine \ default.py", line 470, in do_execute cursor.execute (operator, parameters)

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ("42000", "[42000] [Microsoft] [ODBC SQL Server Driver] [SQL Server] Invalid syntax near" INTEGER ". (102) (SQLExecDirectW)) [ SQL: '\ nCREATE TABLE [[Tableau]. [Dbo]. [Test table]] (\ n \ t [A] INTEGER NULL, \ n \ t [B] INTEGER NULL, \ n \ t [C] INTEGER NULL \ n) \ n \ n ']

Example data frame:

    A  B  C
 0  0  0  0
 1  1  1  1
 2  2  2  2
 3  3  3  3
 4  4  4  4
 5  5  5  5
 6  6  6  6
 7  7  7  7

Someone can help solve the problem.

+4
2

, dbo arg pandas 'to_sql "[Tableau]. [dbo]. [Test table]" , SQL- sqlAlchemy engine CREATE TABLE SQL. , - dbo, : `'[Tableau]. [Dbo].'

, df.to_sql :

df.to_sql(name='Test table', con=engine, index=False, if_exists='append')

dbo Tableau:

CREATE TABLE [Test table] (
    [A] INTEGER NULL,
    [B] INTEGER NULL, 
    [C] INTEGER NULL
);
+3

, to_sql():

df.to_sql('test_table',con=engine, index=False, if_exists='append')
-2

Source: https://habr.com/ru/post/1683764/


All Articles