gpt4 book ai didi

python - cursor.fetchone() 返回一个不在数据库中的值

转载 作者:行者123 更新时间:2023-11-29 23:15:37 24 4
gpt4 key购买 nike

我正在运行一个来自 Python 的存储过程,该过程在 MYSQL 数据库上执行 SELECT 查询以确定记录是否已存在。

执行后,我使用cursor.fetchone() 将查询结果与变量的值进行比较。但是,当我检查查询结果中保存的值时,它与我在查询中传入的参数相同,即使数据库中不存在该记录。

这当然应该是

郑重声明,我传递的参数的方向为 IN,并且是一个 int。

我已经检查了其余部分,我看到的所有类似问题都与我得到的相反!我还研究了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!

关于python - cursor.fetchone() 返回一个不在数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27843042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com