0

I am trying to push data from excel file to sql_server express management studio 2008 using pandas and pyodbc, as I need to push data by creating a table in a database myDB

and here's my code

import pyodbc
import pandas as pd
import os

import sqlalchemy
from sqlalchemy import create_engine

# connect db
engine = create_engine('mssql+pyodbc://WWX542337CDCD\SMARTRNO_EXPRESS/myDB?driver=SQL+Server+Native+Client+11.0')
cursor = engine.raw_connection().cursor()


mydir = (os.getcwd()).replace('\\', '/') + '/'

lte_details = pd.read_excel(r'' + mydir + 'MNM_Rotterdam_5_Daily_Details-20191216081027.xlsx', sheet_name='LTE Details')

lte_details.columns = lte_details.columns.str.replace(' ', '')

# reading and insert one file at a time
for file in os.listdir('.'):
    # only process excels files
    file_basename, extension = file.split('.')
    if extension == 'xlsx':
        lte_details.to_sql(file_basename.lower(), con=engine.connect(), if_exists='replace')

and I find this error:

Traceback (most recent call last):
  File "C:/Users/mwx825326/PycharmProjects/MyReference/test.py", line 22, in <module>
    file_basename, extension = file.split('.')
ValueError: not enough values to unpack (expected 2, got 1)

and this is my connection connection sql Any one have any idea how to solve this issue?

1

1 Answer 1

1

IMHO, you can try use to sqlalchemy connection. Because con in pd.read_sql uses sqlalchemy engine.

from sqlalchemy import create_engine

engine = create_engine('mssql+pyodbc://**server**/**db**?driver=SQL+Server+Native+Client+11.0')
cursor = engine.raw_connection().cursor()

lte_details.to_sql(file_basename.lower(), con=engine.connect(), if_exists='replace')

You can try this for split,

file_basename, extension = tuple(file.split('.'))
Sign up to request clarification or add additional context in comments.

7 Comments

thanks alot for your answer, but for my case I am using windows authentication no user name and password as when I tried to use your code I found this error sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') (Background on this error at: http://sqlalche.me/e/rvf5) please check edits, I am not using SQL Authentication here
Can you check this link? stackoverflow.com/questions/37161574/… I think you need to specify driver.
to be clear when I am connecting to server I an using Server type Database Engine, Servername, Authentication Windows Authentication like in the posted Picture, Check edits please
Can you try adding .raw_connection() to the end of the expression? Like this, engine = sqlalchemy.create_engine('mssql://*server_name*\\SQLEXPRESS/*database_name*?trusted_connection=yes').raw_connection()
it gave me the same error I got before, as I posted now :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.