Python Sqlite LIKE不使用带参数的索引吗?替代变量

时间:2018-07-07 22:59:15

标签: python sql sqlite sql-like substitution

为什么下面的示例不使用参数?替代变量使用索引?

我尝试了几种不同的方法,但是它们都不起作用,我在这里错过了什么?

    Python 2.7.15 (default, Jun 17 2018, 12:51:03) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin

>>> import sqlite3
>>> sqlite3.sqlite_version_info
(3, 24, 0)
>>> sqlite3.version_info
(2, 6, 0)

>>> con = sqlite3.connect("/Users/Ade/test.db")
>>> csr = con.cursor()
>>> artist = 'half man half biscuit'

>>> # This works
>>> csr.execute('EXPLAIN QUERY PLAN SELECT Tracktitle FROM tracks WHERE CleanName LIKE ' + '"' + artist + '%' + '"' + '').fetchone()
(3, 0, 0, u'SEARCH TABLE tracks USING INDEX tracks_CleanName_NoCase (CleanName>? AND CleanName<?)')

>>> # This doesn't work
>>> csr.execute('EXPLAIN QUERY PLAN SELECT TrackTitle FROM tracks WHERE CleanName LIKE ?', [artist+'%']).fetchone()
(2, 0, 0, u'SCAN TABLE tracks')

0 个答案:

没有答案