In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what SQL database connectors in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. PyMySQL (https://pypi.org/project/PyMySQL/)
As you know, MySQL is an industry-leading, multi-user, multi-threaded open source database management system. It is very popular in the development of Web projects.
Installation and use:
We can install PyMySQL on our PC by running the following command:
Bash pip install pymysql
After completing the installation, we can test the database connector by running the following Python code:
Import pymysql con = pymysql.connect ('localhost',' username', 'password',' db_name'') with con. Cursor () as cur: cur.execute ('SELECT VERSION ()') version = cur.fetchone () print (f'Database version: {version [0]}') con.close ()
Advantages:
Most of its public API is compatible with mysqlclient and MySQLdb.
Both Python 2 and 3 are supported.
Able to support MySQL and MariaDB servers.
Disadvantages:
Low-level API provided by MySQL is not supported, including data_seek, store_result, use_result, etc.
2. MySQLdb (https://mysqlclient.readthedocs.io/user_guide.html)
As a thread-compatible-compatible interface on the MySQL database server, MySQLdb provides API for Python databases.
Installation and use:
You can install the MySQLdb module on your own PC by running the following command:
Bash # For Ubuntu, use the following command-sudo apt-get install python-pip python-dev libmysqlclient-dev # For Fedora, use the following command-sudo dnf installPythonpython-devel mysql-devel redhat-rpm-config gcc # ForPythoncommand prompt, use the following command-pip install MySQL-python
You can also use the connector by running the following Python code:
From MySQLdb import _ mysql db=_mysql.connect () db=_mysql.connect (host= "localhost", user= "username", passwd= "password", db= "db_name")
Advantages:
Because it is built in C language, it runs very fast.
Belongs to pure SQL.
Able to support MySQL.
Disadvantages:
Python 3 is not supported.
You need to write your own SQL code.
Not only do you need to manage your own pointers, but you can't do any caching, or parameterization.
If you do not rewrite all the database code, you cannot switch to a different database backend.
3. QTSQL (https://doc.qt.io/qt-5/qtsql-index.html)
As another database connector, QTSQL can be used to integrate databases with various PYQT5 applications. It is worth noting that because QT is a GUI toolkit, QTSQL is mainly used for UI-like applications.
Installation and use:
Since PYQT5 is pre-installed in QTSQL, you can import the corresponding module with the following Python code.
From PyQt5 import QtSql
And connect to the database through the following code snippet:
Self.QSqlDatabase.addDatabase ("QMYSQL") self.db.setHostName ("host_name") self.db.setDatabaseName ("database_name") self.db.setUserName ("username") self.db.setPassword ("password")
In the above code, the first parameter of QSqlDatabase.addDatabase can be used to add drivers, including: QPSQL, QMYSQL, QOCI, QODBC, and QSQLITE. The next four lines of commands, setHostName (), setDatabaseName (), setUserName (), and setPassword (), can be used to initialize the database connection. In addition, after initialization, you can open and access the database by calling QSqlDatabase.open ().
Advantages:
Only various Qt libraries are available.
Because it can be integrated with various standard widgets of Qt, it can return Qt objects.
You can use any database backend that supports Qt (such as MySQL, SQLite, etc.).
Disadvantages:
You need to write your own SQL.
4. Psycopg2 (https://pypi.org/project/psycopg2/)
Psycopg is the most popular PostgreSQL database adapter in the Python programming language. Its main feature is the complete implementation of the PythonDB API 2.0 specification and thread safety (that is, multiple threads can share the same connection). Because it is designed for applications with a large number of multithreaded applications, such applications not only create and destroy a large number of pointers, but also generate a large number of concurrent INSERT or UPDATE.
Installation and Guide:
You can install psycopg2 on your own PC by running the following command:
Bash pip install psycopg2
After completing the installation, run the following Python code:
Import psycopg2
Try:
Conn = psycopg2.connect ("dbname='template1' user='dbuser' host='localhost' password='dbpass'")
Except:
Print "I am unable to connect to the database"
Cur = conn.cursor ()
Cur.execute ("SELECT datname from pg_database")
Advantages:
Fast and efficient.
Supports multiple connections, as well as a variety of connection objects.
Supports a variety of asynchronous queries.
Disadvantages:
Lack of appropriate documentation.
5. SuperSQLite (https://github.com/plasticityai/supersqlite)
As a super SQLite library and driver for Python, SuperSQLite replaces the built-in SQLite package with a new pre-compiled version native to each platform, SQLite, and its SQLite extension.
Installation and Guide:
You can install SuperSQLite on your own PC by running the following command:
Bash pip install supersqlite
After the installation is complete, run the following Python code to use:
From supersqlite import sqlite3 conn = sqlite3.connect ('databasefile.db')
Advantages:
Fast and efficient.
Remote streaming can be realized through HTTP protocol.
Full-text retrieval can be realized.
No obvious shortcomings have been found so far.
These are all the contents of the article "what are the SQL database connectors in Python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.