将pandas数据框插入SQL临时表

时间:2016-03-29 17:05:36

标签: python sql-server pandas

我正在尝试在SQL数据库中创建一个临时表,并从pandas数据帧填充表。我在使用df.to_sql填充临时表时收到错误。谢谢你的帮助。

import pandas as pd
from sqlalchemy import create_engine
import pandas.io.sql as psql
import urllib

params = urllib.quote_plus("DRIVER={SQL Server};SERVER=ServerAddressHere;DATABASE=DatabaseNameHere;Trusted_Connection=yes")

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
resoverall = connection.execute('''SELECT DISTINCT
a.CountryRegionID AS ISO_Short,
b.Name
FROM
CustTable AS a
LEFT JOIN AddressCountryRegion AS b
ON b.CountryRegionID = a.CountryRegionID''')


Countries= pd.DataFrame(resoverall.fetchall())
Countries.columns = resoverall.keys()

Countries= pd.Countries['ISO_Short'].str.upper()

Countries= pd.DataFrame(data=Countries)

temp = connection.execute('''
create table #tempTable
(
ISO_Short varchar(5)
)
''')

Countries.to_sql('Countries',engine)

我收到的错误是:

  

ProgrammingError:(pyodbc.ProgrammingError)(' 42000'," [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server] CREATE TABLE权限在数据库中被拒绝' databasename'(262)(SQLExecDirectW)")[SQL:u' \ nCREATE TABLE [Countries](\ n \ t [index] BIGINT NULL,\ n \ t [ISO_Short] VARCHAR(max )NULL \ n)\ n \ n'

更新:

我想到的另一个选项是使用Pyodbc并将States转换为字典,然后将字典值传递到临时表中。使用这种方法,一切都有效,直到我尝试将字典传递给临时表。我使用这种方法有以下代码:

import pandas as pd
import pyodbc
import pandas.io.sql as psql

cnxn = pyodbc.connect('''DRIVER={SQL Server};SERVER=telsmith;
DATABASE=DatabaseNameHere;Trusted_Connection=yes;''')
cursor = cnxn.cursor()

Countries= '''
SELECT DISTINCT
a.CountryRegionID AS ISO_Short,
b.Name
FROM
CustTable AS a
LEFT JOIN AddressCountryRegion AS b
ON b.CountryRegionID = a.CountryRegionID
'''

Countries= psql.read_sql(Countries, cnxn)


Countries= Countries['ISO_Short'].str.upper()

Countries= pd.DataFrame(data=Countries)

Countriesdict = Countries.to_dict()


Temp = '''
create table #tempTable
(
    ISO_Short varchar(5)
)

'''

cnxn.commit()

# This is where I run into difficulty
placeholders = ', '.join(['%s'] * len(Countriesdict ))
columns = ', '.join(Countriesdict .keys())
sql = "INSERT INTO #tempTable VALUES ( %s )" % (placeholders)
cursor.execute(sql, Countriesdict.values())

3 个答案:

答案 0 :(得分:1)

这可能听起来有些愚蠢,但看看错误:

  

ProgrammingError:(pyodbc.ProgrammingError)(' 42000'," [42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server] CREATE TABLE权限在数据库中被拒绝' databasename'(262)(SQLExecDirectW)")[SQL:u' \ nCREATE TABLE [Countries](\ n \ t [index] BIGINT NULL,\ n \ t [ISO_Short] VARCHAR(max )NULL \ n)\ n \ n'

您是否有任何名为databasename的数据库?由于它无法找到数据库,因此无法创建表。我运行相同的代码,它工作得很好。我相信这是

的原因

答案 1 :(得分:0)

严格来说不是SQLAlchemy关注的问题。你需要获得" CREATE TABLE" DBA上的服务器权限用于某些用户名和密码,并使用这些凭据访问您的数据库。尝试包括" UID = uname; PWD = pword;"在你的参数中获得一些许可证书。

答案 2 :(得分:0)

我可能有一个对我有用的解决方案:

from sqlalchemy import create_engine
import urllib
params = urllib.parse.quote_plus("DRIVER={SQL Server};SERVER=10.233.6.52;DATABASE=databaseName;UID=xxx;PWD=Welcome1!")

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
df.to_sql('tempTable',engine)