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

Can python2.7 connect two databases at the same time?

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

Share

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

This article mainly explains "whether python2.7 can connect two databases at the same time". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "can python2.7 connect two databases at the same time"?

I. Preface

This question is that when I was developing, I encountered the need to connect two databases in a script. I was not very familiar with python2.7 at that time, so I wanted to search the Internet whether python2.7 could connect to two databases at the same time, but there was not a clear answer. In addition, the script kept reporting errors at that time, so I doubted that it was unreasonable to connect two databases at the same time.

It's just that python as a mature language, connecting multiple databases at the same time is an easy problem. The first part of this article is to answer that python can connect to multiple databases, and the second is to record the problems in operating the database.

2. Bug encountered in dual database operations

1. General steps

/ / Connect db1 = MySQLdb.connect (host=dbhost, user=dbuser, passwd=dbpass, db=dbname, port=dbport) db2 = MySQLdb.connect (host=slavehost, user=slaveuser, passwd=slavepass, db=slavedb, port=dbport) / / get the cursor object cursor = db1.cursor () cursor_build = db2.cursor () / / operate the database cursor.execute (sql1) cursor_build.execute (sql2) / / result db1 successfully, but the db2 operation is unresponsive

As shown in the steps, during the operation, I found that the db1 could be executed normally, but the db2 was not successful, and the script did not report an error at this time.

2. Print relevant information

(1) print the information of db2

Mysql instance:

Sql printing: execute sql without error report directly in the database

(2) print cursor objects

You can see that both databases have cursor objects, and there is no obvious difference, so they can operate the database normally.

(3) print the return value

Result = db2.execute (insert_build_sql) # insert data print result

The return result is 1, which means that the number of affected rows is 1, but the database is not actually inserted. The key is that the db1 is plugged in successfully, which is simply speechless.

III. Where the problem lies

While I was frantically searching for nothing, a py boss happened to be sending a message on the group. I decisively seized the opportunity to ask the blogger and finally solved the problem.

Consult the py bosses, and a big guy asks if autocommit is turned on, which is definitely not available. Originally db1 can be inserted successfully, I take it for granted that db2 can also be inserted successfully, there is no need for manual commit and so on. However, I still underestimated python, and then Baidu inquired to know that MySQLdb turned off automatic submission after connection.

After connecting to the db, add the autocommit attribute and set the auto-commit

Db1.autocommit (1) db2.autocommit (1)

Continue to test and find perfect writing. However, through this matter, we can also find some features of mysqldb operating mysql. Generally speaking, when only one library is connected, it is possible to add, delete, modify and check normally. But when connecting multiple libraries, if autocommit is not explicitly set, mysqldb will automatically add a set autocommit = 0 at the end of the statement execution. It is well known that the operations of the innodb engine are transactional, and it is naturally impossible to successfully execute sql by turning off autocommit.

At this point, I believe that everyone on the "python2.7 can connect to two databases at the same time" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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