📜 ⬆️ ⬇️

How to make friends with SQLite android with a language other than English


Hello, dear reader!
In this article I want to touch on the problem of storing Cyrillic data in SQLite, try to deal with the Android NDK, and generally live a wonderful life! However, from this point on, I consider important the first two points. We will talk about them.

A well-known problem with SQLite is that it doesn’t like any characters other than Latin, so the following is true [1] :
SELECT "" LIKE "";
0
SELECT "s" LIKE "S";
1


Such a problem is relevant for our latitudes, and can be solved by user functions.
But Android does not support functions, so crutches are created.


Android NDK


To expand your horizons, dear reader, as well as create suitable crutches, I suggest using Android NDK [2] (download and unpack into an accessible folder)
')
A bit boring

Android NDK is a tool to compile C / C ++ code into native code of the Android platform. In addition, many Android libraries are provided, so that certain intensive program sections can be written in C / C ++ and then used in Java.
NDK is supported starting with Android 1.5, and the following ARMv5TE processor architectures (including Thumb-1 instructions), ARMv7-A (including Thumb-2 instructions and VFPv3-D16, with optional support for NEON / VFPv3-D32) are currently supported
Future releases promise x86 support

In short, let's use this tool!

So...


What do we do? We will combine the source code sqlite and icu.
In order not to burden the process of searching for the necessary libraries and source codes, an assembled set for compilation is proposed here [3] . I apologize if you accidentally included something extra.
If you are not interested in the compilation process, you can download the compiled library here [7] , and skip a few points.

Download the file, unpack, and place this precious in your project.
project / jni
Where the project here and hereafter is complete let it be before the project

Before going to the compilation step, I want to draw your attention to the structure of some files.

common / Android.mk

All source files connected to the future library, various flags and build options are described here.

common / android_sqlite ...

These files provide android-like access to the database, as well as an interface for interacting with the database from Java.
Note the function signatures.
 void Java_biz_sneg_sqlite_SQLiteDatabase_closedb() 

To successfully use native functions, you need to describe them in the format Java____
I also turned on the function of determining the geographic distance GEODISTANCE (x1, y1, x2, y2), because, for me personally, it was not enough.
Now it's easy to plug in your desired functions, be it sin, cos or your exotic

Compilation and connection of the library


First I will describe the steps for Windows, because the solution in the forehead does not work.
Do it once! Download and install Cygwin [6] , when installing, select Devel packages for installation. And ... temporarily retire to sleep or other pleasant things, because it is hellishly long. Although, maybe I just chose the wrong server.
Do two! We start the Cygwin console (just the console for other OS) and passing to the directory from the project:
Windows:
$ cd /cygdrive/project/jni
Non-windows:
$ cd project/jni

Do three! Run NDK
Windows:
$ /cygdrive/ndk-path/ndk-build
Non-windows:
$ ndk-path/ndk-build


Where project is the path to your project, ndk-path is the path where you unpacked the NDK
The compilation process should start:
...
Compile++ thumb : android_sqlite <= tmutfmt.cpp
Compile++ thumb : android_sqlite <= colldata.cpp
Compile++ thumb : android_sqlite <= bmsearch.cpp
Compile++ thumb : android_sqlite <= bms.cpp
Compile++ thumb : android_sqlite <= currpinf.cpp
Compile++ thumb : android_sqlite <= uspoof.cpp
Compile++ thumb : android_sqlite <= uspoof_impl.cpp
...

If not started, I feel sorry for O_o

At the output, we should get the compiled library, along the path project / libs / armeabi / libandroid_sqlite.so

Now we need to connect the library to the project.
First, in the main Activity we must describe its connection.
 public class MainActivity extends TabActivity { static { System.loadLibrary("android_sqlite"); } ... } 


Further, we need code that would work with the native library, and it can be downloaded here [4]

How to work?


How, how, they sat down and went!
Open the base
 try { dataBase_ = new biz.sneg.sqlite.SQLiteDatabase(DB_NAME); } catch (Exception e) { Log.wtf("Database", e); } 


We get data somehow or as you like
 try { biz.sneg.sqlite.SQLiteCursor cursor = dataBase_.query("SELECT * FROM tablename WHERE rus_text_field LIKE ?", new Object[]{"%"}); List<Map<String, String>> results = new ArrayList<Map<String,String>>(); while (cursor.next()) { final int columnCount = cursor.count(); HashMap<String, String> currentMap; currentMap = new HashMap<String, String>(); for (int i = 0; i < columnCount; i++) { currentMap.put(cursor.columnName(i), cursor.stringValue(i)); } results.add(currentMap); } cursor.dispose(); } catch (Exception e) { Log.wtf("Database", e); } 


In general, I wish good luck!

Nikolai Kudashov, the author of the idea and assembly, an invite can be sent here drklo.2kb <known_symbol> gmail.com

References:
1. SQLite and full UNICODE
2. Android NDK
3. Sources for the library
4. Sources for java
5. SQLite
6. Cygwin
7. Compiled library

Source: https://habr.com/ru/post/122408/


All Articles