Python 3.x 在python上使用apsw在SQLite中搜索值
为什么当我要求代码只返回第一个元素>然后是3的行时,它会返回行'p',8,9?此行的第一个元素是“p”。 我注意到这个方法可以很好地处理int,但是如果比较的元素是str,那么这个occours。为什么?如何解决这个问题?例如,我如何只获取某些元素大于3的行? 我更想知道为什么会这样 代码:Python 3.x 在python上使用apsw在SQLite中搜索值,python-3.x,sqlite,apsw,Python 3.x,Sqlite,Apsw,为什么当我要求代码只返回第一个元素>然后是3的行时,它会返回行'p',8,9?此行的第一个元素是“p”。 我注意到这个方法可以很好地处理int,但是如果比较的元素是str,那么这个occours。为什么?如何解决这个问题?例如,我如何只获取某些元素大于3的行? 我更想知道为什么会这样 代码: import apsw connection=apsw.Connection("database01") cursor=connection.cursor() cursor.exec
import apsw
connection=apsw.Connection("database01")
cursor=connection.cursor()
cursor.execute("create table foo(a,b,c)")
cursor.execute("insert into foo values(1,2,3);insert into foo values(4,5,6);insert into foo values(7,8,9);insert into foo values('p',8,9)")
for x,y,z in cursor.execute("select a,b,c from foo"):
print (cursor.getdescription()) # shows column names and declared types
print (x,y,z)
def rowtrace(*results):
"""Called with each row of results before they are handed off. You can return None to
cause the row to be skipped or a different set of values to return"""
print ("Row:",results)
return results
cursor.setrowtrace(rowtrace)
for row in cursor.execute("select a,b from foo where a>3"):
pass
输出:
(('a', None), ('b', None), ('c', None))
1 2 3
(('a', None), ('b', None), ('c', None))
4 5 6
(('a', None), ('b', None), ('c', None))
7 8 9
(('a', None), ('b', None), ('c', None))
p 8 9
Row: (<apsw.Cursor object at 0x7fab057f92b0>, (4, 5))
Row: (<apsw.Cursor object at 0x7fab057f92b0>, (7, 8))
Row: (<apsw.Cursor object at 0x7fab057f92b0>, ('p', 8))
来源:可以在中找到原因。它与a列的匹配 -由于列a具有文本关联性,因此 -比较的右侧在比较之前转换为文本 -比较发生了。 从t1中选择a<40、a<60、a<600; 0 | 1 | 1 On选项是在比较之前将a转换为整数,即casta as int>3。这只是一个选项,因为它不是一个完美的解决方案,这取决于用例。另一种选择是在高端限制a,例如a介于3和9999999之间;同样,这不是一个完美的解决方案