Published by Kenneth on 20 Jan 2010 at 11:44 pm
Add the MySQL instr() function to SQLite
Before you can add a new function to use in your SQL statements with SQLite, you must first create an SQLite extension library. This is relatively easy to do, and the SQLite web site provides a straightforward wiki article explaining what to do.
To actually add an instr() function to SQLite, use the following code segment:
1: void sqlite3_instr(sqlite3_context* pContext, int argc, sqlite3_value** argv)
2: {
3: const char *str1 = (const char *) sqlite3_value_text(argv[0]);
4: const char *str2 = (const char *) sqlite3_value_text(argv[1]);
5:
6: char *p = strstr(str1, str2);
7: int nResult = 0;
8:
9: if(p != NULL)
10: {
11: nResult = p - str1 + 1;
12: }
13:
14: sqlite3_result_int(pContext, nResult);
15: }
And that’s really all you need.