我正在从Python运行一个存储过程,该存储过程在MYSQL数据库上执行SELECT查询,以确定记录是否已存在。
执行此操作后,我使用cursor.fetchone()将查询结果与变量值进行比较。但是,当我检查查询结果中保存的值时,它与我随查询传递的参数相同,即使该记录在数据库中不存在。
当然应该是None
吗?
为了便于记录,我传递的参数的方向为IN,并且为int。
我检查了SO的其余部分,发现的所有此类问题都是我所得到的相反的结果!我还研究了cursor.fetchone(),以确保我正确地使用它,而且看来我是正确的。
任何帮助将不胜感激!
我正在查询的表
ID = 1
名称=名称
存储过程
CREATE PROCEDURE `storedprocedure2`(IN `param` INT) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER
SELECT `This`
FROM `That`
WHERE `This` = param
查询电话
def loadintodatabase(self, connection, args):
value1 = args[0]
value2 = args[1]
value3 = args[2]
value4 = args[3]
value5 = args[4]
value6 = args[5]
value7 = args[6]
value8 = args[7] #this is a list
value8length = len(value8)
value9 = args[8] #this is also a list
value9length = len(value9)
id = 0
i = 0
idlist = [] #this is not related to the id variable
idlistincrement = 0
if value 8 != []:
while i < value8length:
cursor = connection.cursor()
cursor.callproc('storedprocedure1', (value8[0], value8[1], value8[2])) #Inserts into a different table
idlist.append(value8[0])
cursor.close()
connection.commit()
i += 1
i = 0
if value3 != 0 and value4 is not None:
cursor = connection.cursor()
cursor.callproc('storedprocedure2', (param,)) #lets say param holds 11 as an int, sproc is a select query
result = cursor.fetchone() #result should be None but instead is (11L,)
if result is None:
id = None
else:
id = result[0]
if id is None:
cursor = connection.cursor()
cursor.callproc('storedprocedure3', (value3, value4)) #sproc inserts into the table I just selected the id from
cursor.close()
connection.commit()
最佳答案
存储过程略有错误。
SELECT `This`
FROM `That`
WHERE `This` = param
本来应该
BEGIN
SELECT `This`
FROM `That`
WHERE `This` = param;
END
现在cursor.fetchone()返回预期的
None
!