select 1+e+d*10+c*100+b*1000+a*10000 as n from
(select 0 as a union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as b union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as c union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as d union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9),
(select 0 as e union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9);
#!/usr/bin/python
import apsw,tempfile
### Opening/creating database
filename=tempfile.mktemp() #insecure - do not use in production code
connection=apsw.Connection(filename)
cursor=connection.cursor()
# This gets registered with the Connection
class Source:
def Create(self, db, modulename, dbname, tablename, *args):
schema="create table foo( dummy integer )"
return schema,Table()
Connect=Create
# Represents a table
class Table:
def __init__(self):
pass
def BestIndex(self, constraints, orderbys):
used = []
self.constraints = []
ucount = 0
for c in constraints:
if c[1] in (
apsw.SQLITE_INDEX_CONSTRAINT_GT,
apsw.SQLITE_INDEX_CONSTRAINT_GE,
apsw.SQLITE_INDEX_CONSTRAINT_LT,
apsw.SQLITE_INDEX_CONSTRAINT_LE,
apsw.SQLITE_INDEX_CONSTRAINT_EQ,
):
used.append( ucount ) #tell sqlite we want to use this one
self.constraints.append( c[1] ) #save some for later
else:
used.append( None ) #skip anything we don't understand
ucount += 1
return ( used, # used constraints list
0, # index number - no biggie we only support one right now
)
def Open(self):
return Cursor(self)
def Disconnect(self):
pass
Destroy=Disconnect
# Represents a cursor
class Cursor:
def __init__(self, table):
self.table=table
def Filter(self, indexnum, indexname, constraintargs):
start = 0
self.end = 4000000000
#map constraint arguments to start and end of generation
for tc, ca in zip( self.table.constraints, constraintargs ):
if tc == apsw.SQLITE_INDEX_CONSTRAINT_EQ:
start = ca
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LE:
if self.end > ca:
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_LT:
if self.end >= ca:
self.end = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GE:
if start < ca:
start = ca
elif tc == apsw.SQLITE_INDEX_CONSTRAINT_GT:
if start >= ca:
start = ca
self.pos = start
def Eof(self):
return self.pos > self.end
def Rowid(self):
return self.pos
def Next(self):
self.pos+=1
def Close(self):
pass
# Register the module as intsource, you can make a bunch if needed
connection.createmodule("intsource", Source())
# Create virtual table to use intsource
cursor.execute("create virtual table uints using intsource()")
# Do some counting
for i in cursor.execute("SELECT rowid FROM uints WHERE rowid BETWEEN 1 AND 100"):
print i
7条答案
按热度按时间aij0ehis1#
谢谢斯格门策!受到你的回答的启发,我继续前进,也找到了this:
4ioopgfo2#
怎么样
?
kxxlusnw3#
在SQLite中生成系列1<=n<=100000的示例子查询。不创建或使用任何表。
brccelvz4#
我认为SQLite没有一种干净利落的方法来实现这一点,因此您需要使用虚拟表接口。SQLite提供了一个用于‘C’的代码,而apsw提供了一个用于Python的代码,如下所示。以下是APSW Virtual Table interface的文档。
这实现了一个名为“intsource”的虚表类型,默认情况下从0到4*10^9。它支持直接通过相等和比较进行过滤,但任何其他约束仍将被SQLite过滤掉。虚表是一个非常强大的概念,您可以用它做很多事情,这可能是它们最简单的用法之一。另外,感谢您为尝试新的虚拟表API提供了一个很好的理由。
tcomlyy65#
hiz5n14c6#
如果您的目标是从值介于1和100之间的表中选择实际记录,请使用其他受访者所示的介于两者之间。
如果您的目标是生成一个从1到100的数字序列,而没有一个表作为它的基础,我不相信SQLite有这样的功能。
pdsfdshx7#
这比使用2个WHERE子句更有效。