Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: "Can't use index" with DBD::SQLite

by choroba (Bishop)
on Jan 29, 2013 at 21:12 UTC ( #1015933=note: print w/replies, xml ) Need Help??

in reply to "Cannot use index" with DBD::SQLite

How did you create the table and the index? Does the index really index the "name" column? This works for me:
#!/usr/bin/perl use warnings; use strict; use DBI; unlink 'test.db' or warn $!; my $dbh = DBI->connect('dbi:SQLite:test.db', q(), q()); $dbh->do(q(create table profile (profile_id integer, name varchar))); $dbh->do(q(create index idx_name on profile (name, profile_id))); my $insert = $dbh->prepare(q(insert into profile values(?, ?))); $insert->execute(@$_) for [10, 'Joseph'], [11, 'William'], [12, 'Elisabeth']; my $sql = q{ SELECT profile_id FROM profile INDEXED BY idx_name WHERE name == ? }; my $sth = $dbh->prepare($sql); my $name = 'William'; $sth->execute($name); my $arr = $sth->fetchall_arrayref; print "@$_\n" for @$arr;
If I switch name and profile_id in the index creation, I get the error you mentioned, but only if no other index has been yet defined. I am not sure this is the intended behaviour.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: "Can't use index" with DBD::SQLite
by LonelyPilgrim (Beadle) on Jan 29, 2013 at 22:50 UTC

    Thanks. I ran your code, and it worked for me, and then re-created my index per your specifications, with the one tweak I made before. It still didn't work on my table. So I figured that tweak must be the problem.

    For some reason I don't understand, it didn't like my COLLATE NOCASE DESC specification. When I re-created the index once again without that, it started working. Huh.

      When you give the COLLATE NOCASE DESC option in the index it means convert all the text to lower case and store them in descending order for case insensitive search. So, to make use of that index, you have to give the same collation again in your query, and the following should work.

      SELECT profile_id FROM profile INDEXED BY idx_name WHERE name == ? COL +LATE NOCASE

      If you recreated the index without this collation, then it means you cannot do case insensitive search, or have to convert things to upper or lower case every time like below.

      SELECT profile_id FROM profile INDEXED BY idx_name WHERE lower(name) = += lower(?)

      Although you have found a workaround, this may be useful for someone else, facing a similar problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015933]
[usemodperl]: sorry veltro, venting...
[usemodperl]: (is that wrong marto?)
[aitap]: usemodperl: maybe it's you who has changed
[marto]: demonstrably yes, since you claim to want a safe space, your definition for which seems to be a place where you can name call, make things up or otherwise post without being challenged
[usemodperl]: no that's not it at all, meant safe for perl people, to not be hounded by weirdos obsessed with things like... whitespace :-)
[choroba]: I'm definitely not obsessed with whitespace, I rate myself among Perl people, and I haven't noticed any hounding
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-24 15:58 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.