SQLite ordering column with accented characters - sqlite

I'm developping a website using SQLite databases with PHP. I'm running Windows (dev) and my production environment should be a *nix platform. Here is the schema of my table :
CREATE TABLE [animals](
[id] INTEGER NOT NULL UNIQUE,
[name] VARCHAR(50) NOT NULL
);
I want to sort the animals by name (contains accented characters). My SQL query is:
SELECT * FROM animals ORDER BY name DESC
and I get :
éléphant
tigre
renard
chien
instead of,
tigre
renard
éléphant
chien
I've searched on the web. I tried,
SELECT * FROM animals ORDER BY name COLLATE *binary|nocase|rtrim* DESC
but I have the same problem. I also tried,
SELECT * FROM animals ORDER BY name COLLATE *localized|unicode* DESC
But I get an error (either my SQLite client crashes, either PHP returns the following error: no such collation sequence: UNICODE).
It seems there's a solution for Android, but I'm running Windows and my production environment should be a *nix platform.
How can I get my animals sorted the right way?

By default, SQLite has only ASCII collations.
It would be possible to use the ICU extension to get Unicode support, but PHP did not enable this.
But you can create your own collation and implement the comparison in PHP.

Related

Oracle DB vs Mariadb

I have to find out in MariaDb how to implement some features used in Oracle . I have :
Load a file: in Oracle I use the external table. Is there a way (fast and efficient one ) to load a file into a table . Has MariaDb a plugin which allows to load well a specific format of files?
In my existing Oracle code I used to developp a java wrap functions which allow those feature (is there a way in MariaDb to do this?), specifically :
1- Searching a files in an OS directory and insert them in a table,
2- send an SNMP trap
3- Send a mail via SMTP
Is there an equivalent to an Oracle job in Mariadb?
Is there an equivalent to Oracle TDE (Transparent data encryption) ?
Is there an equivalent to the VPD (virtual private policy)?
What is the maximum length of a varchar column/variable ? (in Oracle we can use the CLOBs..)
Many Thanks and Best Regards
MariaDB (and MySQL) can do a LOAD DATA on a CSV file. It is probably the most efficient way to convert external data to a table. (There is also ENGINE=CSV, which requires no conversion, but is limited in that it has no indexes, etc.)
MariaDB cannot, for security reasons, issue any arbitrary system calls. No emails, no 'exec', etc.
No Job, TDE, VPD.
Network transmissions can (optionally) use SSL for encryption at that level.
There is a family of virtually identical datatypes for characters:
CHAR(n), VARCHAR(n) -- where n is up to 65535; n is the limit of _characters_, not _bytes_.
TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT -- of various limits; the last is limited to 4GB.
For non-character storage (eg, images), there is a similar set of datatypes
BINARY(n), VARBINARY(n)
TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB
The various sizes of TEXT and BLOB indicate whether that is a 1-, 2-, 3-, or 4-byte length field in the implementation.
NVARCHAR is a synonym for VARCHAR. Character sets are handled by declaring a column to be, for example, CHARACTER SET utf8 COLLATE utf8_unicode_ci. Such can be defaulted at the database (schema) level, defaulted at the table level, or specified differently for different columns (even in the same table).

Search using English characters to match foreign ones

net MVC application using EF6
and would like searching for "O" to return matches containing "Ø"
I am using the default collation in MSSQL Server at the moment.
Do I have to change the collation or add some code?
thanks
That's called normalization.
E.g. køpenhavn could be written as
køpenhavn
kopenhavn
koepenhavn
So from a plain-sql you could query
select ...
where col1='kopenhavn'
or col1=replace('kopenhavn','o','ø')
or col1=replace('kopenhavn','o','oe')
or
select ...
where col1='kopenhavn' COLLATE SQL_Latin1_General_CP1253_CI_AI
(returns only 2 of 3).
The Entity Framework does support a COLLATE clause, but LINQ to SQL does not.
changing the collation
ALTER TABLE Resource ALTER COLUMN FirstName
nvarchar(50)COLLATE Latin1_General_CI_AI;
worked

Cannot resolve the collation conflict?

I had this error and I don't know how to fix it
Cannot resolve the collation conflict between "Arabic_CI_AS" and
"SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
note: I already change the collation from the database option --> Collation
I change it from "Arabic_CI_AS" to "SQL_Latin1_General_CP1_CI_AS"
and I am still getting the same error !!
Any suggestion to solve this ?
The database collation applies only when you create NEW objects without specifying the collation.
When you change it from "Arabic_CI_AS" to "SQL_Latin1_General_CP1_CI_AS", all the textual columns in the database are still collated Arabic_CI_AS. You can check this using
select object_name(object_id), name, collation_name
from sys.columns
where collation_name like '%Arabic%'
A patch to this problem is to put COLLATE DATABASE_DEFAULT against the comparison, e.g.
SELECT *
FROM TBL1
INNER JOIN TBL2 on X = Y COLLATE DATABASE_DEFAULT
or
SELECT *
FROM TBL1
WHERE X = Y COLLATE DATABASE_DEFAULT
etc
There is a script on this site that attempts to change the collation across an entire database, but
I have not personally tried it
Make sure you have a good backup of your database before trying it
It doesn't look like it will handle complex databases with indexed views, foreign key/default constraints etc

Syntax for using collate nocase in a SQLite replace function

I have an existing database where they created theiw own unicode collation sequence. I'm trying to use the following code and get a "no such collation sequence" exception. Can anybdy hlep with the the syntax to use "collate nocase" with this code?
update Songs set
SongPath = replace (SongPath, 'Owner.Funkytown', 'Jim');
Dump database (via shell), edit output SQL (find and change column definitions, set COLLATION NOCASE). Recreate database.

How to write UTF8 text to MySQL from ASP.NET via ODBC?

I'm using MySQL 5 on shared hosting, connecting from ASP.NET 3.5 using the MySQL 5.1 ODBC driver. I'd like to store UTF8 strings. My tables used to be all in "latin1_swedish_ci", but I converted the the database, table, and column to UTF8 using:
ALTER DATABASE `my_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
ALTER TABLE `my_table` CHANGE `subject` `subjext` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL
But I still get this error when inserting non-ascii characters (like "遊ぶ")into my database using an ODBCConnection and ODBCCommand:
ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.51b-community-nt]Incorrect string value: '\xE3\x80\x80\xE6\x89\x8B...' for column 'subject' at row 1
Note that since I'm using the 5.1 driver, I can't use "SET NAMES utf8;" - it produces an error.
Any ideas what I'm missing?
A few of things to check:
Make sure your tables and text fields really accept utf8:
Use the MySQL Query Browser and try to edit some data manually.
If it stays OK once you saved your edits, then the fields and tables are correctly set.
Make sure that you are actually inserting utf8 compliant-characters and not characters based on another form of encoding such as GB2332 for Chinese.
If that's the case, you may need to convert the strings to utf8 before you can send them to the database.
You can have a look at using the Encoding class in .Net.
I've answered a post on something related a while ago and there is a CodeProject article on just this issue.
You probably also need to make sure that the ODBC connection string includes the following:
CharSet=utf8;
There is a list of all the parameters you can use for the ODBC conection.

Resources