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

Actual operation of python3 connection mysql

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

The following brings you about the actual operation of python3 connection mysql, if you are interested, let's take a look at the actual operation of python3 connection mysql. I believe it will be of some help to you after reading it.

Python3 connects to the mysql database and performs the operation.

Environment:

Os: windows 2008

Python: python 3.5.3

I used python3 to connect to the sqlite3 database before, but it was only used as a stand-alone database, but later when providing web services, the sqlite3 database did not support well enough and instead used the mysql database.

Python3 connects to the database using the pymysql module.

To sum up, writing classes is easy to use:

Class ConMysql: def _ _ init__ (self, host, username, password, database): self._database = database self._host = host self._user = username self._passwd = password def connect (self): "" connect to the database and execute the SQL statement Return tuple "" # connect to the database try: self._db = pymysql.connect (self._host, self._user, self._passwd, self._database) except (ConnectionRefusedError, pymysql.err.OperationalError, pymysql.err.InternalError) as _ con_err: return False _ con_err else: return True, 'OK' def get_data (self, _ sql_str) Query _ cur = self._db.cursor () try: _ cur.execute (_ sql_str) except (pymysql.err.InternalError, pymysql.err.OperationalError) Pymysql.err.ProgrammingError) as _ sql_err: _ cur.close () return False, _ sql_err if s = ='r'_ cur.close () _ array = _ cur.fetchall () return True _ array else: _ cur.close () self._db.commit () return True, 'OK' def edit_data (self, _ sql_str): # modify return self.get_data (_ sql_str) 'w') def _ _ del__ (self): self._db.close ()

There is no problem reading the database here, and errors will be reported when inserting or modifying. Because of coding problems, pymysql will compile all sql strings into byte by default. In order to avoid errors, you can add two parameters, use_unicode=True and charset='utf8'. In fact, there are many optional parameters when instantiating. For details, you can check the documentation or the source code connection you have installed.

Read the above details about the actual operation of python3 connection mysql, whether there is anything to gain. If you want to know more about it, you can continue to follow our industry information section.

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

Database

Wechat

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

12
Report