gpt4 book ai didi

ios - 如何在 iOS SQLite 中为搜索查询启用 soundex()?

转载 作者:搜寻专家 更新时间:2023-10-30 23:13:29 25 4
gpt4 key购买 nike

有没有办法在 iOS 应用程序中使用 SQLite soundex()?

请指导我找到一个方法...

尝试过 Homegrew,但它可以在终端上运行,我每次都需要在终端上运行 soundex。我也不知道如何移植到iOS APP。

终端工作

enter image description here

最佳答案

您可以使用 sqlite3_create_function 创建 soundex SQL 函数:

#import "ViewController.h"
#import <sqlite3.h>

void soundex(sqlite3_context *context, int argc, sqlite3_value **argv);

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self test];
}

- (sqlite3 *)openDatabase
{
NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *path = [docsPath stringByAppendingPathComponent:@"test.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path])
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"db"];
[fileManager copyItemAtPath:bundlePath toPath:path error:nil];
}

sqlite3 *db;
int rc;

if ((rc = sqlite3_open_v2([path UTF8String], &db, SQLITE_OPEN_READWRITE, NULL)) != SQLITE_OK)
{
NSLog(@"%s: sqlite3_open_v2 failed: %d", __FUNCTION__, rc);
}

return db;
}

- (BOOL)createFunction:(sqlite3 *)db
{
int rc;
if ((rc = sqlite3_create_function(db, "soundex", 1, SQLITE_ANY, NULL, soundex, NULL, NULL)) != SQLITE_OK)
{
NSLog(@"%s: sqlite3_create_function error: %s", __FUNCTION__, sqlite3_errmsg(db));
}

return rc;
}

- (void)test
{
int rc;
sqlite3 *db = [self openDatabase];

[self createFunction:db];

sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, "SELECT name FROM todo WHERE soundex(name) = soundex('Mani')", -1, &statement, NULL) != SQLITE_OK)
{
NSLog(@"%s: sqlite3_prepare_v2 error: %s", __FUNCTION__, sqlite3_errmsg(db));
}

while ((rc = sqlite3_step(statement)) == SQLITE_ROW)
{
const unsigned char *name = sqlite3_column_text(statement, 0);
if (name)
NSLog(@"name=%s", name);
else
NSLog(@"name=NULL");
}

if (rc != SQLITE_DONE)
{
NSLog(@"%s: sqlite3_step error: %s", __FUNCTION__, sqlite3_errmsg(db));
}

sqlite3_finalize(statement);
sqlite3_close(db);
}

@end

void soundex(sqlite3_context *context, int argc, sqlite3_value **argv)
{
const char *str = (const char*)sqlite3_value_text(argv[0]);
const char *in = str;

static int code[] =
{ 0,1,2,3,0,1,2,0,0,2,2,4,5,5,0,1,2,6,2,3,0,1,0,2,0,2 };
/* a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z */

char ch;
int last;
int count;

char key[5];
/* Set up default key, complete with trailing '0's */
strcpy(key, "Z000");

/* Advance to the first letter. If none present,
return default key */
while (*in != '\0' && !isalpha(*in))
++in;
if (*in == '\0') {
sqlite3_result_text(context, key, 4, SQLITE_TRANSIENT);
return;
}

/* Pull out the first letter, uppercase it, and
set up for main loop */
key[0] = toupper(*in);
last = code[key[0] - 'A'];
++in;

/* Scan rest of string, stop at end of string or
when the key is full */
for (count = 1; count < 4 && *in != '\0'; ++in) {
/* If non-alpha, ignore the character altogether */
if (isalpha(*in)) {
ch = tolower(*in);
/* Fold together adjacent letters sharing the same code */
if (last != code[ch - 'a']) {
last = code[ch - 'a'];
/* Ignore code==0 letters except as separators */
if (last != 0)
key[count++] = '0' + last;
}
}
}

sqlite3_result_text(context, key, 4, SQLITE_TRANSIENT);
}

关于ios - 如何在 iOS SQLite 中为搜索查询启用 soundex()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17238450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com