Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何在sqlite数据库中高效地搜索整个单词_Iphone_Sql_Sqlite - Fatal编程技术网

Iphone 如何在sqlite数据库中高效地搜索整个单词

Iphone 如何在sqlite数据库中高效地搜索整个单词,iphone,sql,sqlite,Iphone,Sql,Sqlite,我有一个有标题列的表。我想搜索整个单词,比如foo。所以,和hi-foo-bye-o-foo配对,而不是foobar或hellofoo。有没有一种不改变表结构的方法可以做到这一点?我目前使用了3个类似的查询,但速度太慢,我已经从标题为“%foo”或标题为“foo%”或标题为“foo”或标题为“%foo%”的文章中选择了*; 必须有更好的方法才能做到这一点?自然回答: 使用regexp运算符而不是like运算符 编辑我刚刚意识到regexp并不总是包含在SQLite中。你可能需要编译你自己的。。。

我有一个有标题列的表。我想搜索整个单词,比如foo。所以,和hi-foo-bye-o-foo配对,而不是foobar或hellofoo。有没有一种不改变表结构的方法可以做到这一点?我目前使用了3个类似的查询,但速度太慢,我已经从标题为“%foo”或标题为“foo%”或标题为“foo”或标题为“%foo%”的文章中选择了*; 必须有更好的方法才能做到这一点?

自然回答:

使用regexp运算符而不是like运算符

编辑我刚刚意识到regexp并不总是包含在SQLite中。你可能需要编译你自己的。。。换句话说,默认情况下它不在那里

编辑2

下面是一个正在运行的Perl示例

#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use DBI;

# connect to the DB
my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","","");

# create ugly, pureperl function 'regexp'
# stolen from http://d.hatena.ne.jp/tokuhirom/20090416/1239849298
$dbh->func(   "regexp"
            , 2
            , sub { my ( $pattern, $target ) = @_;
                         utf8::decode($pattern);
                         utf8::decode($target);
                         $target =~ m{$pattern} ? 1 : 0;
              }
            , "create_function" );
# drop table, if it exists
$dbh->do('drop table if exists foobar');
$dbh->do('create table foobar (foo varchar not null)');
my $sth=$dbh->prepare('insert into foobar (foo) values (?)');
while (<DATA>) { chop;$sth->execute($_); }
#query using regexp
my $a= $dbh->selectall_arrayref( 'select foo '
                                .'from foobar '
                                .'where foo regexp "^foo$|^foo\s+.*|.*\W+foo\W+.*|.*\W+foo$"'
                               );
print join("\n", map {$_->[0];} @{$a})
__DATA__
foo
foo
barfoo
foobarfolo
sdasdssds bar dasdsdsad
dasdsdasdsadsads foo! dasdasdasdsa
自然回答:

使用regexp运算符而不是like运算符

编辑我刚刚意识到regexp并不总是包含在SQLite中。你可能需要编译你自己的。。。换句话说,默认情况下它不在那里

编辑2

下面是一个正在运行的Perl示例

#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use DBI;

# connect to the DB
my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","","");

# create ugly, pureperl function 'regexp'
# stolen from http://d.hatena.ne.jp/tokuhirom/20090416/1239849298
$dbh->func(   "regexp"
            , 2
            , sub { my ( $pattern, $target ) = @_;
                         utf8::decode($pattern);
                         utf8::decode($target);
                         $target =~ m{$pattern} ? 1 : 0;
              }
            , "create_function" );
# drop table, if it exists
$dbh->do('drop table if exists foobar');
$dbh->do('create table foobar (foo varchar not null)');
my $sth=$dbh->prepare('insert into foobar (foo) values (?)');
while (<DATA>) { chop;$sth->execute($_); }
#query using regexp
my $a= $dbh->selectall_arrayref( 'select foo '
                                .'from foobar '
                                .'where foo regexp "^foo$|^foo\s+.*|.*\W+foo\W+.*|.*\W+foo$"'
                               );
print join("\n", map {$_->[0];} @{$a})
__DATA__
foo
foo
barfoo
foobarfolo
sdasdssds bar dasdsdsad
dasdsdasdsadsads foo! dasdasdasdsa

您可能对lucene、ferret或sphinx等搜索索引器感兴趣。这些将作为单独的进程运行,将索引您的数据,以便在可以配置词干等的地方进行快速搜索


或者,根据您的数据,您可以只返回任何上下文中包含foo的所有结果,然后在数据库之外使用正则表达式或类似的方法对其进行过滤。这可能是一种改进,具体取决于数据的特征。

您可能对lucene、ferret或sphinx等搜索索引器感兴趣。这些将作为单独的进程运行,将索引您的数据,以便在可以配置词干等的地方进行快速搜索


或者,根据您的数据,您可以只返回任何上下文中包含foo的所有结果,然后在数据库之外使用正则表达式或类似的方法对其进行过滤。这可能是一种改进,具体取决于数据的特征。

有各种各样的regexp库,您可以通过在构建中链接到它们来将它们包含在iPhone应用程序中


有关更多信息,请参阅。

有各种regexp库,您可以通过在构建中链接到它们来将它们包含在iPhone应用程序中


有关更多信息,请参阅。

对不起,我忘了提到我在iphone上,我可以将这些正则表达式添加到iphone?我想你可以。但是不使用Perl。必须有一种方法可以使用objectve c添加函数regexp。对不起,我忘了提到我在iphone上,我可以将这些正则表达式添加到iphone?我想你可以。但是不使用Perl。必须有一种方法可以使用objectve c添加函数regexp。