如何在Python中自动将JSON对象插入到SQL Server中?

问题描述 投票:0回答:1

我正在从 API 检索一些数据并将其插入本地 SQL Server 数据库以进行数据分析。

我想创建表并根据我收到的 Json 对象结构将其填充到 SQL Server 中。有没有任何lib/framework python可以自动完成它?

我正在使用 pyodbc 插入数据,如下所示。但是,我需要在 SQL Server 中手动创建每个表,并在 python 脚本上逐个字段地写下数据以插入数据。

response = requests.get(url, headers=headers , auth=auth )
parseResponse = json.loads(response.text) 

conn = pyodbc.connect(constant.DW_CONNECTION)
cursor = conn.cursor()

for record in parseResponse:
    print( "Insert Into LeadSource (RequestGuid, [Key], Description, LocationNumber, Inactive, ShowOnline) values " + json.dumps(convert(record)) )
    cursor.execute("Insert Into LeadSource (RequestGuid, [Key], Description, LocationNumber, Inactive, ShowOnline) values (?, ?, ?, ?, ?, ?)", ( record['RequestGuid'], record['Key'], record['Description'], record['LocationNumber'], record['Inactive'], record['ShowOnline'] ) )
conn.commit()
conn.close()
python sql-server etl pyodbc
1个回答
0
投票

谢谢你,戈德,

这就是我一直在寻找的。最终代码:


response = requests.get(url, headers=headers , auth=auth )
parseResponse = json.loads(response.text) 

# Converting the JSON text to Pandas Dataframe
df = pd.read_json(response.text)

# Create an in-memory SQLite database.
engine = sal.create_engine(constant.DW_STR_CONNECTION)
conn = engine.connect()

# Create and insert the entity 'leads' into DW MSSQL
df.to_sql('LeadSource', con=engine, if_exists='replace', index_label='id')

DW_STR_CONNECTION 应该是这样的:

server = hostname\\\SqlInstance

DW_STR_CONNECTION = 'mssql+pyodbc://' + username + ':' + password + '@' + server + '/'+database+'?driver=SQL Server?Trusted_Connection=yes'
© www.soinside.com 2019 - 2024. All rights reserved.