sqlite3 version lacks instr

Peter Otten __peter__ at web.de
Sun Jul 28 21:14:25 CEST 2013


Joseph L. Casale wrote:

>> Has anyone encountered this and utilized other existing functions
>> within the shipped 3.6.21 sqlite version to accomplish this?
> 
> Sorry guys, forgot about create_function...

Too late, I already did the demo ;)

>>> import sqlite3
>>> db = sqlite3.connect(":memory:")
>>> cs = db.cursor()
>>> cs.execute('select instr("the quick brown fox", "brown")').fetchone()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: no such function: instr
>>> def instr(a, b):
...     return a.find(b) + 1 # add NULL-handling etc.
... 
>>> db.create_function("instr", 2, instr)
>>> cs.execute('select instr("the quick brown fox", "brown")').fetchone()[0]
11
>>> cs.execute('select instr("the quick brown fox", "red")').fetchone()[0]
0





More information about the Python-list mailing list