Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Python language Application in MySQL Database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to solve the problem of Python language application in MySQL database. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Python language is used in a wide range of places, and there are many problems perplexing us in the long-term application.

After trying for a long time, whether you specify charset='utf8', or use set_character_set () when connect, or execute "SET NAMES UTF8", the trace to the character_set_name () method returns latin1.... Then follow the code, it seems to go to the mysql-api, anyway, the solution is very simple, is to use the following way to execute, instead of spelling sql statements.

Python language code

> sql = "INSERT INTO t_user (name,nickname) values (% s)" > param = ("Zhang San", "Zhang San's nickname") > cursor.execute (sql,param)

Use gettext to implement. In fact, it is a way of text replacement, which is similar to using ResourceBundle and properties in java.

1. Import gettext Modul

Python language code

Import gettext gettext.install ('i18n testworthy,'. / locale', unicode=True)

Parameter description:

Scope: used to define the principal name path of the translation file: the path where the translation file is located unicode: use unicode

two。 Replace all text in the code that needs to be internationalized with the form of _ ("text")

3. When internationalization processing is required, call

Python code gettext.translation ('i18n cn','. / locale', languages= ['cn']) .install (True)

Now that the program is written, you need to generate the required resource files:

1. Call the Tools/i18n/pygettext.py of the python installation directory to extract the template to be translated

> pygettext.py path/to/yourfile.py

A file named messages.pot will be generated

two。 After generating the template file, modify the template file, in which msgid is the key value, corresponding to the text written in your program, such as: _ ("New File"), and msgstr is the translated value. Also, pay attention to modifying the charset of Content-Type in the header of the file to the appropriate encoding, such as utf8.

3. After you write the template, change the extension to .po, run Tools/i18n/msgfmt.py, and generate the binary resource file.

> msgfmt.py messages.po

A file named messages.mo will be generated

4. Put the mo file in the right place. For example, this is what you wrote in the program:

Gettext.install ('i18n cn','. / locale', unicode=True) gettext.translation ('i18n testworthy,'. / locale', languages= ['cn']) .install (True)

Then your program directory needs to exist. / local/cn/LC_MESSAGES/i18ntest.mo so that when the program starts, it will read the resource file, replace the corresponding text, and achieve internationalization.

On how to solve the problem of Python language application in the MySQL database is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report