Joke Collection Website - Blessing messages - General interface method for Python to access different types of databases

General interface method for Python to access different types of databases

In daily data management, it is necessary to deal with data stored in different types of database systems. Navicat, DBeaver and other management tools are usually used to manage these data. When analyzing a large amount of data, it needs to be extracted into Python/R for processing. Let's discuss the general connection method of Python calling MySQL, MongoDB, InfluxDB and other types of databases. The implementation method is to encapsulate various database interface packages with Python.

Effect after implementation: 1. Safe. Interface information encapsulation is convenient for confidential management; 2. reuse. Disposable packaging, permanent reuse; 3. Get started quickly. It is convenient for students who are not familiar with python and data call and can only use simple sql, saving time and effort.

We take MySQL, MongoDB and Influx DB as examples to define interface methods, and then encapsulate them into 1 general methods.

mysql_get(sql,db):

mongo_get(sql,db):

intrusion _ get(SQL,db):

As you can see, the parameters of the above function call are sql and db. We add another parameter db_type, which will construct a general method to call the above database.

Similarly, other types of databases can be added to this general framework, including but not limited to all types of relational, key-value and time-series databases.