看不懂,是哪个问题导致的:
将数据类型 nvarchar 转换为 float 时出错。
我正在通过 api 从一个软件获取数据,并想将这些数据插入到 sql 数据库中。 我在探查器中看到的……并怀疑,十进制或整数的问题
我试过替换,但后来我得到了相同的结果和
VALUES(?,?,convert(int,replace(?,',','.')),?,?, convert(decimal(18,2),replace(?,',','.')),?,?,?,?,?) END"
将数据类型 nvarchar 转换为数字时出错。
for t in tuples:
print(t)
use = t[0]
btiId = t[1]
area = None
if t[2] is not None:
area = round(float(str(t[2])[:str(t[2]).find('.')+2]),2)
else:
area = None
print(area)
number = t[5]
name = t[3]
building = None
floor = None
premise = None
if t[8] is not None:
building = t[8][0]['name']
else:
bulding = None
if t[9] is not None:
floor = t[9][0]['name']
else:
floor = None
t = (btiId, building, floor, number, premise, name, area, area, area, 'BPS', 'Коммерческие', 'БПС',use, btiId,area,btiId)
print(t)
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
query = "IF EXISTS (SELECT * FROM [dbo].[premises] WHERE btiId = ?) BEGIN \
UPDATE [dbo].[premises] SET area = ? WHERE btiId = ? END \
ELSE BEGIN INSERT INTO [dbo].[premises] ([building], [floor], [number], [premise], [name],\
[area],[owner],[purpose],[ownershipCode],[used],[btiId])\
VALUES(?,?,convert(int,?),?,?, case when isnumeric(?)=1 then convert(decimal(18,2),round(?,2)) else ? end,?,?,?,?,?) END"
cursor.execute(query,t)
cnxn.commit()
cursor.close()