Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Who converts data when writing to a database with an encoding different from utf8?
#1
Python 3.7.2

I write the strings from my Python code into my database. My strings contain Latin and Cyrillic characters, so in the database I use 1-byte encoding koi8-r. The miracle is that my strings without distortion are written to the database, although utf8 and koi8r have completely different sequence of characters (for example, as in ascii and utf8). Sometimes characters of other layouts appear in the text and then write errors appear.

Therefore, the questions appear:
1. Who converts strings: the database or the aiomysql library, that I use to write to the database.
2. How quickly in Python / MariaDB to remove non-koi8-r characters to avoid errors.

Thank you in advance for participating in the conversation.

Perhaps, there are databases that support an "economical" (for my case) multibyte encoding, which stores the Latin and Cyrillic characters in the first byte, and other layouts in other bytes?
Quote
#2
If I understood the code right, there happens an implicit encoding to utf8mb4, if no other default encoding has been set.
If I'm right, this means the encoding takes 4 byte for each character. This can be something for internal optimization of MySQL itself.

Where the encoding happens: https://github.com/aio-libs/aiomysql/blo...on.py#L426
DEFAULT_CHARSET from external dependency: https://github.com/PyMySQL/PyMySQL/blob/...ons.py#L91

If you have a str, it's already encoded internally with utf8.
If your input was made with a koi8-r encoding, somewhere must happen a conversion.
For example if you enter form data on a web page, you should receive the parameters as raw bytes.
Then they need to be decoded, to be represented as str.

If the query is a str, then it is automatically encoded to utf8mb4, if no other encoding has been set somewhere.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Writing Data to CSV abhaydd 2 162 Aug-09-2019, 01:08 AM
Last Post: DeaD_EyE
  Reading and writing files JakeHoward4 1 124 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Problem writing a variable value to Excel Chuck_Norwich 1 85 Jul-25-2019, 02:20 PM
Last Post: Chuck_Norwich
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 88 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383
  Problem with reading and writing to file. darktitan 2 144 Jul-20-2019, 06:06 PM
Last Post: darktitan
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 136 Jul-20-2019, 06:59 AM
Last Post: scidam
  Database search Columbo 2 107 Jul-15-2019, 03:54 PM
Last Post: Columbo
  Writing the Path to a file properly dcw9996 2 188 Jul-01-2019, 11:26 PM
Last Post: dcw9996
  Custom encoding problems. SheeppOSU 0 129 Jun-23-2019, 11:19 PM
Last Post: SheeppOSU
  How to import data from database to excel nick123 0 112 Jun-14-2019, 04:13 PM
Last Post: nick123

Forum Jump:


Users browsing this thread: 1 Guest(s)