Hi,
I have create a very simple database to test inserting unicode characters:
- CREATE TABLE `tencodingtest` (
- `pk` int(11) NOT NULL AUTO_INCREMENT,
- `value_as_utf8` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
- `value_as_utf16` varchar(30) CHARACTER SET utf16 COLLATE utf16_unicode_ci DEFAULT NULL,
- `normalvarchar` varchar(30) DEFAULT NULL,
- PRIMARY KEY (`pk`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1
In my test, I want to try to insert this character into the field 'value_as_utf8': Unicode Character “𠀋” (U+2000B).
This character in UTF-8 is: 0xF0 0xA0 0x80 0x8B
This character in UTF-16 is: 0xD840 0xDC0B
I have made several attempts to insert this value with this function, but all attempts fail:
- procedure TfmDatabase.Button5Click(Sender: TObject);
- var
- s: String;
- s8: UTF8String;
- begin
- MyConnection1.Connected := False;
- MyConnection1.Options.UseUnicode := True; //IMPORTANT!!
- MyConnection1.Connect;
- MyQuery2.SQL.Text := 'select * from tencodingtest';
- MyQuery2.Execute;
- s := '𠀋';
- s8 := '𠀋';
- //---[ATTEMPT 1]
- try
- MyQuery2.Append;
- MyQuery2.FieldByName('value_as_utf16').AsString := s;
- MyQuery2.Post;
- except on e: Exception do
- begin
- MyQuery2.Cancel;
- mmLog.Lines.Add('[ATTEMPT 1]: Exception: ' + e.Message);
- end;
- end;
- //---[ATTEMPT 2]
- try
- MyQuery2.Append;
- MyQuery2.FieldByName('value_as_utf8').AsString := s;
- MyQuery2.Post;
- except on e: Exception do
- begin
- MyQuery2.Cancel;
- mmLog.Lines.Add('[ATTEMPT 2]: Exception: ' + e.Message);
- end;
- end;
- //---[ATTEMPT 3]
- try
- MyQuery2.Append;
- MyQuery2.FieldByName('value_as_utf8').AsString := s8;
- MyQuery2.Post;
- except on e: Exception do
- begin
- MyQuery2.Cancel;
- mmLog.Lines.Add('[ATTEMPT 3]: Exception: ' + e.Message);
- end;
- end;
- //---[ATTEMPT 4]
- try
- MyQuery2.Append;
- MyQuery2.FieldByName('value_as_utf8').AsWideString := s8;
- MyQuery2.Post;
- except on e: Exception do
- begin
- MyQuery2.Cancel;
- mmLog.Lines.Add('[ATTEMPT 4]: Exception: ' + e.Message);
- end;
- end;
- //---[ATTEMPT 5]
- try
- MyQuery2.Append;
- MyQuery2.FieldByName('value_as_utf8').AsWideString := s;
- MyQuery2.Post;
- except on e: Exception do
- begin
- MyQuery2.Cancel;
- mmLog.Lines.Add('[ATTEMPT 5]: Exception: ' + e.Message);
- end;
- end;
- end;
All 5 attempts fail with these results:
[ATTEMPT 1]: Exception: #HY000Incorrect string value: '\xF0\xA0\x80\x8B' for column 'value_as_utf16' at row 1
[ATTEMPT 2]: Exception: #HY000Incorrect string value: '\xF0\xA0\x80\x8B' for column 'value_as_utf8' at row 1
[ATTEMPT 3]: Exception: #HY000Incorrect string value: '\xF0\xA0\x80\x8B' for column 'value_as_utf8' at row 1
[ATTEMPT 4]: Exception: #HY000Incorrect string value: '\xF0\xA0\x80\x8B' for column 'value_as_utf8' at row 1
[ATTEMPT 5]: Exception: #HY000Incorrect string value: '\xF0\xA0\x80\x8B' for column 'value_as_utf8' at row 1
I tried inserting this character just with a 3rd party editor (HeidiSql), and it can be inserted without problems in both fields (value_as_utf8 and value_as_utf16) (see attachment please).
Am I doing something wrong why this is not working from Delphi?
Mydac Version = 9.1.3 for RAD Studio 10.2.