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 create a request connection in the Python database connection pool

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

Share

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

In the Python database connection pool how to create a request connection, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

If you set up connection pooling during the operation of Python database connection pooling, you can do so as follows.

Request connection:

Db = pool.connection ()

You can use these connections just like the original DB-API 2. In fact, a tough connection is used in the ``SteadyDB`` version. Note that connections can be shared with other threads, as long as you set the maxshared parameter to non-zero and the DB-API 2 module allows it. If you want to use a dedicated connection, use:

Db = pool.connection (0)

If you no longer need this connection, you can return it to the connection pool and use db.close (). You can also use the same method to get another connection. Warning: in a multithreaded environment, do not use the following methods:

Pool.connection (). Cursor (). Execute (...) Db = pool.connection () cur = db.cursor () cur.execute (...) Res = cur.fetchone () cur.close () # or del cur db.close () # or del db

Example [convenient for you to use directly in the future]

Use the PersistentDB module

Import threading,time,datetime import MySQLdb import DBUtils.PersistentDB persist = DBUtils.PersistentDB.PersistentDB (MySQLdb,100,host='localhost',user='root',passwd='321',db='test',charset='utf8') conn = persist.connection () cursor = conn.cursor () cursor.execute ("insert into me values (1meme 22222')") conn.commit () conn.close ()

Through the above, you can get the database connection!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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