使用df.to_sql(pandas,pyodbc)时无法连接到MySQL数据库

时间:2017-08-01 20:17:47

标签: python mysql excel pyodbc

我正在尝试将多个Excel电子表格中的信息移动到SQL数据库的单个表中。首先,我使用pandas读取工作表并将它们转换为单个数据帧(此部分可用)。

def condense(sheet):
    # read all excel files and convert to single pandas dataframe
    dfs = []
    for school in SCHOOLS:
        path = filePaths[school]
        try: opened = open(path, "rb")
        except: print("There was an error with the Excel file path.")
        dataframe = pd.read_excel(opened, sheetname=sheet)
        dfs.append(dataframe)
    return pd.concat(dfs)

然后我想将数据帧上传到数据库。我已经阅读了很多文档,但仍然不知道从哪里开始。这就是我目前所拥有的。

connection = pyodbc.connect('''Driver={SQL Server};
                               Server=serverName;
                               Database=dbName;
                               Trusted_Connection=True''')
df.to_sql(tableName, connection)

我也尝试使用引擎,但我对如何格式化连接字符串感到困惑,特别是因为我不想使用密码。 (我在下面尝试使用密码,但不起作用。)

connection_string= 'mysql://username:password@localhost/dbName'
engine = create_engine(connection_string)
engine.connect()
df.to_sql(tableName, engine)

关于如何在不使用密码的情况下创建引擎或者我的代码出错的任何建议都将非常感激。

0 个答案:

没有答案