“太多SQL变量” django witih sqlite3中的错误 [英] "Too many SQL variables" error in django witih sqlite3

查看:259
本文介绍了“太多SQL变量” django witih sqlite3中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中使用sqlite3获取此错误:

 异常值:太多的SQL变量

我认为答案是这样的,从 http://www.sqlite.org/limits.html


许多SQL程序员很熟悉使用问号(?)作为主机
参数。 SQLite还支持以:,$或
@开头的命名主机参数,编号为?123形式的主机参数。



为了防止过多的内存分配,主机参数
号的最大值为SQLITE_MAX_VARIABLE_NUMBER,默认为999。


然而,还有一个我不明白的奇怪之处,那就是
相同的查询从django shell会话运行起来(用$ code> python
manage.py shell ),但不是当我的 views.py 发出呼叫。



这些是导致错误的代码行:

  vals = Company.objects.filter(id__in = comp_ids).values( 'id','name')。order_by('name')
names_map = SortedDict(vals)

其中 comp_ids 是一个包含1038个整数元素的集合。



完全相同的查询,更大的$ code> comp_ids (3800+)在
中运行正常的django shell(使用 python manage.py shell 启动) - 字典获取
创建,我可以通过它迭代。



我已经尝试将 comp_ids 分解成集合例如 [:996] (似乎是barb之前的
限制)ie filter(id__in = comp_ids [:996]),其余的在
下一次迭代,这将与否的主机参数
说明一致。



但是为什么它会在django shell中运行,但不能从 views.py



编辑:有些更多的信息:
将查询输入sqlite shell(manage.py dbshel​​l)返回完整的
结果集,没有错误,与django shell(管理)相同。 py shell)。



以下是参数(1039个元素)和查询的确切列表,如果您要
想知道详细信息:



params

 
(2,3,4,2053,6,7,2732 ,10,3737,13,2063,2064,17,18,21,2393,2052,29,30,32,424,55,36,38,40,2996,42,2731,45,46,205,3334 ,49号,2098号,2252号,53号,54号,58号,59号,2058号,62号,63号,64号,66号,67号,72号,2123号,3426号,3344号,79号,80号,81号,85号,2134号, 87,91,92,95,98,2747,2149,102,104,106,2155,109,110,3773,2455,2165,118,122,2171,2172,127,134,135,137,138, 2187号,2413号,145号,148号,2414号,2198号,2199号,152号,153条,154条,第2204条,第157条,第2210条,第163条,第2417条,第169条,第170条,第2219条,第172条,第173条,第3279条,第2077条,第179条, 2230,2231,184,186,187,2237,190,191,2240,2241,3877,2245,2264,2247,2228,2249,2225,205,205,207,208,209,210,213,214, 2263,2265,3450,222,2233,2274,2275,228,2227,2811,231,234,2283,2284,238,240,243,248,250,2299,2300,254,230,2305,258, 2307,2308,2309,2310,2311,2312,2313,267,269,2138,271,2320,2321,2322,2323,2325,278,2237,280,282,283,285,286,287,290,290,293,2239,2237,2308,2309,2310,2311,2312,2313,267,269,2238,271,2320,2321,2322,2323,2325,278,2237,280,282,283,285,286,287,290,290,2 2342,295,298,334,323,332,332,327,328,332,332,327,328,332,332,327,328,332,332,334,332,332,332,334,332,328,334,334,332,328,332,334,334,323,332,332,334,332,332,329,334,3328,3323,3343,2343,3323,3343,3343,3323,3323,3343,2328,3323,3343,232 2387,2388,2389,2390,2391,2392,345,2394,347,348,350,352,354,355,356,357,359,360,2410,364,365,366,2245,2416,369, 2451,373,2242,375,377,2246,2428,2429,2430,24 32,2433,387,2454,391,2441,2443,398,399,400,401,253, 2459,420,2469,2470,2472,425,427,2476,2461,2483,2485,440,441,2490,443,444,445,446,2496,2497,2499,2501,2502,455,2504, 2505,459,460,2466,2510,463,465,466,467,469,470,2519,,2520,2468,474,2529,2531, 2543,2564,2754,2564,2565,2564,2564,2564,2564,2564,2564,2564,2564,2564,2564,2564,2564,2564,2954,2564,2954,2564,2954,2564,2954,2564,2954,2564,2954,2564,2954,2564,2954,2564,2564,2564,2564,2564,2564,2564,2954,2564,2564,2564,2954,2564,2954,2564,256 2569,523,2572,526,527,529,536,538,539,2858,2589,2590,2596,550,552,556,2605,2607,2608,2612,565,566,567,568,580,556,532,556,556,532,523,572,526,523,572,526,523,529,536,539,569,556,556,532,556,556,556,532,556,556,556,532,526,556,532,523,525,526,532,523,572,526,523,529,526,523,529,586 2634,587,2363,2638,2639,2636,2638,2639,2207,595,598,2489,605,606,609,610,2407,615,618,619,620,2699,2670,2493,2672,2152,628,629, 630,2680,2682,2684,640,641,642,2691,645,648,650,2699,2700,2701,2702,2703,2705,659,2708,661,2712,667,2718,2719,2720,2620,2674,2864,2612,2674,2912,2679,2712,2720,2609,2954,2954,2954,2953,2954,2954,2954,2953,2954,2954,2954,2709,2954,2954,2953,2709,2954 2721,2723,2724,2725,2728,681,2730,26第14卷,第684号,第2733号,第2735号,第2736号,第689号,第2739号,第2741号,第2746号,第699706706707757,2757号,2759号,2760号,2508号,2763号,2766号,721号,2771号,725号,730号,2511号, 2170,734,2343,743,743,2799,2799,752,753,2515,756,2078,2807,2808,22412,763,2863,766,769,2188,2821,2822,779,2861,2833, 2834,2835,2836,2837,790,802,2852,2854,2855,2505,810,2860,813,2862,2863,817,2869,2870,2871,824,826,2876,829,830,28282, 836,2855,2886,3574,2890,2892,2893,847,2866,2872,2899,2902,2546,2909,2256,2912,2913,870,2920,2921,2922,876,2930,887,2937, 3903,892,893,894,895,896,897,898,899,900,901,903,904,905,906,907,908,909,910,911,2884,2970,927,2977,2978,2977,989,989,989,993,989,989,998,998,998,989,997,998,899,909,997,898,899,990,909,903,904,905,906,907,908,909,910,911,2884,2970,927,2977,2978,903,992,893,893,892 299,2980,2981,2545,941,942,2205,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965和965,965,965,965,959,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,949,929,949 3014,967,968,969,970,971,3234,2552,980,981,982,983,985,2895,3038,995,996,998,2897,2538,2761,3051,1012,1013,3062, 3063,3066,2559,3073,1027,1028,102 9,10,30,2421,3080,1036,1038,1093,1040,1031,3092,3246,3853,1050,3099,1057,3109,31212,1067,1068,1073,1074,1075,1076,1078,1081,1038,1037,1043,1047,1036,1078,1083,1043,1036,1078,1083,1036,1038,1033,1038,1036,1078,1037,1038,1036,1078,1037,1036,1038,1037,1038,1036,1038,1038,1038,1036,1038,1038,1038,1036,1078,1038,1038,1036,1078,1038,1038,1036,1078,1038,1038,1036,1078,10 2491,1058,1089,1090,1091,1092,1093,1095,1096,1097,1098,1099,1100,1101,1102,1103,1104,1105,1106,1107,1109,1010,1010,1010,1093,1095,1098,1098,1090,1091,1092,1093,1095,1096,1097,1098,1098,1096,1097,1098,1098,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1010,1110,1109,1010,1010,1010,1010,1010,1010,1010,1010,1010,10 1111,1112,3161,1119,3170,2235,3174,3176,3177,1132,1133,1134,1135,1136,3186,1141,3192,3193,3194,3195,3197,3198,2425,1152, 1156,3205,2924,3144,1168,1169,1171,1172,1173,1174,117,117,3225,1178,1230,1186,1189,1193,1194,1195,1196,3245,1198,1199,1212, 1202,22541,3273,1210,3259,1213,1225,1226,1228,1230,2253,3280,3281,1234,1235,1238,1243,1244,1248,1249,3298,3299,1253,3222,2257, 3304,3305,3306,3308,1262,2838,3315,3137,3338,127,1278,32727,3331,3332,1285,2945,1288,1289,1292,1295,1296,1298,33747,3484,3499,3949,3334,3294,3328,3994,3954,3954,3994,3954,3994,3954,3954,3954,3334,3304,3324,3304,3304,3304,3304,3304,3304,3334,3305,3324,3305,3304,3954,3304 1302,3351,3352,3533,1306,1307,3356,1312,3334,1320,3370,3337,3337,3337,3337,1328,3777,33 378,3380,3313,334,3388,3390,3296,3994,3395,2227,1350,2615,1357,1361,1362,31411,3412,3413,1368,2267,1371,1372,3105,1378,2228, 3428,3428,3430,3433,1400,1402,1404,3453,3455,3808,1409,3458,3459,1412,1414,1416,1417,1418,1419,1420,2093,3470,1437,34486,1439,257, 3491,3942,3943,1446,2345,1454,1463,3633,33523,1477,1478,1479,1480,1481,1482,3531,1484,1488,1489,1490,35040,1494,1495,1496,3594,3949,3994,3894,3894,3939,3939,3349,3342,3893,3893,3892,3939,3939,3939,3939,3349,3892,3893,3893,3892,3939,3939,3939,3939,3939,3349,3342,3893,3893,3893,3939 3547,2228,1510,1512,3666,1520,1523,2302,1526,1527,3584,1539,1540,1542,1543,1548,1549,3393,1554,1555,1560,1561,3079,1565,257, 3616,3617,1570,3619,3620,3621,3622,1575,3624,2817,3629,3630,1583,1590,1591,1592,296,3643,3365,2997,3649,2781,3365,3655,1608,1605,1637。 3657,2137,1618,3867,1620,3699,1625,2139,3002,2782,1633,1634,31999,3691,3466,3688,1650,1656,3007,3709,3008,1666,21616,3009,268, 2306,3723,3010,2238,1681,1682,1683,1687,3012,3738,2671,1693,3013,1697,1698,2548,3749, 3755,3757,3758,2333,1714,1718,3770,3018,1729,3778,3783,1739,1744,3793,1746,1752,1753,1755,3741,3707,1771,1774,2514,1779,2494,第3831,1788号,第2346号,第3029号,第3840号,第3182号,第2689号,第1802号,第1803号,第2690号,18​​11年,1815年,1817年,3011年,2352年,1827年,2467年,1832年,3881年,1839年,1840,2107,1848,1849,268, 2653,1855,1857,1864,1867,1872,1873,33923,1878,1882,1889,3345,1903,1904,1966,1915,1920,1921,1927,1928,1930,1931,3333,1943,1947, 1957年,1958年,1959年,1964年,1967年,1968年,1976年,2809年,3612年,1987年,1988年,1989年,3745年,1994年,2000年,2003年,2382年,2008年,3613年,2021年,2022年,3410年,3464年,2033年, 2037,2038,2729,2044)

查询

 
'SELECTscreen_company。id,screen_company。nameFROMscreen_companyWHEREscreen_companyidIN(?,?,?,? ?,??????????????????????????? ?,??????????????????????????? ?,?,?,?,?,?,?,?,?,? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,?,?,?,?,?,?,?,? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,??????????????? ,?,?,?,?,?,?,?,?,? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? ?,??????????????????????????? (,,,,)ORDER BYscreen_companynameASC'

你不需要使用pdb - django非常有帮助
在浏览器中显示回溯(在render_to_response()?)中,当发生错误时,每个
全局列表中的局部变量一步,所以你可以看到完整的查询
那里。)



然而,我曾经试过用pdb进入django代码,而
发现错误实际上源于python的pysqlite2.dbapi2(或
sqlite3.dbapi2)模块,在django / db / backends / sqlite3 / base.py的第200行:

  return Database.Cursor.execute(self,query,params)

(这也是错误页面中显示的帮助回溯),其中
数据库是pysqlite2.dbapi2或sqlite3.dbapi2的别名,取决于您的python版本的
但是我认为调用堆栈太远了以保持调试(现在),
决定停止,并开始思考解决方法,谷歌搜索
答案相反。 :

解决方案

实际上这些限制在这里给出:
https://www.sqlite.org/c3ref/c_limit_attached.html#sqlitelimitvariablenumber



说明:


单个SQL语句中的主机参数的最大数量



A host参数是在使用sqlite3_bind_XXXX()接口之一时填充
的SQL语句中的占位符。许多SQL
程序员熟悉使用问号(?)作为主机
参数。 SQLite还支持以:开头的命名主机参数,
$或@,编号为?123的主机参数。



SQLite语句中的每个主机参数都被分配一个数字。
数字通常以1开头,每个新的
参数增加一个。但是,当使用?123表单时,主机参数
number是问号后面的数字。



SQLite分配空间来保存所有主机参数介于1和
之间使用的最大主机参数号。因此,
包含主机参数(如?1000000000)的SQL语句将需要
的千兆字节存储空间。这可能很容易压倒主机
机器的资源。为了防止过多的内存分配,
a主机参数号的最大值为SQLITE_MAX_VARIABLE_NUMBER,默认为
至999。



最大主机参数号可以在运行时使用
sqlite3_limit(db,SQLITE_LIMIT_VARIABLE_NUMBER,size)接口来降低运行时间。


https://www.sqlite.org/limits.html


I'm getting this error using sqlite3 in django:

Exception Value: too many SQL variables

And I think the answer to it is this, from http://www.sqlite.org/limits.html:

Many SQL programmers are familiar with using a question mark ("?") as a host parameter. SQLite also supports named host parameters prefaced by ":", "$", or "@" and numbered host parameters of the form "?123".

To prevent excessive memory allocations, the maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999."

However, there's a remaining oddity that I don't understand, which is that the same query runs fine from a django shell session (launched with python manage.py shell), but not when the call is made from my views.py.

These are the lines of code that are causing the error:

vals = Company.objects.filter(id__in=comp_ids).values('id', 'name').order_by('name')
names_map = SortedDict(vals)

where comp_ids is a set containing 1038 integer elements.

The exact same query, with an even greater no. of comp_ids (3800+) runs fine in the django shell (launched with python manage.py shell) - the dictionary gets created, and I could iterate through it.

I've tried breaking up comp_ids into sets of e.g. [:996] (that seemed to be the limit before it barfed) i.e. filter(id__in=comp_ids[:996]), then the rest in the next iteration, which would be consistent with the "no. of host parameters" explanation.

But why would it work in the django shell but not from views.py?

EDIT: Some more information: Entering the query into the sqlite shell (manage.py dbshell) returns the full set of results with no errors, same as in the django shell (manage.py shell).

Here are the exact list of parameters (1039 elements) and the query, if you'd like to know the details:

params

(2, 3, 4, 2053, 6, 7, 2732, 10, 3737, 13, 2063, 2064, 17, 18, 21, 2393, 2052, 29, 30, 32, 2456, 35, 36, 38, 40, 2396, 42, 2731, 45, 46, 2095, 3343, 49, 2098, 2252, 53, 54, 58, 59, 2058, 62, 63, 64, 66, 67, 72, 2123, 3426, 3344, 79, 80, 81, 85, 2134, 87, 91, 92, 95, 98, 2747, 2149, 102, 104, 106, 2155, 109, 110, 3773, 2455, 2165, 118, 122, 2171, 2172, 127, 134, 135, 137, 138, 2187, 2413, 145, 148, 2414, 2198, 2199, 152, 153, 154, 2204, 157, 2210, 163, 2417, 169, 170, 2219, 172, 173, 3279, 2077, 179, 180, 181, 2230, 2231, 184, 186, 187, 2237, 190, 191, 2240, 2241, 3787, 2245, 2246, 2247, 2248, 2249, 2251, 204, 205, 207, 208, 209, 210, 213, 214, 2263, 2265, 3450, 222, 2273, 2274, 2275, 228, 2277, 2811, 231, 234, 2283, 2284, 238, 240, 243, 248, 250, 2299, 2300, 254, 2303, 2305, 258, 2307, 2308, 2309, 2310, 2311, 2312, 2313, 267, 269, 2318, 271, 2320, 2321, 2322, 2323, 2325, 278, 2327, 280, 282, 283, 285, 286, 287, 290, 2342, 295, 298, 2347, 2348, 2349, 2350, 2351, 304, 2353, 307, 309, 312, 318, 320, 321, 323, 326, 327, 328, 332, 333, 334, 2385, 338, 2387, 2388, 2389, 2390, 2391, 2392, 345, 2394, 347, 348, 350, 352, 354, 355, 356, 357, 359, 360, 2410, 364, 365, 366, 2415, 2416, 369, 2451, 373, 2422, 375, 377, 2426, 2428, 2429, 2430, 2432, 2433, 387, 2454, 391, 2441, 2443, 398, 399, 400, 401, 2553, 403, 404, 405, 406, 407, 408, 409, 410, 411, 413, 2462, 3628, 418, 2459, 420, 2469, 2470, 2472, 425, 427, 2476, 2461, 2483, 2485, 440, 441, 2490, 443, 444, 445, 446, 2496, 2497, 2499, 2501, 2502, 455, 2504, 2505, 459, 460, 2466, 2510, 463, 465, 466, 467, 469, 470, 2519, 2520, 2468, 474, 2529, 2531, 484, 486, 487, 488, 490, 491, 493, 2542, 2543, 2544, 497, 498, 499, 500, 2549, 502, 2551, 504, 505, 2554, 2555, 2556, 509, 2558, 511, 2560, 2562, 2475, 2564, 517, 2566, 2567, 2568, 2569, 523, 2572, 526, 527, 529, 536, 538, 539, 2588, 2589, 2590, 2596, 550, 552, 556, 2605, 2607, 2608, 2612, 565, 566, 567, 568, 580, 2634, 587, 2636, 2638, 2639, 2270, 595, 598, 2489, 605, 606, 609, 610, 2407, 615, 618, 619, 620, 2669, 2670, 2493, 2672, 2152, 628, 629, 630, 2680, 2682, 2684, 640, 641, 642, 2691, 645, 648, 650, 2699, 2700, 2701, 2702, 2703, 2705, 659, 2708, 661, 2712, 667, 2718, 2719, 2720, 2721, 2723, 2724, 2725, 2728, 681, 2730, 2614, 684, 2733, 2735, 2736, 689, 2739, 2741, 2746, 699, 701, 706, 707, 2757, 2759, 2760, 2508, 2763, 2766, 721, 2771, 725, 730, 2511, 732, 2170, 734, 2343, 743, 749, 2798, 2799, 752, 753, 2515, 756, 2078, 2807, 2808, 2412, 763, 2813, 766, 769, 2818, 2821, 2822, 779, 2861, 2833, 2834, 2835, 2836, 2837, 790, 802, 2852, 2854, 2856, 2550, 810, 2860, 813, 2862, 2863, 817, 2869, 2870, 2871, 824, 826, 2876, 829, 830, 2882, 836, 2885, 2886, 3574, 2890, 2892, 2893, 847, 2896, 2872, 2899, 2902, 2546, 2909, 2256, 2912, 2913, 870, 2920, 2921, 2922, 876, 2930, 887, 2937, 3903, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 903, 904, 905, 906, 907, 908, 909, 910, 911, 2884, 2970, 927, 2977, 2978, 2979, 2980, 2981, 2545, 941, 942, 2205, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 3014, 967, 968, 969, 970, 971, 3234, 2552, 980, 981, 982, 983, 985, 2895, 3038, 995, 996, 998, 2897, 2538, 2761, 3051, 1012, 1013, 3062, 3063, 3066, 2559, 3073, 1027, 1028, 1029, 1030, 2421, 3080, 1036, 1038, 1039, 1040, 1041, 3092, 3246, 3853, 1050, 3099, 1057, 3109, 3112, 1067, 1068, 1073, 1074, 1075, 1076, 1078, 1081, 2491, 1086, 2229, 1088, 1089, 1090, 1091, 1092, 1093, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 3161, 1119, 3170, 2235, 3174, 3176, 3177, 1132, 1133, 1134, 1135, 1136, 3186, 1141, 3192, 3193, 3194, 3195, 3197, 3198, 2425, 1152, 1155, 1156, 3205, 2924, 3214, 1168, 1169, 1171, 1172, 1173, 1174, 1175, 1176, 3225, 1178, 3230, 1186, 1189, 1193, 1194, 1195, 1196, 3245, 1198, 1199, 1200, 1202, 2541, 3273, 1210, 3259, 1213, 1225, 1226, 1228, 1230, 2253, 3280, 3281, 1234, 1235, 1238, 1243, 1244, 1248, 1249, 3298, 3299, 1253, 3302, 2257, 3304, 3305, 3306, 3308, 1262, 2838, 3315, 3317, 3381, 1277, 1278, 3327, 3331, 3332, 1285, 2945, 1288, 1289, 1292, 1295, 1296, 1298, 3347, 3348, 3349, 1302, 3351, 3352, 3353, 1306, 1307, 3356, 1312, 3364, 1320, 3370, 3371, 3372, 3374, 3375, 1328, 3377, 3378, 3380, 1333, 1334, 3388, 3390, 3296, 3394, 3395, 2227, 1350, 2615, 1357, 1361, 1362, 3411, 3412, 3413, 1368, 2276, 1371, 1372, 3105, 1378, 2228, 3428, 3430, 3433, 1400, 1402, 1404, 3453, 3455, 3808, 1409, 3458, 3459, 1412, 1414, 1416, 1417, 1418, 1419, 1420, 2093, 3470, 1437, 3486, 1439, 2571, 3491, 3492, 3493, 1446, 2435, 1454, 1463, 3663, 3523, 1477, 1478, 1479, 1480, 1481, 1482, 3531, 1484, 1488, 1489, 1490, 3540, 1494, 1495, 1496, 3546, 3547, 2298, 1510, 1512, 3666, 1520, 1523, 2302, 1526, 1527, 3584, 1539, 1540, 1542, 1543, 1548, 1549, 3393, 1554, 1555, 1560, 1561, 3079, 1565, 2507, 3616, 3617, 1570, 3619, 3620, 3621, 3622, 1575, 3624, 2817, 3629, 3630, 1583, 1590, 1591, 1592, 2996, 3643, 3645, 2997, 3649, 2781, 3653, 3655, 1608, 3657, 2317, 1618, 3667, 1620, 3669, 1625, 2319, 3002, 2782, 1633, 1634, 3199, 3691, 3346, 3688, 1650, 1656, 3007, 3709, 3008, 1666, 2616, 3009, 2668, 2306, 3723, 3010, 2238, 1681, 1682, 1683, 1687, 3012, 3738, 2671, 1693, 3013, 1697, 1698, 2548, 3749, 3755, 3757, 3758, 2333, 1714, 1718, 3770, 3018, 1729, 3778, 3783, 1739, 1744, 3793, 1746, 1752, 1753, 1755, 3741, 3707, 1771, 1774, 2514, 1779, 2494, 3831, 1788, 2346, 3029, 3840, 3182, 2689, 1802, 1803, 2690, 1811, 1815, 1817, 3011, 2352, 1827, 2467, 1832, 3881, 1839, 1840, 2107, 1848, 1849, 2698, 2653, 1855, 1857, 1864, 1867, 1872, 1873, 3923, 1878, 1882, 1889, 3354, 1903, 1904, 2366, 1915, 1920, 1921, 1927, 1928, 1930, 1931, 3133, 1943, 1947, 1957, 1958, 1959, 1964, 1967, 1968, 1976, 2809, 3612, 1987, 1988, 1989, 3745, 1994, 2000, 2003, 2382, 2008, 3613, 2021, 2022, 3410, 3464, 2033, 2034, 2037, 2038, 2729, 2044)

query

'SELECT "screen_company"."id", "screen_company"."name" FROM "screen_company" WHERE "screen_company"."id" IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?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?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ORDER BY "screen_company"."name" ASC'

(Incidentally, you don't need to use pdb for that - django very helpfully displays the backtrace in the browser (in render_to_response()?) when an error occurs, with a full list of the local variables at each step, so you can see the full query there.)

I had, however, previously tried stepping into the django code with pdb, and found the error was actually originating from python's pysqlite2.dbapi2 (or sqlite3.dbapi2) module, at line 200 of django/db/backends/sqlite3/base.py:

return Database.Cursor.execute(self, query, params)

(which was also in the helpful backtrace shown in the error page), where Database is an alias for either pysqlite2.dbapi2, or sqlite3.dbapi2, depending on your python version.

But I thought that was too far down the call stack to keep debugging (for now), so decided to stop, and started thinking about workarounds and googling for answers instead. :)

解决方案

actually those limits are given here: https://www.sqlite.org/c3ref/c_limit_attached.html#sqlitelimitvariablenumber

with an explanation:

Maximum Number Of Host Parameters In A Single SQL Statement

A host parameter is a place-holder in an SQL statement that is filled in using one of the sqlite3_bind_XXXX() interfaces. Many SQL programmers are familiar with using a question mark ("?") as a host parameter. SQLite also supports named host parameters prefaced by ":", "$", or "@" and numbered host parameters of the form "?123".

Each host parameter in an SQLite statement is assigned a number. The numbers normally begin with 1 and increase by one with each new parameter. However, when the "?123" form is used, the host parameter number is the number that follows the question mark.

SQLite allocates space to hold all host parameters between 1 and the largest host parameter number used. Hence, an SQL statement that contains a host parameter like ?1000000000 would require gigabytes of storage. This could easily overwhelm the resources of the host machine. To prevent excessive memory allocations, the maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999.

The maximum host parameter number can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_VARIABLE_NUMBER,size) interface.

https://www.sqlite.org/limits.html

这篇关于“太多SQL变量” django witih sqlite3中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆