2011-01-27 46 views
0

我很確定我只是錯過了一些愚蠢的小東西,但卻讓我無法繼續前進。我得到這個錯誤Django SQLite3 db在通過原始查詢調用時無法識別數學函數

"DatabaseError at/no such function: SQRT" ,

我得到這樣的錯誤每個數學函數像mod等......所以我有點困惑。請問sqlite3的有沒有數學函數,這是我的猜測是有點荒唐,還是我只是愚蠢O_O

def check_difference(self): 
    cursor = connection.cursor() 
    cursor.execute("SELECT t1.id, COUNT(t1.id) AS rep, MAX(t3.right_key) AS max_right \ 
        FROM cms_Book AS t1, cms_Book AS t2, cms_Book AS t3\ 
        WHERE t1.left_key <> t2.left_key \ 
        AND t1.left_key <> t2.right_key \ 
        AND t1.right_key <> t2.left_key \ 
        AND t1.right_key <> t2.right_key \ 
        GROUP BY t1.id \ 
        HAVING max_right <> SQRT(4 * rep + 1) + 1 ") 
    q = cursor.fetchall() 
    return q 

回答

2

SQRT是sqlite3的可通過擴展:

This library will provide common mathematical and string functions in SQL queries using the operating system libraries or provided definitions. It includes the following functions:

Math: acos, asin, atan, atn2, atan2, acosh, asinh, atanh, difference, degrees, radians, cos, sin, tan, cot, cosh, sinh, tanh, coth, exp, log, log10, power, sign, sqrt, square, ceil, floor, pi.

注意擴展加載機制可以在默認情況下(出於安全原因被關閉 - S ee ticket #1863) - 因此您需要從源代碼重新編譯sqlite3。在autoconf分配中,默認情況下啓用了擴展加載。

+0

謝謝你的回答。很有幫助 。至少現在我確定需要擴展庫。現在我不想錯過它。但只是面臨另一個問題。找不到如何將.c擴展名添加到django。也許你可以指出一些描述它的資源? – Viktor 2011-01-27 17:23:11

0

沒關係。從這裏下載MySQLdb http://www.codegood.com/archives/129,安裝到目前爲止工作正常。 >。 <不知道爲什麼它給出錯誤之前。也許sometghing是錯誤的註冊表或可能下載64位Python的32位。 =)