我正在使用Python 3.6.1和pypyodbc库将.csv文件导入Management Studio中的本地数据库。任何人都可以告诉我为什么这个代码在Management Studio中可以正常工作,但不能在我的pypyodbc脚本中工作?
Python脚本运行时没有任何错误,但实际上并没有将C:\MemberMapUpdates
中的新.csv文件插入CSVTest表。当我在管理工作室中运行该SQL命令时,它会按预期插入值。 任何见解表示赞赏。感谢。
import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
'Server=SERVER-SQL;'
'Database=server1;'
'uid=sa;pwd=Pa$$word!')
cursor = connection.cursor()
SQLCommand = (
'''
drop table if exists CSVTest
create table CSVTest
(dccode varchar(255),
member varchar(255),
date_sub date,
date_add date,
sa_update date,
buff_rad float,
geom varchar(255),
sub_type varchar(255),
notes varchar(255))
bulk
insert CSVTest
from 'C:\MemberMapUpdates\MemberMapUpdates.csv'
with (FIRSTROW = 3, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
'''
)
cursor.execute(SQLCommand)
connection.close()
print('Process Completed')
已插入存储过程的更新脚本</ strong>
import pypyodbc
#from osgeo import ogr
connection = pypyodbc.connect('Driver={SQL Server};'
'Server=SERVER-SQL;'
'Database=DB;'
'uid=sa;pwd=p@SSWORD!')
cursor = connection.cursor()
SQLCommand = ('exec FirstProcedure')
cursor.execute(SQLCommand)
connection.close()
print('Process Completed')
这是存储过程:
drop table if exists CSVTest;
create table CSVTest
(dccode varchar(255),
member varchar(255),
date_sub date,
date_add date,
sa_update date,
buff_rad float,
geom varchar(255),
sub_type varchar(255),
notes varchar(255));
bulk
insert CSVTest
from '\\NETWORKSHARE\MemberMapUpdates\MemberMapUpdates.csv'
with (FIRSTROW = 3, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
答案 0 :(得分:0)
这个难题的缺失部分是connection.commit()
所以这是我的完整查询,使用与上面相同的存储过程:
import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
'Server=SERVER-SQL;'
'Database=DB;'
'uid=sa;pwd=PASSWORD;')
cursor = connection.cursor()
SQLCommand = ("exec FirstProcedure;")
cursor.execute(SQLCommand)
connection.commit()
connection.close()
print('Process Completed')