Sqlite android query() / like with accents and without

Asked

Viewed 1,038 times

0

How to make a query in Sqlite for Android that searches both accented and not accented words?

For example:

String[]ARGUMENTOS = {"%"+argumento+"%"}

query(TABELA_CIDADES, COLUNA_NOME, NOME + " like ?",ARGUMENTOS,null,null,null,null);

Where the argument for the search would be Sao Paulo, without accent, but also seek São Paulo, and vice versa.

I saw in some questions and on the Internet, that many people recommend to create a normalized column with names without accent and perform the search in both, but I was wondering if there is any other option.

  • Did the answer solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.

1 answer

2

The solution presented even works, but is absurd.

Sqlite works well by default with ASCII and lets the user cope with other options. If you are going to use accent need other text encoding.

One hypothesis is to use the UTF-8 that people consider universal (there are controversies), but it has a problem, the code needed to handle correct UTF-8 is absurdly larger than Sqlite itself.

An alternative is to use UTF-16 (actually UCS-2) that will make the data size large. To implement a leotard suitable for this encoding will not be so simple either. There will be some performance gain, but I don’t know if it pays, most of the time it almost pays to use the solution of the question.

Sqlite says it supports both, of course it supports anything, but that doesn’t mean it has the leotard appropriate by default. Has an extension (ICU Extension), or you can use what the language offers, but rarely is this a viable option.

Another possibility is to use a Latin1 encoding (ISO 8859-1). It is much simpler, as fast as it can be where there are accents and does not take up extra space, but needs to turn with the extension, never seen a ready.

If you want to do something simpler, there’s a question here that I did and Bacco’s response solves the problem in 99% of the cases, for some people only those cases count.

Having said all this :P, the implementation of Sqlite in Android has the ability to do this, to work should use the option of COLLATE LOCALIZED.

  • Bigown, very complete your answer, but I am beginner on android and I think I did not understand completely. Are you saying that I should create a new collation, as answered in the question you performed, or should I simply use COLLATE LOCALIZED? If it is only COLLATE LOCALIZED I must pass it as the orderby parameter of the query() method or add something to the database table?

  • You can just use the COLLATE LOCALIZED, only if it does not work as you wish (do not give up on the first problem) should try the other possibilities. I think it is ideal to create tables already with COLLATE, but can use in cheese if the table was not created with this possibility.

  • I am testing here directly in the query, but I had no success, I will test using directly in the table.

  • Using the COLLATE LOCALIZED directly in the table worked, but only in query’s where I use =, in which I search using like ? I have not yet succeeded.

  • Yes, there may be some particularity of the implementation that I don’t know, but it makes sense to have to go through the table. I thought it might not work with the LIKE, These solutions are not usually complete. I can not guarantee that there is no other way to make it work this way because I do not use Sqlite on Android, but maybe you have to apply one of the other solutions, which are not so simple to apply, will have to study the Sqlite API itself. I don’t even know how to apply this on Android. So I passed some information on how to do "at hand".

  • I’ll keep trying here, as soon as I have a solution put here.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.