如何使用Windows身份验证使用Pyodbc,to_sql和SQL Server Managment Studio Express R2从工作表Excel文件中推送数据

时间:2019-12-27 19:42:05

标签: python pandas pyodbc

我正在尝试使用pandaspyodbc将数据从excel文件推送到sql_server Express Management Studio 2008,因为我需要通过在数据库myDB中创建表来推送数据< / p>

这是我的代码

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')

我发现此错误:

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)

这是我的联系 connection sql 有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

恕我直言,您可以尝试使用df=pd.DataFrame(df.apply(lambda x : sorted(x,key= lambda s: ':' in s),1).tolist(),columns=df.columns) Out[119]: c a d b 0 0 10 22:58:00 23:27:00 1 10 17 23:03:00 23:39:00 2 100 10 22:58:00 23:27:00 3 10 17 23:03:00 23:39:00 连接。因为self.display.text = "wait" // do task in background queue like DispatchQueue.global(qos: .background).async { callSlow() DispatchQueue.main.async { self.display.text = "done" } } 中的sqlalchemy使用con

pd.read_sql

您可以尝试将其拆分,

sqlalchemy engine