Python 连接数据库的多种方法(Python连接数据库的实用方法大全)
原创
一、引言
在Python中,连接数据库是常见的需求,无论是进行数据查询、数据更新还是数据迁移,都需要与数据库进行交互。本文将介绍Python连接数据库的多种实用方法,包括常用的数据库类型和相应的连接库。
二、Python连接MySQL数据库
MySQL是最流行的关系型数据库管理系统之一,以下是几种连接MySQL数据库的方法。
2.1 使用MySQLdb模块
MySQLdb是一个Python库,用于连接MySQL数据库。
import MySQLdb
# 连接数据库
conn = MySQLdb.connect(host='localhost', user='root', passwd='password', db='database_name', charset='utf8')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
2.2 使用PyMySQL模块
PyMySQL是一个纯Python写的库,用于连接MySQL数据库。
import pymysql
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', passwd='password', db='database_name', charset='utf8')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
三、Python连接SQLite数据库
SQLite是一种轻量级的数据库,适合于嵌入式设备和移动应用。
3.1 使用sqlite3模块
Python标准库中的sqlite3模块可以用来连接SQLite数据库。
import sqlite3
# 连接数据库
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
四、Python连接PostgreSQL数据库
PostgreSQL是一个功能强盛的开源对象关系型数据库系统。
4.1 使用psycopg2模块
psycopg2是Python中连接PostgreSQL数据库的常用库。
import psycopg2
# 连接数据库
conn = psycopg2.connect(
dbname="database_name",
user="user",
password="password",
host="localhost"
)
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
五、Python连接Oracle数据库
Oracle是一个强盛的商业数据库系统,广泛用于企业级应用。
5.1 使用cx_Oracle模块
cx_Oracle是Python连接Oracle数据库的常用库。
import cx_Oracle
# 连接数据库
conn = cx_Oracle.connect(user='user', password='password', dsn='localhost/orclpdb1')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
六、Python连接SQL Server数据库
SQL Server是微软开发的关系型数据库管理系统。
6.1 使用pyodbc模块
pyodbc是一个用于连接ODBC数据库的Python库,可以用来连接SQL Server。
import pyodbc
# 连接数据库
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=database_name;UID=user;PWD=password')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
6.2 使用pymssql模块
pymssql是专门用于连接SQL Server的Python库。
import pymssql
# 连接数据库
conn = pymssql.connect(server='localhost', user='user', password='password', database='database_name')
cursor = conn.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询导致
results = cursor.fetchall()
for row in results:
print(row)
# 关闭连接
cursor.close()
conn.close()
七、Python连接NoSQL数据库
NoSQL数据库是一种非关系型数据库,适用于大数据和高性能应用。
7.1 使用pymongo连接MongoDB
pymongo是Python连接MongoDB的库。
from pymongo import MongoClient
# 连接数据库
client = MongoClient('localhost', 27017)
db = client['database_name']
collection = db['collection_name']
# 查询数据
results = collection.find()
for document in results:
print(document)
# 关闭连接
client.close()
7.2 使用redis-py连接Redis
redis-py是Python连接Redis数据库的库。
import redis
# 连接数据库
client = redis.Redis(host='localhost', port=6379, db=0)
# 设置和获取key-value
client.set('key', 'value')
print(client.get('key'))
# 关闭连接
client.close()
八、总结
本文介绍了Python连接多种数据库的方法,包括关系型数据库(MySQL、SQLite、PostgreSQL、Oracle、SQL Server)和非关系型数据库(MongoDB、Redis)。不同的数据库系统有不同的连接库,但基本的连接和操作流程相似,都是确立连接、执行SQL(或相应的查询语句)、获取导致和关闭连接。掌握这些方法可以帮助开发者在不同的应用场景中高效地处理数据。