尝试将熊猫数据框插入临时表

mit*_*tch 3 sql-server temp-tables pyodbc python-3.x pandas

我正在寻找创建一个临时表并向其中插入一些数据。我已经广泛使用 pyodbc 来提取数据,但我不熟悉从 python 环境将数据写入 SQL。我在工作中这样做,所以我没有能力创建表,但我可以创建临时表和全局临时表。我的意图是将一个相对较小的数据帧(150 行 x 4cols)插入到临时表中并在我的整个会话中引用它,我的程序结构使得会话中的全局变量不够用。我在尝试时收到以下错误下面的一块,我做错了什么?

pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (8180)")

import numpy as np
import pandas as pd
import pyodbc


conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=SERVER;'
                      'Database=DATABASE;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()

temp_creator = '''CREATE TABLE #rankings (Col1 int, Col2 int)'''

cursor.execute(temp_creator)

df_insert = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2':[4,5,6]})
df_insert.to_sql(r'#rankings', conn, if_exists='append')
read_query = '''SELECT * FROM #rankings'''
df_back = pd.read_sql(read_query,conn)
Run Code Online (Sandbox Code Playgroud)

Dav*_*oft 7

Pandas.to_sql 在那里失败了。但是对于 SQL Server 2016+/Azure SQL 数据库,无论如何都有更好的方法。不是让 Pandas 插入每一行,而是将整个数据帧以 JSON 格式发送到服务器并将其插入到单个语句中。像这样:

import numpy as np
import pandas as pd
import pyodbc

conn = pyodbc.connect('Driver={Sql Server};'
                      'Server=localhost;'
                      'Database=tempdb;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()

temp_creator = '''CREATE TABLE #rankings (Col1 int, Col2 int);'''
cursor.execute(temp_creator)

df_insert = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2':[4,5,6]})

df_json = df_insert.to_json(orient='records')
print(df_json)

load_df = """\
insert into #rankings(Col1, Col2)
select Col1, Col2
from openjson(?)
with 
(
  Col1 int '$.Col1',
  Col2 int '$.Col2'
);
"""

cursor.execute(load_df,df_json)

#df_insert.to_sql(r'#rankings', conn, if_exists='append')
read_query = '''SELECT * FROM #rankings'''
df_back = pd.read_sql(read_query,conn)
print(df_back)
Run Code Online (Sandbox Code Playgroud)

哪个输出

[{"Col1":1,"Col2":4},{"Col1":2,"Col2":5},{"Col1":3,"Col2":6}]
   Col1  Col2
0     1     4
1     2     5
2     3     6
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)

  • 您应该在带有 [oracle] 标签的单独问题中提出这个问题,可能知道答案的人会观看该问题。 (2认同)