从 sqlite3 确定最大列数 [英] Determine maximum number of columns from sqlite3

查看:30
本文介绍了从 sqlite3 确定最大列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在运行时获得 sqlite3 支持的最大列数?此数据库限制是通过编译时变量 SQLITE_MAX_COLUMN 建立的(请参阅 limits一>).默认值通常为 2000 列.

Is it possible to get the maximum number of columns supported from sqlite3 at runtime? This database limitation is established with a compile-time variable SQLITE_MAX_COLUMN (see limits). The default is normally 2000 columns.

我正在寻找可从 Python 或 SQL 界面访问的内容.

I'm looking for something accessible from either the Python or SQL interface.

推荐答案

这在实践中似乎是不可能的(即,如果没有按照 dan04 相当出色的答案采用非常昂贵的蛮力方法).

This appears to be impossible in practical terms (i.e. without a very expensive brute-force approach along the lines of dan04's rather brilliant answer).

删除>

来源(12) 用于 sqlite3 模块不包含对 SQLITE_MAX_COLUMN 或一般编译时间限制;似乎也没有任何方法可以从 SQL 界面中访问它们.

UPDATE:

更新:

dan04 的解决方案进行简单修改以使用二分搜索可大大加快速度:

import sqlite3 def max_columns(): db = sqlite3.connect(':memory:') low = 1 high = 32767 # hard limit <http://www.sqlite.org/limits.html> while low < high - 1: guess = (low + high) // 2 try: db.execute('CREATE TABLE T%d (%s)' % ( guess, ','.join('C%d' % i for i in range(guess)) )) except sqlite3.DatabaseError as ex: if 'too many columns' in str(ex): high = guess else: raise else: low = guess return low

Running the code above through timeit.repeat():

通过timeit.repeat()运行上面的代码:

... 平均运行时间为 30.76/150 = 0.205 秒(在 2.6 GHz 四核机器上) - 不完全,但可能比 15 更有用-20 秒的踢它直到它打破"从一个方法开始计数.

... which comes to an average run-time of 30.76 / 150 = 0.205 seconds (on a 2.6 GHz quad-core machine) - not exactly fast, but likely more usable than the 15-20 seconds of the "kick it 'til it breaks" counting-from-one method.

这篇关于从 sqlite3 确定最大列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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