2011-09-12 22 views
1

看到這個存儲過程SQL_CALC_FOUND_ROWS在MySQL存儲過程中使用python

-- -------------------------------------------------------------------------------- 
-- Routine DDL 
-- -------------------------------------------------------------------------------- 
DELIMITER $$ 

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_followers`(in _user_id int,in _topic_id int,in _type int) 
MAIN:BEGIN 

SELECT SQL_CALC_FOUND_ROWS follow.user_id 
      -- COUNT(follow.user_id) AS _topic_idcount 
FROM 
    follow 
WHERE 
    follow.followee_user_id = _user_id 
    AND (topic_id = _topic_id OR topic_id = 0) 
GROUP BY follow.user_id; 
SELECT FOUND_ROWS() AS count; 

END 

當我使用測試呼叫到MySQL工作臺它給預期的結果作爲計數的數量這個存儲過程的功能。

但是,當我執行python代碼並轉儲JSON出這個查詢它提供以下結果。

[{"user_id": 3}, {"user_id": 4}, {"user_id": 5}] 

根據我的看法是不考慮SELECT FOUND_ROWS() AS count;這個說法時,我叫SP形式Python代碼爲休閒

results = execute_sp("get_followers", [user_id, topic_id, type]) 

這裏EXECUT是我的自定義功能。

def execute_sp(sp_name, paramaters): 
    #helper method to run sp's and return results 
    db = Db() 
    cursor = db.cursor() 
    cursor.callproc(sp_name,paramaters) 
    results = cursor.fetchallDict() 
    cursor.close() 
    return results 

懇求幫助我解決這個.....

回答

1

你必須要試試這個,看看它的工作原理 - 我不能在此刻測試...

results = cursor.fetchallDict()返回第一個結果集,就mysqldb而言。即來自第一個SELECT的結果。嘗試添加nextset()調用,如下所示:

def execute_sp(sp_name, paramaters): 
    #helper method to run sp's and return results 
    db = Db() 
    cursor = db.cursor() 
    cursor.callproc(sp_name,paramaters) 
    cursor.nextset() #Need the second result set in the proc 
    results = cursor.fetchallDict() 
    cursor.close() 
    return results 

讓我知道它是否無效。