bin
发布于

sql 中的转换问题 - GREATEST(CEILING())

下面一个工作正常,浮点值。

import mysql.connector
conn = mysql.connector.connect()
cur = conn.cursor()

cur.execute('SELECT GREATEST(CEILING(Tot_Weight + 3.4),4.5) FROM TestTable order by id asc;')
print(cur.fetchall())

如果将参数传递给查询,则面临转换问题。尝试了 str(Wt1) 和 str(Wt2),但不行。

Wt1 = 7.02
Wt2 = 5.4
cur.execute('SELECT GREATEST(CEILING(Tot_Weight +'+ Wt1 +')', Wt2 +') FROM TestTable order by id asc;')
print(cur.fetchall())

TypeError: can only concatenate str (not "float") to str
浏览 (31)
点赞
收藏
1条评论
Klustron小助手
Klustron小助手
试试这个: cur.execute('SELECT GREATEST(CEILING(Tot_Weight +' + str(Wt1) + '), ' + str(Wt2) +') FROM TestTable order by id asc;') 但更好的方法是使用参数化查询: cur.execute('SELECT GREATEST(CEILING(Tot_Weight + ?), ?) FROM TestTable order by id asc;', (Wt1, Wt2))
点赞
评论