python中使用pymssql的sql数据插入查询出现语法错误

g6ll5ycj  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(273)

我用python创建了一个到pymysql的连接,然后在其中编写了下面的代码。

import pymssql  
import pandas as pd
conn = pymssql.connect(server='', user='', password='', database='Projects')
cursor = conn.cursor()
data = pd.read_excel('C:\\Users\\admin\\Documents\\test.xlsx')
cols = "`,`".join([str(i) for i in data.columns.tolist()])

# Insert DataFrame records one by one.

for i,row in data.iterrows():
    sql = "INSERT INTO `Projects.[Source].[NPC-7]` (`" +cols + "`) VALUES (" + "%s,"*(len(row)-1) + "%s)"
    print(sql, tuple(row))
    cursor.execute(sql, tuple(row))

上面的代码给出了语法错误。错误:(102,b“'''.db lib错误消息20018,严重性15:\n常规sql server错误:检查来自sql server的消息\n”)
我试着在里面打印查询,下面是它提供的内容:

INSERT INTO `Projects.[Source].[NPC-7]` (`number`,`assigned_to`,`caller_id#user_name`,`state`,`assignment_group`,`business_duration`,`business_service`,`category`,`u_category_structure`,`u_resolution_ci`,`resolved_by`,`u_resolver_group`,`u_service_provider`,`sys_created_on`,`caller_id`,`u_caller_first_name`,`u_caller_last_name`,`u_vendor_category`,`rfc`,`closed_at`,`closed_by`,`contact_type`,`sys_created_by`,`u_customer_portal_state`,`description`,`u_first_call_resolution`,`u_first_time_fix`,`impact`,`u_incident_age`,`knowledge`,`reopened_by`,`location`,`u_on_behalf_of`,`opened_at`,`opened_by`,`priority`,`reassignment_count`,`u_resolution_method`,`close_code`,`close_notes`,`calendar_stc`,`resolved_at`,`u_sd_esclation`,`sla_due`,`severity`,`short_description`,`reopen_count`,`subcategory`,`sys_updated_by`,`urgency`,`sys_updated_on`,`assigned_to#active`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) ('IN01422990', 'Prasann Bansode', 'store.systems.monitoring', 'Closed', 'RIS - SERVICE DESK-ENG', 28800, 'Store Systems', 'ISP Offline', 'CATS0006406', nan, 'Perim Ben', 'RIS - SERVICE DESK-ENG', nan, Timestamp('2020-07-12 09:42:42'), 'Store systems Monitoring', nan, nan, 'ISP Offline', nan, Timestamp('2020-04-18 22:00:13'), 'Perim Ben', 'Auto-generated', 'store.systems.monitoring', 'Closed', 'ISP heartbeat missing for more than 30mins/Network Offline', 'Yes', False, '1 - High', '0-6 hours', False, nan, '7-ELEVEN STORE - 10872', nan, Timestamp('2020-04-13 09:42:42'), 'Store systems Monitoring', '1 - Critical', 1, nan, 'Solved (Permanently)', 'Issue resolved in ref to IN01423462 as duplicate case', 43481, Timestamp('2020-04-13 21:47:23'), nan, 'UNKNOWN', '3 - Low', 'ISP heartbeat missing for more than 30mins/Network Offline', 0, nan, 'zthar001', '1 - High', Timestamp('2020-04-23 07:42:16'), True)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题