不同数据库的通用数据访问类
创始人
2025-01-09 18:00:49
0

解决这个问题的方法是创建一个通用的数据访问类,它可以适用于不同的数据库。下面是一个示例代码,展示了如何实现这个通用的数据访问类:

import pymysql
import psycopg2
import pyodbc

class Database:
    def __init__(self, db_type, host, username, password, database):
        self.db_type = db_type
        self.host = host
        self.username = username
        self.password = password
        self.database = database
        self.connection = None
        self.cursor = None

    def connect(self):
        if self.db_type == 'mysql':
            self.connection = pymysql.connect(host=self.host,
                                              user=self.username,
                                              password=self.password,
                                              database=self.database)
            self.cursor = self.connection.cursor()
        elif self.db_type == 'postgresql':
            self.connection = psycopg2.connect(host=self.host,
                                               user=self.username,
                                               password=self.password,
                                               database=self.database)
            self.cursor = self.connection.cursor()
        elif self.db_type == 'mssql':
            conn_str = f'DRIVER=ODBC Driver 17 for SQL Server;SERVER={self.host};DATABASE={self.database};UID={self.username};PWD={self.password}'
            self.connection = pyodbc.connect(conn_str)
            self.cursor = self.connection.cursor()
        else:
            raise Exception('Unsupported database type')

    def execute_query(self, query):
        self.cursor.execute(query)
        result = self.cursor.fetchall()
        return result

    def execute_update(self, query):
        self.cursor.execute(query)
        self.connection.commit()

    def disconnect(self):
        self.cursor.close()
        self.connection.close()

# 使用示例
db = Database('mysql', 'localhost', 'root', 'password', 'mydatabase')
db.connect()

# 执行查询
result = db.execute_query('SELECT * FROM mytable')
print(result)

# 执行更新
db.execute_update('UPDATE mytable SET column1 = 1 WHERE column2 = 2')

db.disconnect()

这个通用的数据访问类使用了不同的数据库连接库(pymysql、psycopg2和pyodbc)来连接不同类型的数据库(MySQL、PostgreSQL和SQL Server)。可以根据具体的需求,使用适当的数据库连接库和连接参数来初始化Database类的实例。然后,可以使用execute_query方法执行查询语句并返回结果,使用execute_update方法执行更新语句,最后使用disconnect方法断开与数据库的连接。

相关内容

热门资讯

实测透视"微扑克辅助... 实测透视"微扑克辅助器破解器"开挂(透视)辅助脚本(六分钟了解教你攻略)实测透视"微扑克辅助器破解器...
分享透视"wepok... 分享透视"wepoker透视挂的玩法"开挂(透视)辅助器(科普分享黑科技教程)分享透视"wepoke...
细节透视"有wepo... 细节透视"有wepoker透视吗"开挂(透视)辅助下载(记者揭秘透明挂教程)细节透视"有wepoke...
教会透视"wepok... 教会透视"wepokerplus透视挂真的假的"开挂(透视)辅助软件(发现玩家辅助教程)教会透视"w...
必看透视"WePoK... 必看透视"WePoKer辅助透视软件挂"开挂(透视)辅助挂(玩家亲测德州论坛)必看透视"WePoKe...
传授透视"wpk到底... 传授透视"wpk到底有没有辅助挂"开挂(透视)辅助下载(技术分享靠谱教程)传授透视"wpk到底有没有...
曝光透视"WePok... 曝光透视"WePoker脚本实战效果"开挂(透视)辅助挂(记者发布wpk教程)《曝光透视"WePok...
传授透视"wepok... 传授透视"wepoker德州出牌规律"开挂(透视)辅助下载(盘点几款黑科技教程)《传授透视"wepo...
正品透视"wepok... 正品透视"wepoker开挂功能"开挂(透视)辅助安装(热点讨论存在挂教程)正品透视"wepoker...
关于透视"微扑克封号... 关于透视"微扑克封号如何避免"开挂(透视)辅助器(实测分享切实教程)关于透视"微扑克封号如何避免"开...