How to connect to the database in Python
In today's data-driven era, databases are important tools for storing and managing data. As a powerful programming language, Python provides a variety of ways to connect to databases. This article will introduce in detail how to connect to the database in Python, and attach examples of structured data.
1. Common ways to connect to the database in Python

Python can connect to different types of databases through a variety of libraries. The following are common database connection methods and their corresponding Python libraries:
| Database type | Python library | Features |
|---|---|---|
| MySQL | mysql-connector-python, pymysql | Support MySQL database, easy to use |
| PostgreSQL | psycopg2 | High performance, supporting advanced features of PostgreSQL |
| SQLite | sqlite3 | Lightweight, no server installation required |
| Oracle | cx_Oracle | Support Oracle database, powerful function |
| MongoDB | pymongo | Support NoSQL database, strong flexibility |
2. Example of Python connecting to MySQL database
The following is usedmysql-connector-pythonSample code to connect to MySQL database:
| step | code example |
|---|---|
| Installation library | pip install mysql-connector-python |
| Connect to database | import mysql.connector conn = mysql.connector.connect(host="localhost", user="root", password="password", database="test") |
| Create cursor | cursor = conn.cursor() |
| Execute SQL query | cursor.execute("SELECT * FROM users") |
| Get results | result = cursor.fetchall() |
| close connection | cursor.close() conn.close() |
3. Example of Python connecting to SQLite database
SQLite is a lightweight database that requires no server installation. The following is usedsqlite3Example of library connecting to SQLite database:
| step | code example |
|---|---|
| Import library | import sqlite3 |
| Connect to database | conn = sqlite3.connect("example.db") |
| Create cursor | cursor = conn.cursor() |
| Create table | cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)") |
| Insert data | cursor.execute("INSERT INTO users (name) VALUES ('Alice')") |
| Commit changes | conn.commit() |
| close connection | conn.close() |
4. Example of Python connecting to MongoDB database
MongoDB is a NoSQL database suitable for unstructured data storage. The following is usedpymongoExample of connecting to MongoDB:
| step | code example |
|---|---|
| Installation library | pip install pymongo |
| Connect to database | from pymongo import MongoClient client = MongoClient("mongodb://localhost:27017/") |
| Select database | db = client["testdb"] |
| select collection | collection = db["users"] |
| Insert data | collection.insert_one({"name": "Alice", "age": 25}) |
| Query data | result = collection.find_one({"name": "Alice"}) |
5. Summary
Python provides a wealth of libraries to connect to various types of databases. Whether it is a relational database such as MySQL, PostgreSQL, or a non-relational database such as MongoDB, it can be easily handled. Database operations can be completed efficiently by choosing the appropriate library and following the correct connection steps.
The above is a detailed introduction and sample code for connecting to the database in Python. I hope it will be helpful to you!
check the details
check the details