简体   繁体   English

SQLAlchemy df.to_sql MSSQL

[英]SQLAlchemy df.to_sql MSSQL

I am trying to move data from a database I own to a database I only have read/write permissions on (as a temp table). 我正在尝试将数据从我拥有的数据库移到我只具有(作为临时表)读/写权限的数据库。 However, I get an error. 但是,我得到一个错误。 Please see the below code: 请看下面的代码:

import pandas as pd
import pyodbc
import sqlalchemy

con = pyodbc.connect(r'Driver={SQL Server};Server=MYServer;Database=DB_People;Trusted_Connection=yes;)

sSQL = "SELECT * FROM tbl_People;"

df = pd.read_sql(sSQL, con)

con.close()

con = pyodbc.connect(r'Driver={SQL Server};Server=NOT_MyServer;Database=DB_People;Trusted_Connection=yes;)

sSQL = "CREATE TABLE [##People Table] (p_Name varchar(25), p_DOB char(10));"

con.execute(sSQL)

con.committ()

engine = sqlalchemy.create_engine('mssql://NOT_MyServer/DB_People?Driver=SQL+Server+Native+Client+11.0?trusted_connection=yes')

df.to_sql("##People Table")

I get the error: 我得到错误:

DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

Any clues as to what the issue is? 关于这个问题有什么线索吗? I specified a Driver in my connection and I know the Temp table exists because when I connect via SSMS I can query the table (although there are no records at this point. 我在连接中指定了一个驱动程序,并且我知道Temp表存在,因为当我通过SSMS连接时我可以查询该表(尽管此时没有记录。

I 一世

It seems as if I have found the problem(s): 好像我发现了问题:

  1. The "Create Table" function is unneeded as the df.to_sql creates the table df.to_sql创建表时,不需要“创建表”功能
  2. Instead of the "+" in the Driver name, I was able to replace them with %20 我可以用%20代替驱动程序名称中的“ +”

This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM