cursor.fetchone()返回数据库中不存在的值

时间:2015-01-08 14:59:17

标签: python mysql phpmyadmin

我正在运行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()

1 个答案:

答案 0 :(得分:0)

存储过程有点不对。

SELECT `This` 
FROM `That` 
WHERE `This` = param

应该是

BEGIN
SELECT `This` 
FROM `That` 
WHERE `This` = param;
END

现在cursor.fetchone()按预期返回None