数据未插入 MSSQL Python
Data is not inserted MSSQL Python
我正在执行 python 脚本以将文本文件添加到 MSSQL table。使用pyodbc,我正在做。结果中未插入数据。 运行.
时没有显示错误
这是我的代码,
import pyodbc
import os
from pathlib import Path
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=LAPTOP-LMBL29RS;DATABASE=mydb;UID=sa;PWD=123456')
cursor = cnxn.cursor()
path = 'testfiles'
files = os.listdir(path)
for f in files:
filepath = path +'/'+ f
filename = os.path.basename(path+'/'+filepath)
with open(filepath) as fp:
lines = fp.read().splitlines()
with open(filepath, "r") as fp:
count = 0;
for line in lines:
dataarr=[]
if (count>0):
dataarr = line.split(',')
if(len(dataarr)==8):
print(dataarr[3])
query = ('INSERT INTO final_tbl("Date","Open","High","Low","Close","Volume","OpenInt","filename") '
'VALUES(?,?,?,?,?,?,?,?)')
cursor.execute(query, dataarr[0],dataarr[1],dataarr[2], dataarr[3], dataarr[4], dataarr[5],dataarr[6],dataarr[7])
count = count+1
#print(dataarr)
循环完成后,您错过了对 cnxn.commit()
的调用。
我正在执行 python 脚本以将文本文件添加到 MSSQL table。使用pyodbc,我正在做。结果中未插入数据。 运行.
时没有显示错误这是我的代码,
import pyodbc
import os
from pathlib import Path
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=LAPTOP-LMBL29RS;DATABASE=mydb;UID=sa;PWD=123456')
cursor = cnxn.cursor()
path = 'testfiles'
files = os.listdir(path)
for f in files:
filepath = path +'/'+ f
filename = os.path.basename(path+'/'+filepath)
with open(filepath) as fp:
lines = fp.read().splitlines()
with open(filepath, "r") as fp:
count = 0;
for line in lines:
dataarr=[]
if (count>0):
dataarr = line.split(',')
if(len(dataarr)==8):
print(dataarr[3])
query = ('INSERT INTO final_tbl("Date","Open","High","Low","Close","Volume","OpenInt","filename") '
'VALUES(?,?,?,?,?,?,?,?)')
cursor.execute(query, dataarr[0],dataarr[1],dataarr[2], dataarr[3], dataarr[4], dataarr[5],dataarr[6],dataarr[7])
count = count+1
#print(dataarr)
循环完成后,您错过了对 cnxn.commit()
的调用。