Я пытаюсь импортировать данные из файла .csv
в таблицу SQL server
. Это прекрасно работает с SQL
в Microsoft SQL Server Management Studio (SSMS)
. Однако, когда я пытаюсь сделать это из Python с pyodbc
, это выдает мне следующую ошибку:
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0]
[SQL Server]Bulk load failed due to invalid column value in CSV data file
C:/~pathToFile~/file.csv in row 2, column 38. (4879) (SQLExecDirectW);
[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]The OLE DB
provider "BULK" for linked server "(null)" reported an error.
The provider did not give any information about the error. (7399);
[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)". (7330)')
Мой код пока:
import pyodbc
# ------------------------------------------------------
# DEFINE FUNCTIONS
# ------------------------------------------------------
# Define a database command
def DB(SQL):
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=~myServer~;"
"Database=~myDB~;"
"Trusted_Connection=yes;")
try:
with cnxn.cursor() as cursor:
cursor.execute(SQL)
cnxn.commit()
finally:
cnxn.close()
# ------------------------------------------------------
# Import from .csv file to table
# ------------------------------------------------------
sql = '''BULK INSERT dbo.~myDB~
FROM 'C:/~pathToFile~/file.csv'
WITH (
FORMAT='CSV',
FIRSTROW = 2,
ROWTERMINATOR = '\n',
FIELDQUOTE= '"',
TABLOCK
)'''
DB(sql)
И здесь первые несколько строк файла .csv, которые я пытаюсь импортировать:
SITE_ID,FIELD_SAMPLE_ID,LOCATION_ID,SAMPLE_DATE,PARAMETER_NAME,REPORT_RESULT,REPORT_UNITS,LAB_QUALIFIER,DETECTED,SAMPLE_MATRIX,SAMPLE_PURPOSE,SAMPLE_TYPE,SAMPLE_TIME,LATITUDE_(DECIMAL),LONGITUDE_(DECIMAL),FILTERED,FIELD_SAMPLE_COMMENTS,LAB_MATRIX,COC_#,LAB_METHOD,REPORT_DETECTION_LIMIT,SOURCE_FILENAME,WTR_SOURCE_FLOW,VALIDATION_QUALIFIER,VALIDATION_REASON_CODES,ANALYSIS_DATE,RESULT_TYPE,PARAMETER_CODE,LAB_RESULT,DILUTION_FACTOR,METHOD_DETECTION_LIMIT,INSTRUMENT_DETECTION_LIMIT,ANALYSIS_TYPE_CODE,ANALYSIS_TIME,QC_BATCH_SEQUENCE_#,SAMPLE_RESULT_COMMENTS,LAB_SAMPLE_ID,FIELD_SAMPLE_RESULT_RECORD_ID
"N3B","CAPA-08-11017","03-B-10","03-17-2008","RDX","0.325","ug/L","U","N","W","REG","WG","10:40","35.873716600000","-106.330115800000","N",,"W","08-824","SW-846:8321A_MOD","0.33",,"N","U","U_LAB","03-26-2008","TRG","121-82-4","0.325","2","0.13",,"INIT","00:00",,,"204935003","638"
"N3B","CAPA-08-13138","03-B-10","06-12-2008","RDX","0.325","ug/L","U","N","W","REG","WG","10:35","35.873716600000","-106.330115800000","N",,"W","08-1350","SW-846:8321A_MOD","0.33",,"N","U","U_LAB","06-24-2008","TRG","121-82-4","0.325","2","0.13",,"INIT","00:00",,,"210389014","638"
"N3B","CAPA-08-13139","03-B-10","06-12-2008","RDX","0.325","ug/L","U","N","W","FB","WG","10:35","35.873716600000","-106.330115800000","N",,"W","08-1350","SW-846:8321A_MOD","0.33",,"N","U","U_LAB","06-24-2008","TRG","121-82-4","0.325","2","0.13",,"INIT","00:00",,,"210389017","638"
Есть идеи, почему это не сработает? Опять же, он работает отлично от SSMS, только не Python / pyodb c.